Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -50,72 +50,78 @@ Text: {search_results}"""
|
|
50 |
return f"[ERROR] Extraction failed: {str(e)}"
|
51 |
|
52 |
|
53 |
-
# === Gradio interface ===
|
54 |
|
55 |
-
def
|
56 |
-
"""
|
57 |
if not name.strip():
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
search_results = ""
|
62 |
-
entities = ""
|
63 |
-
full_names = ""
|
64 |
-
|
65 |
try:
|
66 |
-
# Step 1: Search
|
67 |
-
progress(0.1, desc="Searching for articles...")
|
68 |
-
search_results += f"Starting search for: {name}\n"
|
69 |
-
yield search_results, "", ""
|
70 |
-
|
71 |
search_start = time.time()
|
72 |
articles_output = search_articles(name.strip())
|
73 |
search_time = time.time() - search_start
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
search_results += f"Search completed in {search_time:.2f}s\n"
|
76 |
-
search_results += f"{articles_output}\n"
|
77 |
-
yield search_results, "", ""
|
78 |
-
|
79 |
-
# Step 2: Extract entities
|
80 |
-
progress(0.5, desc="Extracting entities...")
|
81 |
-
search_results += "Starting entity extraction...\n"
|
82 |
-
yield search_results, "Extracting entities...", ""
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
extract_start = time.time()
|
85 |
-
entities = extract_entities(
|
86 |
extract_time = time.time() - extract_start
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
except Exception as e:
|
92 |
-
|
93 |
-
yield search_results + error_msg, entities or error_msg, full_names or error_msg
|
94 |
-
|
95 |
|
96 |
|
97 |
# === Gradio UI ===
|
98 |
|
99 |
-
with gr.Blocks(title="Related
|
100 |
gr.Markdown("# 🔎 Related Entities Finder")
|
101 |
gr.Markdown("Enter a business or project name to search for related articles and extract key entities.")
|
102 |
|
|
|
|
|
|
|
103 |
with gr.Row():
|
104 |
name_input = gr.Textbox(label="Name", placeholder="Enter business or project name")
|
105 |
with gr.Column():
|
106 |
-
search_btn = gr.Button("Search
|
|
|
107 |
|
108 |
-
|
109 |
with gr.Column():
|
110 |
output1 = gr.Textbox(label="Search Results", lines=50, max_lines=100)
|
111 |
output2 = gr.Textbox(label="Extracted Entities and Relationships", lines=5, max_lines=10)
|
112 |
-
|
|
|
113 |
search_btn.click(
|
114 |
-
fn=
|
115 |
inputs=[name_input],
|
116 |
-
outputs=[output1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
)
|
118 |
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
-
demo.launch()
|
|
|
50 |
return f"[ERROR] Extraction failed: {str(e)}"
|
51 |
|
52 |
|
53 |
+
# === Gradio interface functions ===
|
54 |
|
55 |
+
def search_only(name: str):
|
56 |
+
"""Perform search only and return results"""
|
57 |
if not name.strip():
|
58 |
+
return "No name provided", ""
|
59 |
+
|
|
|
|
|
|
|
|
|
|
|
60 |
try:
|
|
|
|
|
|
|
|
|
|
|
61 |
search_start = time.time()
|
62 |
articles_output = search_articles(name.strip())
|
63 |
search_time = time.time() - search_start
|
64 |
+
|
65 |
+
search_results = f"Search completed for: {name} in {search_time:.2f}s\n\n"
|
66 |
+
search_results += articles_output
|
67 |
+
|
68 |
+
return search_results, articles_output # Return both display and raw results
|
69 |
+
except Exception as e:
|
70 |
+
error_msg = f"[ERROR] Search failed: {str(e)}"
|
71 |
+
return error_msg, ""
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
def extract_only(stored_search_results: str):
|
75 |
+
"""Extract entities from stored search results"""
|
76 |
+
if not stored_search_results.strip():
|
77 |
+
return "No search results available. Please search first."
|
78 |
+
|
79 |
+
try:
|
80 |
extract_start = time.time()
|
81 |
+
entities = extract_entities(stored_search_results)
|
82 |
extract_time = time.time() - extract_start
|
83 |
+
|
84 |
+
extraction_results = f"Entity extraction completed in {extract_time:.2f}s\n\n"
|
85 |
+
extraction_results += entities
|
86 |
+
|
87 |
+
return extraction_results
|
88 |
except Exception as e:
|
89 |
+
return f"[ERROR] Extraction failed: {str(e)}"
|
|
|
|
|
90 |
|
91 |
|
92 |
# === Gradio UI ===
|
93 |
|
94 |
+
with gr.Blocks(title="Related Entities Finder") as demo:
|
95 |
gr.Markdown("# 🔎 Related Entities Finder")
|
96 |
gr.Markdown("Enter a business or project name to search for related articles and extract key entities.")
|
97 |
|
98 |
+
# State to store search results between operations
|
99 |
+
search_state = gr.State("")
|
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 Articles", variant="primary")
|
105 |
+
extract_btn = gr.Button("Extract Entities", variant="secondary")
|
106 |
|
|
|
107 |
with gr.Column():
|
108 |
output1 = gr.Textbox(label="Search Results", lines=50, max_lines=100)
|
109 |
output2 = gr.Textbox(label="Extracted Entities and Relationships", lines=5, max_lines=10)
|
110 |
+
|
111 |
+
# Search button click
|
112 |
search_btn.click(
|
113 |
+
fn=search_only,
|
114 |
inputs=[name_input],
|
115 |
+
outputs=[output1, search_state]
|
116 |
+
)
|
117 |
+
|
118 |
+
# Extract button click
|
119 |
+
extract_btn.click(
|
120 |
+
fn=extract_only,
|
121 |
+
inputs=[search_state],
|
122 |
+
outputs=[output2]
|
123 |
)
|
124 |
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
+
demo.launch()
|