Danaasa commited on
Commit
bee79a8
·
verified ·
1 Parent(s): 37b2efa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -77,12 +77,13 @@ def chat(message, history):
77
  Sends the user's message, waits for the assistant's reply, streams the reformatted response,
78
  and then returns only the new conversation for this call (without appending previous history).
79
  """
80
- # Ignore any incoming history to return a separate conversation
81
  history = []
 
82
  assistant_id = ASSISTANT_ID
83
  get_or_create_thread()
84
 
85
- # Get current messages to detect new ones.
86
  old_messages = client.beta.threads.messages.list(thread_id=thread_id).data
87
  old_ids = {msg.id for msg in old_messages}
88
 
@@ -93,13 +94,13 @@ def chat(message, history):
93
  content=message
94
  )
95
 
96
- # Start a run for the assistant.
97
  run = client.beta.threads.runs.create(
98
  thread_id=thread_id,
99
  assistant_id=assistant_id
100
  )
101
 
102
- # Wait until the run is complete.
103
  while True:
104
  run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
105
  if run_status.status == "completed":
@@ -116,7 +117,7 @@ def chat(message, history):
116
  assistant_response = msg.content[0].text.value
117
  break
118
 
119
- # Fallback: if not found, use the latest assistant message.
120
  if not assistant_response:
121
  for msg in reversed(new_messages):
122
  if msg.role == "assistant":
@@ -132,7 +133,7 @@ def chat(message, history):
132
  {"role": "assistant", "content": current_assistant_message}
133
  ]
134
 
135
- # Once streaming is complete, extract ingredients and send to Bubble.
136
  ingredients = extract_ingredients(assistant_response)
137
  if ingredients:
138
  send_to_bubble_database(ingredients, subject=message)
@@ -166,4 +167,4 @@ chat_interface = gr.ChatInterface(
166
  css=custom_css
167
  )
168
 
169
- chat_interface.launch(show_api=False, share=True)
 
77
  Sends the user's message, waits for the assistant's reply, streams the reformatted response,
78
  and then returns only the new conversation for this call (without appending previous history).
79
  """
80
+ # Reset history to an empty list so that previous conversation is not included.
81
  history = []
82
+
83
  assistant_id = ASSISTANT_ID
84
  get_or_create_thread()
85
 
86
+ # Get the current messages so we can detect new ones later.
87
  old_messages = client.beta.threads.messages.list(thread_id=thread_id).data
88
  old_ids = {msg.id for msg in old_messages}
89
 
 
94
  content=message
95
  )
96
 
97
+ # Start a run so the assistant processes the conversation.
98
  run = client.beta.threads.runs.create(
99
  thread_id=thread_id,
100
  assistant_id=assistant_id
101
  )
102
 
103
+ # Wait for the run to complete.
104
  while True:
105
  run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
106
  if run_status.status == "completed":
 
117
  assistant_response = msg.content[0].text.value
118
  break
119
 
120
+ # Fallback: if no new message is found, use the latest assistant message.
121
  if not assistant_response:
122
  for msg in reversed(new_messages):
123
  if msg.role == "assistant":
 
133
  {"role": "assistant", "content": current_assistant_message}
134
  ]
135
 
136
+ # Once streaming is complete, extract ingredients.
137
  ingredients = extract_ingredients(assistant_response)
138
  if ingredients:
139
  send_to_bubble_database(ingredients, subject=message)
 
167
  css=custom_css
168
  )
169
 
170
+ chat_interface.launch(show_api=False)