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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -41
app.py CHANGED
@@ -3,66 +3,54 @@ import openai
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)
 
3
  import gradio as gr
4
  from monolith_theme import MonolithTheme
5
 
6
+ 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
+ def openai_chatbot(message, history=None):
 
 
15
  try:
16
+ conversation_history = history or []
17
+ if message: # Only append if there is a message to send
18
+ conversation_history.append({"role": "user", "content": message})
19
+
20
+ chat_completion = client.chat.completions.create(
21
+ model="gpt-3.5-turbo",
22
+ messages=conversation_history,
 
 
 
 
 
 
 
 
 
 
23
  )
24
+
25
+ # Append the new assistant message to the conversation history
26
+ response_text = chat_completion.choices[0].message.content
27
+ conversation_history.append({"role": "assistant", "content": response_text})
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
+ return history_formatted, conversation_history # Return the formatted history and the raw history
 
 
 
 
 
 
 
 
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,
42
  inputs=[
43
+ gr.Textbox(label="Your Message"),
44
  gr.State() # State input to hold the history
45
  ],
46
  outputs=[
47
+ gr.Textbox(label="Response", lines=10), # More lines for displaying the history
48
+ gr.State(hidden=True) # State output to pass the history back to the function
49
  ],
50
  title="Foundation Coin Assistant",
51
+ description="Interplanetary Governance, one query at a time.",
52
+ theme=theme
53
  )
54
 
55
  if __name__ == "__main__":
56
+ chat_interface.launch(debug=True)