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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -38
app.py CHANGED
@@ -3,71 +3,69 @@ import openai
3
  import gradio as gr
4
  from monolith_theme import MonolithTheme
5
 
6
- print(gr.__version__)
 
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,
59
  inputs=[
60
  gr.Textbox(label="Your Message"),
61
- gr.State() # State input to hold the history
 
62
  ],
63
  outputs=[
64
- gr.Textbox(label="Response"), # Textbox for the chatbot's response
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
 
 
72
  if __name__ == "__main__":
73
- chat_interface.launch(debug=True)
 
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()