Spaces:
Runtime error
Runtime error
ambrosfitz
commited on
Commit
•
23a2abf
1
Parent(s):
dabe6f0
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
|
|
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
|
@@ -9,48 +9,34 @@ client = OpenAI(
|
|
9 |
base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
|
10 |
)
|
11 |
|
12 |
-
def predict(message, history
|
13 |
-
#
|
14 |
-
if history is None:
|
15 |
-
history = []
|
16 |
-
# Append the system role at the start if history is empty
|
17 |
-
if not history:
|
18 |
-
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."))
|
19 |
-
|
20 |
-
# Prepare messages in the format required by OpenAI
|
21 |
history_openai_format = []
|
22 |
for human, assistant in history:
|
23 |
history_openai_format.append({"role": "user", "content": human})
|
24 |
history_openai_format.append({"role": "assistant", "content": assistant})
|
25 |
history_openai_format.append({"role": "user", "content": message})
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
model="ambrosfitz/llama-3-history",
|
30 |
messages=history_openai_format,
|
31 |
temperature=0,
|
32 |
max_tokens=150,
|
33 |
-
stream=
|
34 |
)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# Update history with the latest exchange
|
44 |
-
history.append((message, full_message))
|
45 |
|
46 |
-
|
47 |
-
iface = gr.Interface(
|
48 |
fn=predict,
|
49 |
-
inputs=[gr.Textbox(label="Type your question here..."), gr.State()],
|
50 |
-
outputs=[gr.Textbox(), gr.State()],
|
51 |
title="HistoryBot Chat",
|
52 |
-
description="Interact with HistoryBot, a specialized assistant for American History. Ask any historical questions to get detailed and nuanced answers."
|
53 |
-
allow_flagging="never"
|
54 |
)
|
55 |
|
56 |
-
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
import gradio as gr
|
3 |
import os
|
|
|
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
|
|
|
9 |
base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
|
10 |
)
|
11 |
|
12 |
+
def predict(message, history):
|
13 |
+
# Format the history for OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
history_openai_format = []
|
15 |
for human, assistant in history:
|
16 |
history_openai_format.append({"role": "user", "content": human})
|
17 |
history_openai_format.append({"role": "assistant", "content": assistant})
|
18 |
history_openai_format.append({"role": "user", "content": message})
|
19 |
+
|
20 |
+
response = client.chat.completions.create(
|
21 |
+
model='ambrosfitz/llama-3-history',
|
|
|
22 |
messages=history_openai_format,
|
23 |
temperature=0,
|
24 |
max_tokens=150,
|
25 |
+
stream=False # Set to False for simplicity in this example
|
26 |
)
|
27 |
|
28 |
+
if response.choices and response.choices[0].text:
|
29 |
+
response_text = response.choices[0].text.strip()
|
30 |
+
history.append((message, response_text)) # Update history with the new Q&A pair
|
31 |
+
return response_text
|
32 |
+
else:
|
33 |
+
return "No response generated."
|
|
|
|
|
|
|
34 |
|
35 |
+
demo = gr.ChatInterface(
|
|
|
36 |
fn=predict,
|
|
|
|
|
37 |
title="HistoryBot Chat",
|
38 |
+
description="Interact with HistoryBot, a specialized assistant for American History. Ask any historical questions to get detailed and nuanced answers."
|
|
|
39 |
)
|
40 |
|
41 |
+
if __name__ == "__main__":
|
42 |
+
demo.launch()
|