ambrosfitz commited on
Commit
192371d
·
verified ·
1 Parent(s): 22b900e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -1,48 +1,53 @@
1
  import gradio as gr
2
  import os
3
  from openai import OpenAI
4
- import time
5
 
6
- # Initialize the OpenAI Client
 
7
  client = OpenAI(
8
- api_key=os.environ.get("RUNPOD_API_KEY"),
9
  base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
10
  )
11
 
12
- def runpod_chat(question, history=None):
13
- if history is None:
14
- history = [] # Ensure history starts as an empty list if none is provided
15
- # Add the role description at the beginning of the session
16
  if not history:
17
- history.append({"role": "system", "content": "You are a history assistant, that provides the best possible answers to any historical questions asked about American History. Be helpful and specific, providing any detailed nuance needed to have a full understanding of the question."})
18
- history.append({"role": "user", "content": question})
 
 
 
 
 
 
19
 
 
20
  response_stream = client.chat.completions.create(
21
  model="ambrosfitz/llama-3-history",
22
- messages=history,
23
  temperature=0,
24
  max_tokens=150,
25
  stream=True,
26
  )
27
-
28
- # Stream the response and accumulate full response before displaying
29
- full_response = "HistoryBot: "
30
- for message in response_stream:
31
- part = message.choices[0].delta.content if message.choices[0].delta.content is not None else ""
32
- full_response += part
33
 
34
- # Append the full response to history once complete
35
- history.append({"role": "assistant", "content": full_response})
 
 
 
 
36
 
37
- return full_response, history # Return full response and updated history to maintain state
 
38
 
39
- # Set up the Gradio interface
40
  iface = gr.Interface(
41
- fn=runpod_chat,
42
- inputs=[gr.Textbox(label="Enter your question:"), gr.State()],
43
- outputs=[gr.Textbox(label="Responses"), gr.State()],
44
  title="HistoryBot Chat",
45
- description="Interact with HistoryBot, a specialized assistant for American History. Ask any historical questions to get detailed and nuanced answers."
 
46
  )
47
 
48
  iface.launch()
 
1
  import gradio as gr
2
  import os
3
  from openai import OpenAI
 
4
 
5
+ # Initialize the OpenAI Client with your API key and endpoint
6
+ api_key = os.environ.get("RUNPOD_API_KEY") # Ensure your API key is correctly loaded from environment variables
7
  client = OpenAI(
8
+ api_key=api_key,
9
  base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
10
  )
11
 
12
+ def predict(message, history=[]):
13
+ # Append the system role at the start if history is empty
 
 
14
  if not history:
15
+ history.append(("system", "You are a history assistant, that provides the best possible answers to any historical questions asked about American History. Be helpful and specific, providing any detailed nuance needed to have a full understanding of the question."))
16
+
17
+ # Prepare messages in the format required by OpenAI
18
+ history_openai_format = []
19
+ for human, assistant in history:
20
+ history_openai_format.append({"role": "user", "content": human})
21
+ history_openai_format.append({"role": "assistant", "content": assistant})
22
+ history_openai_format.append({"role": "user", "content": message})
23
 
24
+ # Make the API call
25
  response_stream = client.chat.completions.create(
26
  model="ambrosfitz/llama-3-history",
27
+ messages=history_openai_format,
28
  temperature=0,
29
  max_tokens=150,
30
  stream=True,
31
  )
 
 
 
 
 
 
32
 
33
+ # Accumulate response chunks to form the full message
34
+ full_message = ""
35
+ for chunk in response_stream:
36
+ if chunk.choices[0].delta.content is not None:
37
+ full_message += chunk.choices[0].delta.content
38
+ yield full_message
39
 
40
+ # Update history with the latest exchange
41
+ history.append((message, full_message))
42
 
43
+ # Set up the Gradio chat interface
44
  iface = gr.Interface(
45
+ fn=predict,
46
+ inputs=[gr.Textbox(label="Type your question here..."), gr.State(label="History")],
47
+ outputs=[gr.Textbox(label="HistoryBot Responses"), gr.State()],
48
  title="HistoryBot Chat",
49
+ description="Interact with HistoryBot, a specialized assistant for American History. Ask any historical questions to get detailed and nuanced answers.",
50
+ allow_flagging="never"
51
  )
52
 
53
  iface.launch()