manoj555 commited on
Commit
3844ee8
·
verified ·
1 Parent(s): 9398545

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -7,22 +7,19 @@ client = OpenAI(
7
  api_key="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"
8
  )
9
 
10
- # System message
11
- system_prompt = {
12
- "role": "system",
13
- "content": "You are a helpful assistant to answer user queries."
14
- }
15
 
16
- # Main chat function with memory from Gradio
17
  def get_text_response(user_message, history):
18
- # Convert Gradio message history to OpenAI format
19
- formatted_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
20
- for i, msg in enumerate(sum(history, []))]
21
 
22
- # Add the latest user message
23
- messages = [system_prompt] + formatted_history + [{"role": "user", "content": user_message}]
24
 
25
- # Stream the response
26
  response = ""
27
  completion = client.chat.completions.create(
28
  model="nvidia/llama-3.1-nemotron-70b-instruct",
@@ -38,14 +35,18 @@ def get_text_response(user_message, history):
38
  if delta and delta.content:
39
  response += delta.content
40
 
41
- return response
 
 
42
 
43
- # Gradio Chat Interface
 
 
44
  demo = gr.ChatInterface(
45
  fn=get_text_response,
46
  title="🧠 Nemotron 70B Assistant",
47
  theme="soft",
48
- #chatbot=gr.Chatbot(height=400), # default type is compatible
49
  textbox=gr.Textbox(placeholder="Ask me anything...", container=False),
50
  examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"]
51
  )
 
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
+ # Main chat function with memory
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
+ # Add the user message to the history
20
+ messages = [system_prompt] + history + [{"role": "user", "content": user_message}]
21
 
22
+ # Stream response
23
  response = ""
24
  completion = client.chat.completions.create(
25
  model="nvidia/llama-3.1-nemotron-70b-instruct",
 
35
  if delta and delta.content:
36
  response += delta.content
37
 
38
+ # Update chat history
39
+ history.append({"role": "user", "content": user_message})
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
  )