Shreyas094 commited on
Commit
75e9d4c
·
verified ·
1 Parent(s): 6f866bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -86,7 +86,7 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, tempe
86
 
87
  for i in range(num_calls):
88
  print(f"Starting API call {i+1}")
89
- if stop_clicked and stop_clicked.value: # Check if stop_clicked is not None and its value is True
90
  print("Stop clicked, breaking loop")
91
  break
92
  try:
@@ -97,7 +97,7 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, tempe
97
  temperature=temperature,
98
  stream=True,
99
  ):
100
- if stop_clicked and stop_clicked.value:
101
  print("Stop clicked during streaming, breaking")
102
  break
103
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
@@ -255,23 +255,29 @@ with gr.Blocks() as demo:
255
  if is_generating:
256
  print("Already generating, returning")
257
  return message, history, is_generating, stop_clicked
 
258
  is_generating = True
259
- stop_clicked.value = False # Reset stop_clicked at the start of generation
260
 
 
 
 
 
 
 
261
  try:
262
  print(f"Generating response for: {message}")
263
  if use_web_search:
264
  print("Using web search")
265
- main_content, sources = get_response_with_search(query, model, num_calls=num_calls, temperature=temperature)
266
  formatted_response = f"{main_content}\n\nSources:\n{sources}"
267
  else:
268
  print("Using PDF search")
269
- response = get_response_from_pdf(query, model, num_calls=num_calls, temperature=temperature)
270
  formatted_response = response
271
 
272
  print(f"Generated response: {formatted_response[:100]}...")
273
 
274
- if not stop_clicked.value:
275
  print("Appending to history")
276
  history.append((message, formatted_response))
277
  else:
@@ -283,7 +289,7 @@ with gr.Blocks() as demo:
283
  is_generating = False
284
  print(f"Returning history with {len(history)} items")
285
  return "", history, is_generating, stop_clicked
286
-
287
  def on_submit(message, history, use_web_search, model, temperature, num_calls, is_generating, stop_clicked):
288
  print(f"Submit button clicked with message: {message}")
289
  _, new_history, new_is_generating, new_stop_clicked = protected_generate_response(
 
86
 
87
  for i in range(num_calls):
88
  print(f"Starting API call {i+1}")
89
+ if (isinstance(stop_clicked, gr.State) and stop_clicked.value) or stop_clicked:
90
  print("Stop clicked, breaking loop")
91
  break
92
  try:
 
97
  temperature=temperature,
98
  stream=True,
99
  ):
100
+ if (isinstance(stop_clicked, gr.State) and stop_clicked.value) or stop_clicked:
101
  print("Stop clicked during streaming, breaking")
102
  break
103
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
 
255
  if is_generating:
256
  print("Already generating, returning")
257
  return message, history, is_generating, stop_clicked
258
+
259
  is_generating = True
 
260
 
261
+ # Handle both boolean and Gradio State object
262
+ if isinstance(stop_clicked, gr.State):
263
+ stop_clicked.value = False
264
+ else:
265
+ stop_clicked = False
266
+
267
  try:
268
  print(f"Generating response for: {message}")
269
  if use_web_search:
270
  print("Using web search")
271
+ main_content, sources = get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, stop_clicked=stop_clicked)
272
  formatted_response = f"{main_content}\n\nSources:\n{sources}"
273
  else:
274
  print("Using PDF search")
275
+ response = get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature, stop_clicked=stop_clicked)
276
  formatted_response = response
277
 
278
  print(f"Generated response: {formatted_response[:100]}...")
279
 
280
+ if not (isinstance(stop_clicked, gr.State) and stop_clicked.value) and not stop_clicked:
281
  print("Appending to history")
282
  history.append((message, formatted_response))
283
  else:
 
289
  is_generating = False
290
  print(f"Returning history with {len(history)} items")
291
  return "", history, is_generating, stop_clicked
292
+
293
  def on_submit(message, history, use_web_search, model, temperature, num_calls, is_generating, stop_clicked):
294
  print(f"Submit button clicked with message: {message}")
295
  _, new_history, new_is_generating, new_stop_clicked = protected_generate_response(