Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
|
| 4 |
-
# Set your OpenAI API key
|
| 5 |
openai.api_key = "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"
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Extract the chatbot's response
|
| 19 |
-
bot_response = response['choices'][0]['message']['content']
|
| 20 |
-
|
| 21 |
-
# Append the bot's response to the conversation history
|
| 22 |
-
conversation_history.append({"role": "assistant", "content": bot_response})
|
| 23 |
-
|
| 24 |
-
# Return the bot's response
|
| 25 |
-
return bot_response
|
| 26 |
|
| 27 |
-
# Gradio interface
|
| 28 |
iface = gr.Interface(
|
| 29 |
-
fn=
|
| 30 |
-
inputs=gr.Textbox(placeholder="Type your message here..."),
|
| 31 |
-
outputs=
|
| 32 |
title="Real-Time Chatbot with GPT-3.5",
|
| 33 |
description="Ask any question or start a conversation. Powered by OpenAI GPT-3.5-turbo."
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Launch the
|
| 37 |
-
|
| 38 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
|
| 4 |
+
# Set your OpenAI API key here
|
| 5 |
openai.api_key = "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"
|
| 6 |
|
| 7 |
+
def generate_response(message):
|
| 8 |
+
try:
|
| 9 |
+
# Call the OpenAI API to get a response
|
| 10 |
+
response = openai.ChatCompletion.create(
|
| 11 |
+
model="gpt-3.5-turbo",
|
| 12 |
+
messages=[{"role": "user", "content": message}],
|
| 13 |
+
max_tokens=150
|
| 14 |
+
)
|
| 15 |
+
return response['choices'][0]['message']['content'].strip()
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Create the Gradio interface
|
| 20 |
iface = gr.Interface(
|
| 21 |
+
fn=generate_response,
|
| 22 |
+
inputs=gr.inputs.Textbox(placeholder="Type your message here...", label="Message"),
|
| 23 |
+
outputs="text",
|
| 24 |
title="Real-Time Chatbot with GPT-3.5",
|
| 25 |
description="Ask any question or start a conversation. Powered by OpenAI GPT-3.5-turbo."
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Launch the interface
|
| 29 |
+
iface.launch()
|
|
|