Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ def search_articles(name: str) -> str:
|
|
32 |
def extract_entities(search_results: str) -> str:
|
33 |
"""Extract entities using Mistral 7B endpoint"""
|
34 |
modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
|
35 |
-
prompt = f"""Extract all person names and organization names from the following text.Do not extract products and service names. Only individuals and organizations.Bring the full details of the name in the newspaper article. For example, if only ACME is mentioned as company name, bring only ACME. IF ACME Inc is mentioned as company name, then you have to extract ACME Inc.
|
36 |
Format as:
|
37 |
PERSON: [name]
|
38 |
ORG: [organization name]
|
@@ -40,7 +40,7 @@ Text: {search_results}"""
|
|
40 |
try:
|
41 |
response = requests.post(
|
42 |
modal_endpoint,
|
43 |
-
json={"prompt": prompt, "max_tokens":
|
44 |
)
|
45 |
if response.status_code == 200:
|
46 |
return response.json().get("response", "No entities extracted")
|
@@ -53,7 +53,7 @@ Text: {search_results}"""
|
|
53 |
# === Gradio interface ===
|
54 |
|
55 |
def process_name_with_progress(name: str, progress=gr.Progress()):
|
56 |
-
"""Process name
|
57 |
if not name.strip():
|
58 |
yield "No name provided", "", ""
|
59 |
return
|
@@ -65,26 +65,26 @@ def process_name_with_progress(name: str, progress=gr.Progress()):
|
|
65 |
try:
|
66 |
# Step 1: Search
|
67 |
progress(0.1, desc="Searching for articles...")
|
68 |
-
search_results += f"
|
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"
|
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 += "
|
82 |
-
yield search_results, "
|
83 |
|
84 |
extract_start = time.time()
|
85 |
entities = extract_entities(articles_output)
|
86 |
extract_time = time.time() - extract_start
|
87 |
-
search_results += f"
|
88 |
yield search_results, entities, ""
|
89 |
|
90 |
|
@@ -107,7 +107,7 @@ with gr.Blocks(title="Related Entitites Finder") as demo:
|
|
107 |
|
108 |
|
109 |
with gr.Column():
|
110 |
-
output1 = gr.Textbox(label="Search Results", lines=
|
111 |
output2 = gr.Textbox(label="Extracted Entities and Relationships", lines=5, max_lines=10)
|
112 |
|
113 |
search_btn.click(
|
|
|
32 |
def extract_entities(search_results: str) -> str:
|
33 |
"""Extract entities using Mistral 7B endpoint"""
|
34 |
modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
|
35 |
+
prompt = f"""Extract all person names and organization names from the following text.Do not extract products and service names. Only individuals and organizations.Bring the full details of the name in the newspaper article. For example, if only ACME is mentioned as company name, bring only ACME. IF ACME Inc is mentioned as company name, then you have to extract ACME Inc. In addition, define the relationship between the entity and the company that is being searched. For example, is ACME Inc an owner of the company being searched? Then write 'owner'. Is ACME Inc. a funder of the company being searched? Then write 'funder'
|
36 |
Format as:
|
37 |
PERSON: [name]
|
38 |
ORG: [organization name]
|
|
|
40 |
try:
|
41 |
response = requests.post(
|
42 |
modal_endpoint,
|
43 |
+
json={"prompt": prompt, "max_tokens": 2500, "temperature": 0.15}
|
44 |
)
|
45 |
if response.status_code == 200:
|
46 |
return response.json().get("response", "No entities extracted")
|
|
|
53 |
# === Gradio interface ===
|
54 |
|
55 |
def process_name_with_progress(name: str, progress=gr.Progress()):
|
56 |
+
"""Process name """
|
57 |
if not name.strip():
|
58 |
yield "No name provided", "", ""
|
59 |
return
|
|
|
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(articles_output)
|
86 |
extract_time = time.time() - extract_start
|
87 |
+
search_results += f"Entity extraction completed in {extract_time:.2f}s\n"
|
88 |
yield search_results, entities, ""
|
89 |
|
90 |
|
|
|
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(
|