Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,71 +3,69 @@ import openai
|
|
| 3 |
import gradio as gr
|
| 4 |
from monolith_theme import MonolithTheme
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
theme = MonolithTheme()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Initialize the OpenAI client once and use it in the openai_chatbot function
|
| 13 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
-
client = openai.OpenAI(api_key=api_key)
|
| 15 |
-
|
| 16 |
-
assistant_id = 'asst_wsFyYTDSGAkPibNdRGBY1Sa1' # Replace with new assistant ID if you have a different one
|
| 17 |
|
| 18 |
def openai_chatbot(message, history=None, thread_id=None):
|
| 19 |
try:
|
|
|
|
| 20 |
if not thread_id:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
thread_id = thread.id
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
role="user",
|
| 29 |
content=message
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Run the assistant to
|
| 33 |
-
run =
|
| 34 |
-
|
| 35 |
-
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Retrieve the messages
|
| 39 |
-
messages =
|
| 40 |
-
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
for msg in messages.data:
|
| 46 |
-
conversation_history.append(f"{msg['role'].title()}: {msg['content']}")
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
|
| 51 |
-
return
|
| 52 |
except Exception as e:
|
| 53 |
print(f"An error occurred: {e}")
|
| 54 |
-
|
| 55 |
-
return "Sorry, there was an error processing your request.", history, thread_id
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
fn=openai_chatbot,
|
| 59 |
inputs=[
|
| 60 |
gr.Textbox(label="Your Message"),
|
| 61 |
-
gr.State()
|
|
|
|
| 62 |
],
|
| 63 |
outputs=[
|
| 64 |
-
gr.Textbox(label="
|
| 65 |
-
gr.State()
|
|
|
|
| 66 |
],
|
| 67 |
-
title="
|
| 68 |
-
description="
|
| 69 |
-
theme=theme
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from monolith_theme import MonolithTheme
|
| 5 |
|
| 6 |
+
# Set your API key from an environment variable or directly
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
theme = MonolithTheme()
|
| 10 |
|
| 11 |
+
# Initialize the assistant ID you created via the OpenAI platform
|
| 12 |
+
assistant_id = 'asst_wsFyYTDSGAkPibNdRGBY1Sa1' # Replace with your Assistant ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def openai_chatbot(message, history=None, thread_id=None):
|
| 15 |
try:
|
| 16 |
+
# If there's no thread_id, create a new thread (new conversation)
|
| 17 |
if not thread_id:
|
| 18 |
+
thread = openai.Thread.create(assistant=assistant_id)
|
| 19 |
+
thread_id = thread['id']
|
|
|
|
| 20 |
|
| 21 |
+
# Add the user's message to the thread
|
| 22 |
+
if message:
|
| 23 |
+
openai.Message.create(
|
| 24 |
+
thread=thread_id,
|
| 25 |
role="user",
|
| 26 |
content=message
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Run the assistant on the thread to generate a response
|
| 30 |
+
run = openai.Run.create(
|
| 31 |
+
assistant=assistant_id,
|
| 32 |
+
thread=thread_id
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Retrieve the messages from the thread
|
| 36 |
+
messages = openai.Message.list(
|
| 37 |
+
thread=thread_id
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Format the conversation history
|
| 41 |
+
history_formatted = [f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages['data']]
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# The bot's latest response will be the last message in the list
|
| 44 |
+
bot_response = history_formatted[-1] if history_formatted else "No response from the bot."
|
| 45 |
|
| 46 |
+
return bot_response, history_formatted, thread_id
|
| 47 |
except Exception as e:
|
| 48 |
print(f"An error occurred: {e}")
|
| 49 |
+
return "Sorry, there was an error processing your request.", history, thread_id
|
|
|
|
| 50 |
|
| 51 |
+
# Create Gradio interface
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
fn=openai_chatbot,
|
| 54 |
inputs=[
|
| 55 |
gr.Textbox(label="Your Message"),
|
| 56 |
+
gr.State(label="Conversation History"),
|
| 57 |
+
gr.State(label="Thread ID")
|
| 58 |
],
|
| 59 |
outputs=[
|
| 60 |
+
gr.Textbox(label="Chatbot's Reply"),
|
| 61 |
+
gr.State(label="Updated History"),
|
| 62 |
+
gr.State(label="Updated Thread ID")
|
| 63 |
],
|
| 64 |
+
title="OpenAI Chatbot",
|
| 65 |
+
description="Talk to a bot powered by OpenAI GPT-3.",
|
| 66 |
+
theme=theme
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Launch the Gradio app
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
iface.launch()
|