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