yk07577 commited on
Commit
5fbb944
·
verified ·
1 Parent(s): e991c1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -325,19 +325,17 @@ def ask_question(history, question):
325
  if "similar analysis" in question.lower() and (last_uploaded_company or "").lower() != "apple":
326
  similar_prompts = [item for item in prompt_history if item["company"].lower() == "apple"]
327
  if not similar_prompts:
328
- return history + [(question, "No Apple prompts stored to reference.")], gr.update()
329
 
330
  query_embedding = embedding_model.encode(question)
331
  scores = [np.dot(query_embedding, item["embedding"]) for item in similar_prompts]
332
  top_idxs = np.argsort(scores)[::-1][:3]
333
 
334
- # optional: rewrite "Apple" -> current company just for nicer text
335
  def rewrite_company(text: str) -> str:
336
  company = (last_uploaded_company or "this company").title()
337
  return text.replace("Apple's", f"{company}'s").replace("Apple", company)
338
 
339
- # quick support checks to skip prompts that the current file can't satisfy
340
- df = global_df # use the currently uploaded file
341
  def dataset_supports(prompt: str) -> bool:
342
  q = prompt.lower()
343
  if "research and development" in q or "r&d" in q:
@@ -346,9 +344,10 @@ def ask_question(history, question):
346
  or df["tag"].astype(str).str.contains("Research.*Development|TechnologyAndContent", case=False, na=False).any()
347
  )
348
  return bool(has_rnd)
349
- # add similar guards for other analyses if you like
350
  return True
351
 
 
 
352
  for i in top_idxs:
353
  past_q = similar_prompts[i]["question"]
354
  if not dataset_supports(past_q):
@@ -357,11 +356,17 @@ def ask_question(history, question):
357
  try:
358
  resp, img = handle_custom_question(rewrite_company(past_q))
359
  history.append((f"Reused: {rewrite_company(past_q)}", resp))
 
 
360
  except Exception as e:
361
  history.append((f"Skipped (error): {past_q}", f"Couldn’t run this on {last_uploaded_company.title()}: {e}"))
362
  continue
363
 
364
- return history, gr.update()
 
 
 
 
365
 
366
  # === Handle current question ===
367
  custom_response, custom_image = handle_custom_question(question)
 
325
  if "similar analysis" in question.lower() and (last_uploaded_company or "").lower() != "apple":
326
  similar_prompts = [item for item in prompt_history if item["company"].lower() == "apple"]
327
  if not similar_prompts:
328
+ return history + [(question, "No Apple prompts stored to reference.")], gr.update(value=None)
329
 
330
  query_embedding = embedding_model.encode(question)
331
  scores = [np.dot(query_embedding, item["embedding"]) for item in similar_prompts]
332
  top_idxs = np.argsort(scores)[::-1][:3]
333
 
 
334
  def rewrite_company(text: str) -> str:
335
  company = (last_uploaded_company or "this company").title()
336
  return text.replace("Apple's", f"{company}'s").replace("Apple", company)
337
 
338
+ df = global_df
 
339
  def dataset_supports(prompt: str) -> bool:
340
  q = prompt.lower()
341
  if "research and development" in q or "r&d" in q:
 
344
  or df["tag"].astype(str).str.contains("Research.*Development|TechnologyAndContent", case=False, na=False).any()
345
  )
346
  return bool(has_rnd)
 
347
  return True
348
 
349
+ image_to_show = None # <— NEW
350
+
351
  for i in top_idxs:
352
  past_q = similar_prompts[i]["question"]
353
  if not dataset_supports(past_q):
 
356
  try:
357
  resp, img = handle_custom_question(rewrite_company(past_q))
358
  history.append((f"Reused: {rewrite_company(past_q)}", resp))
359
+ if img is not None:
360
+ image_to_show = img # <— capture latest non-None image
361
  except Exception as e:
362
  history.append((f"Skipped (error): {past_q}", f"Couldn’t run this on {last_uploaded_company.title()}: {e}"))
363
  continue
364
 
365
+ # Return the captured image if any; otherwise clear the image pane
366
+ if image_to_show is not None:
367
+ return history, image_to_show
368
+ else:
369
+ return history, gr.update(value=None)
370
 
371
  # === Handle current question ===
372
  custom_response, custom_image = handle_custom_question(question)