Spaces:
Runtime error
Runtime error
kumshing-wilson-huang
commited on
Commit
•
618d572
1
Parent(s):
90f588c
OK
Browse files
app.py
CHANGED
@@ -1,7 +1,27 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
import gradio as gr
|
3 |
|
4 |
+
api_key = "sk-proj-KyHyIzEDLKe94DhrSq6LT3BlbkFJynj2NRPnnRwdDSV3XaZY" # Replace with your key
|
5 |
+
client = OpenAI(api_key=api_key)
|
6 |
|
7 |
+
|
8 |
+
def predict(message, history):
|
9 |
+
history_openai_format = []
|
10 |
+
for human, assistant in history:
|
11 |
+
history_openai_format.append({"role": "user", "content": human})
|
12 |
+
history_openai_format.append({"role": "assistant", "content": assistant})
|
13 |
+
history_openai_format.append({"role": "user", "content": message})
|
14 |
+
|
15 |
+
response = client.chat.completions.create(model='gpt-3.5-turbo',
|
16 |
+
messages=history_openai_format,
|
17 |
+
temperature=1.0,
|
18 |
+
stream=True)
|
19 |
+
|
20 |
+
partial_message = ""
|
21 |
+
for chunk in response:
|
22 |
+
if chunk.choices[0].delta.content is not None:
|
23 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
24 |
+
yield partial_message
|
25 |
+
|
26 |
+
|
27 |
+
gr.ChatInterface(predict).launch()
|