Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Retrieve API key from environment variable
|
| 6 |
+
api_key = os.environ.get('sk-proj-EDY3jlWEqNq4xkGk0inbT3BlbkFJKbN26l1g02vd16B11Ksb')
|
| 7 |
+
|
| 8 |
+
client = OpenAI(api_key=api_key)
|
| 9 |
+
|
| 10 |
+
def predict(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 |
+
history_openai_format.append({"role": "user", "content": message})
|
| 16 |
+
|
| 17 |
+
response = client.chat.completions.create(model='gpt-3.5-turbo',
|
| 18 |
+
messages= history_openai_format,
|
| 19 |
+
temperature=1.0,
|
| 20 |
+
stream=True)
|
| 21 |
+
|
| 22 |
+
partial_message = ""
|
| 23 |
+
for chunk in response:
|
| 24 |
+
if chunk.choices[0].delta.content is not None:
|
| 25 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
| 26 |
+
yield partial_message
|
| 27 |
+
|
| 28 |
+
gr.ChatInterface(predict).launch()
|