Spaces:
Sleeping
Sleeping
Marko Vukovic
commited on
Commit
·
1276d79
1
Parent(s):
afdd51b
Show errors to user
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import gradio as gr
|
|
| 3 |
|
| 4 |
client = AsyncOpenAI()
|
| 5 |
|
| 6 |
-
|
| 7 |
async def predict(message, history, model, temperature):
|
| 8 |
history_openai_format = []
|
| 9 |
for human, assistant in history:
|
|
@@ -11,19 +10,22 @@ async def predict(message, history, model, temperature):
|
|
| 11 |
history_openai_format.append({"role": "assistant", "content": assistant})
|
| 12 |
history_openai_format.append({"role": "user", "content": message})
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
model_dropdown = gr.Dropdown(
|
| 29 |
label="Model",
|
|
|
|
| 3 |
|
| 4 |
client = AsyncOpenAI()
|
| 5 |
|
|
|
|
| 6 |
async def predict(message, history, model, temperature):
|
| 7 |
history_openai_format = []
|
| 8 |
for human, assistant in history:
|
|
|
|
| 10 |
history_openai_format.append({"role": "assistant", "content": assistant})
|
| 11 |
history_openai_format.append({"role": "user", "content": message})
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
response = await client.chat.completions.create(
|
| 15 |
+
model=model,
|
| 16 |
+
messages=history_openai_format,
|
| 17 |
+
temperature=temperature,
|
| 18 |
+
stream=True,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
partial_message = ""
|
| 22 |
+
async for chunk in response:
|
| 23 |
+
if chunk.choices[0].delta.content is not None:
|
| 24 |
+
partial_message += chunk.choices[0].delta.content
|
| 25 |
+
yield partial_message
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise gr.Error(str(e))
|
| 29 |
|
| 30 |
model_dropdown = gr.Dropdown(
|
| 31 |
label="Model",
|