Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
|
4 |
-
# NVIDIA-compatible OpenAI client
|
5 |
client = OpenAI(
|
6 |
base_url="https://integrate.api.nvidia.com/v1",
|
7 |
api_key="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"
|
8 |
)
|
9 |
|
10 |
-
# System prompt
|
11 |
system_prompt = {"role": "system", "content": "You are a helpful assistant to answer user queries."}
|
12 |
|
13 |
-
|
14 |
-
def get_text_response(user_message, history):
|
15 |
-
# history is a list of {"role": ..., "content": ...}
|
16 |
if history is None:
|
17 |
history = []
|
18 |
|
19 |
-
#
|
20 |
-
messages = [system_prompt]
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
# Stream response
|
23 |
response = ""
|
24 |
completion = client.chat.completions.create(
|
25 |
model="nvidia/llama-3.1-nemotron-70b-instruct",
|
@@ -35,19 +34,13 @@ def get_text_response(user_message, history):
|
|
35 |
if delta and delta.content:
|
36 |
response += delta.content
|
37 |
|
38 |
-
|
39 |
-
history
|
40 |
-
history.append({"role": "assistant", "content": response})
|
41 |
|
42 |
-
return response, history
|
43 |
-
|
44 |
-
# Gradio Chat UI
|
45 |
demo = gr.ChatInterface(
|
46 |
fn=get_text_response,
|
47 |
title="🧠 Nemotron 70B Assistant",
|
48 |
theme="soft",
|
49 |
-
chatbot=gr.Chatbot(height=400, type="messages"),
|
50 |
-
textbox=gr.Textbox(placeholder="Ask me anything...", container=False),
|
51 |
examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"]
|
52 |
)
|
53 |
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
|
|
|
4 |
client = OpenAI(
|
5 |
base_url="https://integrate.api.nvidia.com/v1",
|
6 |
api_key="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"
|
7 |
)
|
8 |
|
|
|
9 |
system_prompt = {"role": "system", "content": "You are a helpful assistant to answer user queries."}
|
10 |
|
11 |
+
def get_text_response(user_message, history=None):
|
|
|
|
|
12 |
if history is None:
|
13 |
history = []
|
14 |
|
15 |
+
# Prepare messages for OpenAI API in the correct format
|
16 |
+
messages = [system_prompt]
|
17 |
+
for user_msg, assistant_msg in history:
|
18 |
+
messages.append({"role": "user", "content": user_msg})
|
19 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
20 |
+
messages.append({"role": "user", "content": user_message})
|
21 |
|
|
|
22 |
response = ""
|
23 |
completion = client.chat.completions.create(
|
24 |
model="nvidia/llama-3.1-nemotron-70b-instruct",
|
|
|
34 |
if delta and delta.content:
|
35 |
response += delta.content
|
36 |
|
37 |
+
history.append((user_message, response))
|
38 |
+
return history, history
|
|
|
39 |
|
|
|
|
|
|
|
40 |
demo = gr.ChatInterface(
|
41 |
fn=get_text_response,
|
42 |
title="🧠 Nemotron 70B Assistant",
|
43 |
theme="soft",
|
|
|
|
|
44 |
examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"]
|
45 |
)
|
46 |
|