Caerii commited on
Commit
33cc5c7
·
1 Parent(s): da6dbaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -3,69 +3,66 @@ import openai
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()
 
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)