Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,52 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import os
|
4 |
+
import time
|
5 |
|
6 |
+
# Initialize the OpenAI Client with your API key and endpoint
|
7 |
+
api_key = os.environ.get("RUNPOD_API_KEY") # Make sure to set your environment variable correctly
|
8 |
+
client = OpenAI(
|
9 |
+
api_key=api_key,
|
10 |
+
base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
|
11 |
+
)
|
12 |
|
13 |
+
def get_response(user_message, history):
|
14 |
+
# Format the history for OpenAI
|
15 |
+
history_openai_format = []
|
16 |
+
for human, assistant in history:
|
17 |
+
if human: # Ensure there's a human message
|
18 |
+
history_openai_format.append({"role": "user", "content": human})
|
19 |
+
if assistant: # Ensure there's an assistant message
|
20 |
+
history_openai_format.append({"role": "assistant", "content": assistant})
|
21 |
+
history_openai_format.append({"role": "user", "content": user_message})
|
22 |
+
|
23 |
+
# Make the API call
|
24 |
+
response = client.chat.completions.create(
|
25 |
+
model='ambrosfitz/llama-3-history',
|
26 |
+
messages=history_openai_format,
|
27 |
+
temperature=0.5,
|
28 |
+
max_tokens=150
|
29 |
+
)
|
30 |
+
|
31 |
+
# Get the text response
|
32 |
+
bot_message = response.choices[0].message['content'].strip() if response.choices else "No response generated."
|
33 |
+
return bot_message
|
34 |
+
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
chatbot = gr.Chatbot()
|
37 |
+
msg = gr.Textbox()
|
38 |
+
clear = gr.Button("Clear")
|
39 |
+
|
40 |
+
def user(user_message, history):
|
41 |
+
if not user_message.strip(): # Handle empty input gracefully
|
42 |
+
return "", history
|
43 |
+
bot_response = get_response(user_message, history)
|
44 |
+
return "", history + [[user_message, bot_response]]
|
45 |
+
|
46 |
+
def clear_chat():
|
47 |
+
return "", [] # Clear the chat history
|
48 |
+
|
49 |
+
msg.submit(user, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
50 |
+
clear.click(clear_chat, inputs=None, outputs=[msg, chatbot])
|
51 |
+
|
52 |
+
demo.launch()
|