Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,32 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
|
| 4 |
+
# Replace 'your-api-key' with your actual OpenAI API key
|
| 5 |
+
openai.api_key = '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'
|
| 6 |
|
| 7 |
+
def openai_chatbot(message, history):
|
| 8 |
+
# Convert history format for OpenAI API
|
| 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 |
+
# Call the OpenAI ChatCompletion API
|
| 16 |
+
response = openai.ChatCompletion.create(
|
| 17 |
+
model='gpt-3.5-turbo',
|
| 18 |
+
messages=history_openai_format,
|
| 19 |
+
temperature=1.0
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Extract and return the response
|
| 23 |
+
return response.choices[0].message['content']
|
| 24 |
+
|
| 25 |
+
chat_interface = gr.ChatInterface(
|
| 26 |
+
fn=openai_chatbot,
|
| 27 |
+
title="OpenAI Chatbot",
|
| 28 |
+
description="Talk to a bot powered by OpenAI GPT-3."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
chat_interface.launch()
|