Spaces:
Runtime error
Runtime error
now with OpenAI
Browse files
app.py
CHANGED
|
@@ -1,16 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def chat(message, history):
|
| 5 |
-
|
| 6 |
-
for
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
demo = gr.ChatInterface(
|
| 11 |
fn=chat,
|
| 12 |
# examples=[{"text": "hello"}, {"text": "hola"}, {"text": "merhaba"}],
|
| 13 |
-
title="
|
| 14 |
multimodal=True)
|
| 15 |
|
| 16 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
api_key = os.getenv("OPENAI_APIKEY")
|
| 7 |
+
client = OpenAI(api_key=api_key)
|
| 8 |
+
|
| 9 |
|
| 10 |
def chat(message, history):
|
| 11 |
+
history_openai_format = []
|
| 12 |
+
for human, assistant in history:
|
| 13 |
+
history_openai_format.append({"role": "user", "content": human })
|
| 14 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
| 15 |
+
|
| 16 |
+
history_openai_format.append({"role": "user", "content": message['text']})
|
| 17 |
+
|
| 18 |
+
response = client.chat.completions.create(
|
| 19 |
+
model='gpt-3.5-turbo',
|
| 20 |
+
messages= history_openai_format,
|
| 21 |
+
temperature=1.0,
|
| 22 |
+
stream=True)
|
| 23 |
+
|
| 24 |
+
partial_message = ""
|
| 25 |
+
for chunk in response:
|
| 26 |
+
if chunk.choices[0].delta.content is not None:
|
| 27 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
| 28 |
+
yield partial_message
|
| 29 |
|
| 30 |
demo = gr.ChatInterface(
|
| 31 |
fn=chat,
|
| 32 |
# examples=[{"text": "hello"}, {"text": "hola"}, {"text": "merhaba"}],
|
| 33 |
+
title="Summarization and more",
|
| 34 |
multimodal=True)
|
| 35 |
|
| 36 |
demo.launch()
|