Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
13 |
+
if not history:
|
14 |
+
history = []
|
15 |
+
history.append({"role": "user", "content": question})
|
16 |
+
|
17 |
+
response_stream = client.chat.completions.create(
|
18 |
+
model="ambrosfitz/llama-3-history",
|
19 |
+
messages=history,
|
20 |
+
temperature=0,
|
21 |
+
max_tokens=1028,
|
22 |
+
stream=True,
|
23 |
+
)
|
24 |
+
|
25 |
+
# Stream the response and add to history
|
26 |
+
responses = []
|
27 |
+
for message in response_stream:
|
28 |
+
response = message.choices[0].delta.content
|
29 |
+
responses.append(response)
|
30 |
+
history.append({"role": "assistant", "content": response})
|
31 |
+
time.sleep(0.3) # Simulate typing delay
|
32 |
+
yield "RunPod: " + response
|
33 |
+
|
34 |
+
# Set up the Gradio interface
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=runpod_chat,
|
37 |
+
inputs=[
|
38 |
+
gr.inputs.Textbox(label="Enter your question:"),
|
39 |
+
gr.inputs.State(label="History")
|
40 |
+
],
|
41 |
+
outputs="chat",
|
42 |
+
title="RunPod Chat",
|
43 |
+
description="This app interfaces with RunPod's API to provide responses to your queries."
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch()
|