Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,69 +3,66 @@ import openai
|
|
| 3 |
import gradio as gr
|
| 4 |
from monolith_theme import MonolithTheme
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 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
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
if message:
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
role="user",
|
| 26 |
content=message
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
# Run the assistant
|
| 30 |
-
run =
|
| 31 |
-
|
| 32 |
-
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
# Retrieve the messages
|
| 36 |
-
messages =
|
| 37 |
-
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 52 |
-
iface = gr.Interface(
|
| 53 |
fn=openai_chatbot,
|
| 54 |
inputs=[
|
| 55 |
-
gr.Textbox(label="Your Message"),
|
| 56 |
-
gr.State(
|
| 57 |
-
gr.State(label="Thread ID")
|
| 58 |
],
|
| 59 |
outputs=[
|
| 60 |
-
gr.Textbox(label="
|
| 61 |
-
gr.State(
|
| 62 |
-
gr.State(label="Updated Thread ID")
|
| 63 |
],
|
| 64 |
-
title="
|
| 65 |
-
description="
|
| 66 |
-
theme=theme
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Launch the Gradio app
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from monolith_theme import MonolithTheme
|
| 5 |
|
| 6 |
+
# Initialize the OpenAI client once and use it in the openai_chatbot function
|
| 7 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
client = openai.OpenAI(api_key=api_key)
|
| 9 |
|
| 10 |
+
assistant_id = 'asst_wsFyYTDSGAkPibNdRGBY1Sa1' # Replace with your Assistant's ID
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def openai_chatbot(message, history=None, thread_id=None):
|
| 13 |
try:
|
|
|
|
| 14 |
if not thread_id:
|
| 15 |
+
# Create a new thread if one does not exist
|
| 16 |
+
thread = client.beta.threads.create()
|
| 17 |
+
thread_id = thread.id
|
| 18 |
+
|
| 19 |
if message:
|
| 20 |
+
# Add the user's message to the thread
|
| 21 |
+
client.beta.threads.messages.create(
|
| 22 |
+
thread_id=thread_id,
|
| 23 |
role="user",
|
| 24 |
content=message
|
| 25 |
)
|
| 26 |
+
|
| 27 |
+
# Run the assistant to get the response
|
| 28 |
+
run = client.beta.threads.runs.create(
|
| 29 |
+
thread_id=thread_id,
|
| 30 |
+
assistant_id=assistant_id
|
| 31 |
)
|
| 32 |
+
|
| 33 |
+
# Retrieve the messages to get the assistant's latest response
|
| 34 |
+
messages = client.beta.threads.messages.list(
|
| 35 |
+
thread_id=thread_id
|
| 36 |
)
|
| 37 |
+
|
| 38 |
+
# Extract the conversation history and the assistant's response
|
| 39 |
+
conversation_history = []
|
| 40 |
+
for msg in messages.data:
|
| 41 |
+
conversation_history.append(f"{msg['role'].title()}: {msg['content']}")
|
| 42 |
+
|
| 43 |
+
# Join the conversation history into a formatted string
|
| 44 |
+
history_formatted = "\n".join(conversation_history)
|
| 45 |
+
|
| 46 |
+
return history_formatted, conversation_history, thread_id # Return the formatted history, raw history, and thread_id
|
| 47 |
except Exception as e:
|
| 48 |
print(f"An error occurred: {e}")
|
| 49 |
+
# Return the error message and the existing history so the error message doesn't get appended
|
| 50 |
return "Sorry, there was an error processing your request.", history, thread_id
|
| 51 |
|
| 52 |
+
chat_interface = gr.Interface(
|
|
|
|
| 53 |
fn=openai_chatbot,
|
| 54 |
inputs=[
|
| 55 |
+
gr.Textbox("text", label="Your Message"),
|
| 56 |
+
gr.State() # State input to hold the history
|
|
|
|
| 57 |
],
|
| 58 |
outputs=[
|
| 59 |
+
gr.Textbox("text", label="Response"), # Textbox for the chatbot's response
|
| 60 |
+
gr.State() # State output to pass the history back to the function
|
|
|
|
| 61 |
],
|
| 62 |
+
title="Foundation Coin Assistant",
|
| 63 |
+
description="Interplanetary governance, just one query away!",
|
| 64 |
+
theme=MonolithTheme() # Apply the custom theme here
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
chat_interface.launch(debug=True)
|