dygoo commited on
Commit
3c6afdf
Β·
verified Β·
1 Parent(s): d0bd726

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -61
app.py CHANGED
@@ -1,20 +1,12 @@
1
  import gradio as gr
2
  import requests
3
- import re
4
- from typing import List, Dict
5
- import os
6
  import time
7
  from duckduckgo_search import DDGS
8
 
9
- # ----------------------------
10
- # Model functions
11
- # ----------------------------
12
-
13
  def search_articles(name: str) -> str:
14
- """Search for 3 newspaper articles containing the name and keywords using DuckDuckGo"""
15
  keywords = ['founders', 'partners', 'funders', 'owners']
16
  search_query = f'"{name}" ({" OR ".join(keywords)}) site:news'
17
-
18
  try:
19
  with DDGS() as ddgs:
20
  results = list(ddgs.text(search_query, max_results=3))
@@ -31,20 +23,17 @@ def search_articles(name: str) -> str:
31
  return f"Search failed: {str(e)}"
32
 
33
  def extract_entities(search_results: str) -> str:
34
- """Extract entities using Mistral 7B endpoint"""
35
  modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
36
-
37
  prompt = f"""Extract all person names and organization names from the following text.
38
  Format as:
39
  PERSON: [name]
40
  ORG: [organization name]
41
  Text: {search_results}"""
42
-
43
  try:
44
  response = requests.post(
45
  modal_endpoint,
46
  json={"prompt": prompt, "max_tokens": 500, "temperature": 0.1},
47
- timeout=90 # Increased timeout
48
  )
49
  if response.status_code == 200:
50
  return response.json().get("response", "No entities extracted")
@@ -54,19 +43,16 @@ Text: {search_results}"""
54
  return f"Extraction failed: {str(e)}"
55
 
56
  def find_full_names(search_results: str, entities: str) -> str:
57
- """Find full names using Mistral 7B endpoint"""
58
  modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
59
-
60
  prompt = f"""Based on the search results, find the full names and titles/roles for these entities:
61
  Entities: {entities}
62
  Search Results: {search_results}
63
  Provide full names with their roles/titles where mentioned."""
64
-
65
  try:
66
  response = requests.post(
67
  modal_endpoint,
68
  json={"prompt": prompt, "max_tokens": 300, "temperature": 0.1},
69
- timeout=90 # Increased timeout
70
  )
71
  if response.status_code == 200:
72
  return response.json().get("response", "No full names found")
@@ -75,81 +61,60 @@ Provide full names with their roles/titles where mentioned."""
75
  except Exception as e:
76
  return f"Full name extraction failed: {str(e)}"
77
 
78
- # ----------------------------
79
- # Gradio Interface Logic
80
- # ----------------------------
81
-
82
  def process_name_with_progress(name: str, progress=gr.Progress(track_tqdm=True)):
83
  if not name.strip():
84
- yield "", "", ""
85
 
86
- # Start with all outputs empty
87
  search_results = ""
88
  entities = ""
89
  full_names = ""
90
 
91
  try:
92
- # Step 1: Search
93
- progress(0.1, desc="Searching for articles...")
94
  search_results = search_articles(name.strip())
95
- yield search_results, "", ""
96
 
97
- # Step 2: Extract Entities
98
- progress(0.4, desc="Extracting entities...")
99
  entities = extract_entities(search_results)
100
- yield search_results, entities, ""
101
 
102
- # Step 3: Resolve Full Names
103
- progress(0.7, desc="Finding full names...")
104
  full_names = find_full_names(search_results, entities)
105
- progress(1.0, desc="Done.")
106
- yield search_results, entities, full_names
107
 
108
- except Exception as e:
109
- error_msg = f"Error: {str(e)}"
110
- yield search_results or error_msg, entities or error_msg, full_names or error_msg
111
 
112
- def process_name_simple(name: str):
113
- """Simple debugging version (no progress bars)"""
114
- if not name.strip():
115
- return "", "", ""
116
-
117
- search_results = search_articles(name.strip())
118
- entities = extract_entities(search_results)
119
- full_names = find_full_names(search_results, entities)
120
- return search_results, entities, full_names
121
-
122
- # ----------------------------
123
- # Gradio UI
124
- # ----------------------------
125
 
 
126
  with gr.Blocks(title="Name Research Tool") as demo:
127
- gr.Markdown("# πŸ” Name Research Tool")
128
  gr.Markdown("Enter a business or project name to search for related articles and extract key entities.")
129
 
130
  with gr.Row():
131
  name_input = gr.Textbox(label="Name", placeholder="Enter business or project name")
132
  with gr.Column():
133
- search_btn = gr.Button("πŸ”Ž Search (Real-time)", variant="primary")
134
- debug_btn = gr.Button("🐞 Search (Debug Mode)", variant="secondary")
135
 
136
  with gr.Column():
137
  output1 = gr.Textbox(label="Search Results", lines=10, max_lines=20)
138
  output2 = gr.Textbox(label="Extracted Entities", lines=5, max_lines=10)
139
  output3 = gr.Textbox(label="Full Names", lines=5, max_lines=10)
 
140
 
141
- # Real-time (streamed) version
142
  search_btn.click(
143
  fn=process_name_with_progress,
144
  inputs=[name_input],
145
- outputs=[output1, output2, output3]
146
- )
147
-
148
- # Debug version (no progress)
149
- debug_btn.click(
150
- fn=process_name_simple,
151
- inputs=[name_input],
152
- outputs=[output1, output2, output3]
153
  )
154
 
155
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import requests
 
 
 
3
  import time
4
  from duckduckgo_search import DDGS
5
 
6
+ # --- Model Functions ---
 
 
 
7
  def search_articles(name: str) -> str:
 
8
  keywords = ['founders', 'partners', 'funders', 'owners']
9
  search_query = f'"{name}" ({" OR ".join(keywords)}) site:news'
 
10
  try:
11
  with DDGS() as ddgs:
12
  results = list(ddgs.text(search_query, max_results=3))
 
23
  return f"Search failed: {str(e)}"
24
 
25
  def extract_entities(search_results: str) -> str:
 
26
  modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
 
27
  prompt = f"""Extract all person names and organization names from the following text.
28
  Format as:
29
  PERSON: [name]
30
  ORG: [organization name]
31
  Text: {search_results}"""
 
32
  try:
33
  response = requests.post(
34
  modal_endpoint,
35
  json={"prompt": prompt, "max_tokens": 500, "temperature": 0.1},
36
+ timeout=180 # Increased timeout
37
  )
38
  if response.status_code == 200:
39
  return response.json().get("response", "No entities extracted")
 
43
  return f"Extraction failed: {str(e)}"
44
 
45
  def find_full_names(search_results: str, entities: str) -> str:
 
46
  modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
 
47
  prompt = f"""Based on the search results, find the full names and titles/roles for these entities:
48
  Entities: {entities}
49
  Search Results: {search_results}
50
  Provide full names with their roles/titles where mentioned."""
 
51
  try:
52
  response = requests.post(
53
  modal_endpoint,
54
  json={"prompt": prompt, "max_tokens": 300, "temperature": 0.1},
55
+ timeout=180 # Increased timeout
56
  )
57
  if response.status_code == 200:
58
  return response.json().get("response", "No full names found")
 
61
  except Exception as e:
62
  return f"Full name extraction failed: {str(e)}"
63
 
64
+ # --- Pipeline Function with Progress & Status ---
 
 
 
65
  def process_name_with_progress(name: str, progress=gr.Progress(track_tqdm=True)):
66
  if not name.strip():
67
+ yield "", "", "", "Please enter a name."
68
 
 
69
  search_results = ""
70
  entities = ""
71
  full_names = ""
72
 
73
  try:
74
+ progress(0.1)
75
+ status = "πŸ” Searching for articles..."
76
  search_results = search_articles(name.strip())
77
+ yield search_results, "", "", status
78
 
79
+ progress(0.4)
80
+ status = "πŸ“„ Extracting entities from articles..."
81
  entities = extract_entities(search_results)
82
+ yield search_results, entities, "", status
83
 
84
+ progress(0.7)
85
+ status = "🧠 Finding full names and roles..."
86
  full_names = find_full_names(search_results, entities)
87
+ yield search_results, entities, full_names, status
 
88
 
89
+ progress(1.0)
90
+ yield search_results, entities, full_names, "βœ… Complete!"
 
91
 
92
+ except Exception as e:
93
+ err = f"❌ Error: {str(e)}"
94
+ yield search_results or err, entities or err, full_names or err, err
 
 
 
 
 
 
 
 
 
 
95
 
96
+ # --- Gradio Interface ---
97
  with gr.Blocks(title="Name Research Tool") as demo:
98
+ gr.Markdown("# Name Research Tool")
99
  gr.Markdown("Enter a business or project name to search for related articles and extract key entities.")
100
 
101
  with gr.Row():
102
  name_input = gr.Textbox(label="Name", placeholder="Enter business or project name")
103
  with gr.Column():
104
+ search_btn = gr.Button("Search (Real-time)", variant="primary")
105
+ debug_btn = gr.Button("Search (Debug Mode)", variant="secondary")
106
 
107
  with gr.Column():
108
  output1 = gr.Textbox(label="Search Results", lines=10, max_lines=20)
109
  output2 = gr.Textbox(label="Extracted Entities", lines=5, max_lines=10)
110
  output3 = gr.Textbox(label="Full Names", lines=5, max_lines=10)
111
+ status_output = gr.Textbox(label="Status / Progress", lines=1, interactive=False)
112
 
113
+ # Real-time search with progress
114
  search_btn.click(
115
  fn=process_name_with_progress,
116
  inputs=[name_input],
117
+ outputs=[output1, output2, output3, status_output]
 
 
 
 
 
 
 
118
  )
119
 
120
  if __name__ == "__main__":