dygoo commited on
Commit
b3950a6
·
verified ·
1 Parent(s): 2f675f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -4,6 +4,13 @@ import time
4
  import re
5
  from duckduckgo_search import DDGS
6
  from bs4 import BeautifulSoup
 
 
 
 
 
 
 
7
 
8
  # === Model functions ===
9
 
@@ -119,9 +126,7 @@ def search_articles(name: str, max_articles: int = 2) -> str:
119
  return f"[INFO] No articles found for {name}"
120
 
121
  def extract_entities(search_results: str) -> str:
122
- """Extract entities using Mistral 7B endpoint"""
123
- modal_endpoint = "https://msoaresdiego--mistral-llm-endpoint-fastapi-app.modal.run/generate"
124
-
125
  MAX_CHARS = 8000
126
  if len(search_results) > MAX_CHARS:
127
  trunc = search_results[:MAX_CHARS]
@@ -129,30 +134,27 @@ def extract_entities(search_results: str) -> str:
129
  search_results = trunc[:last_period + 1] if last_period > 3000 else trunc
130
 
131
  prompt = f"""Extract all named entities from the following text.
132
-
133
  Return a JSON object with two keys:
134
  - "people": a list of names of people mentioned
135
  - "organizations": a list of organization names mentioned
136
-
137
  Respond only with valid JSON. Do not include any explanations, comments, or additional formatting.
138
-
139
  Text:
140
  {search_results}"""
141
 
142
  try:
143
- response = requests.post(
144
- modal_endpoint,
145
- json={"prompt": prompt, "max_tokens": 1000, "temperature": 0.15},
146
- timeout=1000
 
 
 
 
 
 
147
  )
148
- if response.status_code == 200:
149
- return response.json().get("response", "No entities extracted")
150
- else:
151
- return f"[ERROR] API Error: {response.status_code} - {response.text}"
152
-
153
-
154
- except requests.exceptions.Timeout:
155
- return "[ERROR] Entity extraction timeout - please try again"
156
  except Exception as e:
157
  return f"[ERROR] Extraction failed: {str(e)}"
158
 
 
4
  import re
5
  from duckduckgo_search import DDGS
6
  from bs4 import BeautifulSoup
7
+ import anthropic
8
+ import os
9
+
10
+ # Initialize Anthropic client
11
+ client = anthropic.Anthropic(
12
+ api_key=os.getenv("ANTHROPIC_API_KEY") # Set your API key as environment variable
13
+ )
14
 
15
  # === Model functions ===
16
 
 
126
  return f"[INFO] No articles found for {name}"
127
 
128
  def extract_entities(search_results: str) -> str:
129
+ """Extract entities using Claude 4"""
 
 
130
  MAX_CHARS = 8000
131
  if len(search_results) > MAX_CHARS:
132
  trunc = search_results[:MAX_CHARS]
 
134
  search_results = trunc[:last_period + 1] if last_period > 3000 else trunc
135
 
136
  prompt = f"""Extract all named entities from the following text.
 
137
  Return a JSON object with two keys:
138
  - "people": a list of names of people mentioned
139
  - "organizations": a list of organization names mentioned
 
140
  Respond only with valid JSON. Do not include any explanations, comments, or additional formatting.
 
141
  Text:
142
  {search_results}"""
143
 
144
  try:
145
+ message = client.messages.create(
146
+ model="claude-sonnet-4-20250514",
147
+ max_tokens=1000,
148
+ temperature=0.15,
149
+ messages=[
150
+ {
151
+ "role": "user",
152
+ "content": prompt
153
+ }
154
+ ]
155
  )
156
+ return message.content[0].text
157
+
 
 
 
 
 
 
158
  except Exception as e:
159
  return f"[ERROR] Extraction failed: {str(e)}"
160