dygoo commited on
Commit
196bf92
·
verified ·
1 Parent(s): 917501b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -11,7 +11,7 @@ from dateutil import parser
11
  import json
12
  import threading
13
  from concurrent.futures import ThreadPoolExecutor, TimeoutError, as_completed
14
- import signal
15
 
16
  # Initialize Anthropic client
17
  client = anthropic.Anthropic(
@@ -31,6 +31,7 @@ def check_cancellation():
31
 
32
  # === Enhanced Model functions with progress tracking ===
33
 
 
34
  def extract_publication_date(soup, url):
35
  """Extract publication date from article HTML - same as before"""
36
  try:
@@ -405,6 +406,7 @@ Text:
405
  progress(0.5, desc="Sending request to AI model...")
406
 
407
  message = client.messages.create(
 
408
  model="claude-sonnet-4-20250514",
409
  max_tokens=1500,
410
  temperature=0.1,
@@ -444,8 +446,9 @@ def search_only_enhanced(name: str, article_count: int, progress=gr.Progress()):
444
 
445
  articles_output = search_articles_enhanced(name.strip(), max_articles=article_count, progress=progress)
446
 
 
447
  if "[CANCELLED]" in articles_output:
448
- return articles_output, ""
449
 
450
  elapsed = time.time() - start
451
  progress(1.0, desc=f"Search completed in {elapsed:.1f}s")
@@ -537,24 +540,29 @@ with gr.Blocks(title="Enhanced Founder Finder", theme=gr.themes.Soft()) as demo:
537
  show_copy_button=True
538
  )
539
 
540
- # Event handlers
541
- search_click = search_btn.click(
 
542
  fn=search_only_enhanced,
543
  inputs=[name_input, article_count_slider],
544
  outputs=[output1, search_state],
545
- show_progress=True
546
  )
547
 
 
548
  cancel_btn.click(
549
  fn=cancel_search,
550
- outputs=[status_output]
 
 
 
551
  )
552
 
553
  extract_btn.click(
554
  fn=extract_only_enhanced,
555
  inputs=[search_state, name_input],
556
  outputs=[output2],
557
- show_progress=True
558
  )
559
 
560
  # Add some example companies
@@ -569,11 +577,15 @@ with gr.Blocks(title="Enhanced Founder Finder", theme=gr.themes.Soft()) as demo:
569
  inputs=[name_input, article_count_slider],
570
  )
571
 
 
 
 
572
  if __name__ == "__main__":
573
  demo.launch(
574
  share=False,
575
  show_error=True
576
  )
 
577
  '''
578
  import gradio as gr
579
  import requests
 
11
  import json
12
  import threading
13
  from concurrent.futures import ThreadPoolExecutor, TimeoutError, as_completed
14
+
15
 
16
  # Initialize Anthropic client
17
  client = anthropic.Anthropic(
 
31
 
32
  # === Enhanced Model functions with progress tracking ===
33
 
34
+
35
  def extract_publication_date(soup, url):
36
  """Extract publication date from article HTML - same as before"""
37
  try:
 
406
  progress(0.5, desc="Sending request to AI model...")
407
 
408
  message = client.messages.create(
409
+ # The model name is left as you provided.
410
  model="claude-sonnet-4-20250514",
411
  max_tokens=1500,
412
  temperature=0.1,
 
446
 
447
  articles_output = search_articles_enhanced(name.strip(), max_articles=article_count, progress=progress)
448
 
449
+ # This now correctly handles cancellation from the main thread
450
  if "[CANCELLED]" in articles_output:
451
+ return "🛑 Search was cancelled by user.", ""
452
 
453
  elapsed = time.time() - start
454
  progress(1.0, desc=f"Search completed in {elapsed:.1f}s")
 
540
  show_copy_button=True
541
  )
542
 
543
+ # --- FIX 2: Implement proper Gradio event cancellation ---
544
+ # The search_btn.click event is assigned to a variable.
545
+ search_event = search_btn.click(
546
  fn=search_only_enhanced,
547
  inputs=[name_input, article_count_slider],
548
  outputs=[output1, search_state],
549
+ show_progress="full" # Use "full" for better progress bar experience
550
  )
551
 
552
+ # The cancel_btn.click event now correctly cancels the search_event.
553
  cancel_btn.click(
554
  fn=cancel_search,
555
+ inputs=None,
556
+ outputs=[status_output],
557
+ # This is the crucial argument that links the cancel button to the search event.
558
+ cancels=[search_event]
559
  )
560
 
561
  extract_btn.click(
562
  fn=extract_only_enhanced,
563
  inputs=[search_state, name_input],
564
  outputs=[output2],
565
+ show_progress="full"
566
  )
567
 
568
  # Add some example companies
 
577
  inputs=[name_input, article_count_slider],
578
  )
579
 
580
+ # --- FIX 1: Enable the queue. This is essential for long-running tasks on Hugging Face Spaces. ---
581
+ demo.queue()
582
+
583
  if __name__ == "__main__":
584
  demo.launch(
585
  share=False,
586
  show_error=True
587
  )
588
+
589
  '''
590
  import gradio as gr
591
  import requests