Spaces:
Sleeping
Sleeping
Update model.py
Browse files
model.py
CHANGED
@@ -8,67 +8,51 @@ def search_articles(name: str) -> str:
|
|
8 |
"""Search for 3 newspaper articles containing the name and keywords using DuckDuckGo"""
|
9 |
keywords = ['founders', 'partners', 'funders', 'owners']
|
10 |
search_query = f'"{name}" ({" OR ".join(keywords)}) site:news'
|
11 |
-
|
12 |
try:
|
13 |
with DDGS() as ddgs:
|
14 |
results = list(ddgs.text(search_query, max_results=3))
|
15 |
-
|
16 |
if not results:
|
17 |
return f"No articles found for {name}"
|
18 |
-
|
19 |
articles = []
|
20 |
for i, result in enumerate(results, 1):
|
21 |
article = f"**{i}. {result['title']}**\n"
|
22 |
article += f"Source: {result['href']}\n"
|
23 |
article += f"{result['body']}\n"
|
24 |
articles.append(article)
|
25 |
-
|
26 |
return "\n\n".join(articles)
|
27 |
-
|
28 |
except Exception as e:
|
29 |
return f"Search failed: {str(e)}"
|
30 |
|
31 |
def extract_entities(search_results: str) -> str:
|
32 |
-
"""Extract entities using
|
33 |
-
modal_endpoint =
|
34 |
-
modal_token = os.getenv("MODAL_TOKEN")
|
35 |
-
|
36 |
-
if not modal_endpoint or not modal_token:
|
37 |
-
return "Modal Labs credentials not configured"
|
38 |
|
39 |
-
prompt = f"""Extract all person names and organization names from the following text.
|
40 |
Format as:
|
41 |
PERSON: [name]
|
42 |
ORG: [organization name]
|
43 |
|
44 |
Text: {search_results}"""
|
45 |
-
|
46 |
try:
|
47 |
response = requests.post(
|
48 |
modal_endpoint,
|
49 |
-
headers={"Authorization": f"Bearer {modal_token}"},
|
50 |
json={
|
51 |
"prompt": prompt,
|
52 |
"max_tokens": 500,
|
53 |
"temperature": 0.1
|
54 |
}
|
55 |
)
|
56 |
-
|
57 |
if response.status_code == 200:
|
58 |
-
return response.json().get("
|
59 |
else:
|
60 |
return f"API Error: {response.status_code}"
|
61 |
-
|
62 |
except Exception as e:
|
63 |
return f"Extraction failed: {str(e)}"
|
64 |
|
65 |
def find_full_names(search_results: str, entities: str) -> str:
|
66 |
-
"""Find full names using
|
67 |
-
modal_endpoint =
|
68 |
-
modal_token = os.getenv("MODAL_TOKEN")
|
69 |
-
|
70 |
-
if not modal_endpoint or not modal_token:
|
71 |
-
return "Modal Labs credentials not configured"
|
72 |
|
73 |
prompt = f"""Based on the search results, find the full names and titles/roles for these entities:
|
74 |
|
@@ -77,22 +61,19 @@ Entities: {entities}
|
|
77 |
Search Results: {search_results}
|
78 |
|
79 |
Provide full names with their roles/titles where mentioned."""
|
80 |
-
|
81 |
try:
|
82 |
response = requests.post(
|
83 |
modal_endpoint,
|
84 |
-
headers={"Authorization": f"Bearer {modal_token}"},
|
85 |
json={
|
86 |
"prompt": prompt,
|
87 |
"max_tokens": 300,
|
88 |
"temperature": 0.1
|
89 |
}
|
90 |
)
|
91 |
-
|
92 |
if response.status_code == 200:
|
93 |
-
return response.json().get("
|
94 |
else:
|
95 |
return f"API Error: {response.status_code}"
|
96 |
-
|
97 |
except Exception as e:
|
98 |
return f"Full name extraction failed: {str(e)}"
|
|
|
8 |
"""Search for 3 newspaper articles containing the name and keywords using DuckDuckGo"""
|
9 |
keywords = ['founders', 'partners', 'funders', 'owners']
|
10 |
search_query = f'"{name}" ({" OR ".join(keywords)}) site:news'
|
|
|
11 |
try:
|
12 |
with DDGS() as ddgs:
|
13 |
results = list(ddgs.text(search_query, max_results=3))
|
|
|
14 |
if not results:
|
15 |
return f"No articles found for {name}"
|
|
|
16 |
articles = []
|
17 |
for i, result in enumerate(results, 1):
|
18 |
article = f"**{i}. {result['title']}**\n"
|
19 |
article += f"Source: {result['href']}\n"
|
20 |
article += f"{result['body']}\n"
|
21 |
articles.append(article)
|
|
|
22 |
return "\n\n".join(articles)
|
|
|
23 |
except Exception as e:
|
24 |
return f"Search failed: {str(e)}"
|
25 |
|
26 |
def extract_entities(search_results: str) -> str:
|
27 |
+
"""Extract entities using Mistral 7B endpoint"""
|
28 |
+
modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
prompt = f"""Extract all person names and organization names from the following text.
|
31 |
Format as:
|
32 |
PERSON: [name]
|
33 |
ORG: [organization name]
|
34 |
|
35 |
Text: {search_results}"""
|
36 |
+
|
37 |
try:
|
38 |
response = requests.post(
|
39 |
modal_endpoint,
|
|
|
40 |
json={
|
41 |
"prompt": prompt,
|
42 |
"max_tokens": 500,
|
43 |
"temperature": 0.1
|
44 |
}
|
45 |
)
|
|
|
46 |
if response.status_code == 200:
|
47 |
+
return response.json().get("response", "No entities extracted")
|
48 |
else:
|
49 |
return f"API Error: {response.status_code}"
|
|
|
50 |
except Exception as e:
|
51 |
return f"Extraction failed: {str(e)}"
|
52 |
|
53 |
def find_full_names(search_results: str, entities: str) -> str:
|
54 |
+
"""Find full names using Mistral 7B endpoint"""
|
55 |
+
modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
|
|
|
|
|
|
|
|
|
56 |
|
57 |
prompt = f"""Based on the search results, find the full names and titles/roles for these entities:
|
58 |
|
|
|
61 |
Search Results: {search_results}
|
62 |
|
63 |
Provide full names with their roles/titles where mentioned."""
|
64 |
+
|
65 |
try:
|
66 |
response = requests.post(
|
67 |
modal_endpoint,
|
|
|
68 |
json={
|
69 |
"prompt": prompt,
|
70 |
"max_tokens": 300,
|
71 |
"temperature": 0.1
|
72 |
}
|
73 |
)
|
|
|
74 |
if response.status_code == 200:
|
75 |
+
return response.json().get("response", "No full names found")
|
76 |
else:
|
77 |
return f"API Error: {response.status_code}"
|
|
|
78 |
except Exception as e:
|
79 |
return f"Full name extraction failed: {str(e)}"
|