Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,13 @@ from transformers import pipeline
|
|
| 10 |
from newspaper import Article
|
| 11 |
import hashlib, datetime
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
@@ -32,58 +39,86 @@ class BasicAgent:
|
|
| 32 |
|
| 33 |
class SmartResearchAgent:
|
| 34 |
def __init__(self):
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
def search_web(self, query: str) -> str:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
article.download()
|
| 50 |
article.parse()
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
def generate_citation(self,
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def __call__(self, question: str) -> str:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if "summarize" in question.lower():
|
| 66 |
-
return self.summarize(question.replace("summarize", "", 1).strip())
|
| 67 |
-
|
| 68 |
-
elif "cite" in question.lower() or "citation" in question.lower():
|
| 69 |
-
return self.generate_citation(question.split()[-1])
|
| 70 |
-
|
| 71 |
-
elif "search" in question.lower():
|
| 72 |
-
return self.search_web(question.replace("search", "", 1).strip())
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if first_url:
|
| 80 |
summary = self.summarize(first_url)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
|
|
|
| 87 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 88 |
"""
|
| 89 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 10 |
from newspaper import Article
|
| 11 |
import hashlib, datetime
|
| 12 |
|
| 13 |
+
import hashlib
|
| 14 |
+
import datetime
|
| 15 |
+
from newspaper import Article
|
| 16 |
+
from duckduckgo_search import DDGS
|
| 17 |
+
from transformers import pipeline
|
| 18 |
+
import logging
|
| 19 |
+
|
| 20 |
|
| 21 |
# (Keep Constants as is)
|
| 22 |
# --- Constants ---
|
|
|
|
| 39 |
|
| 40 |
class SmartResearchAgent:
|
| 41 |
def __init__(self):
|
| 42 |
+
logging.info("Initializing SmartResearchAgent")
|
| 43 |
+
self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 44 |
|
| 45 |
def search_web(self, query: str) -> str:
|
| 46 |
+
try:
|
| 47 |
+
with DDGS() as ddgs:
|
| 48 |
+
results = ddgs.text(query, max_results=3)
|
| 49 |
+
top = [f"{i+1}. {r['title']}: {r['href']}" for i, r in enumerate(results)]
|
| 50 |
+
return "\n".join(top) if top else "No results found."
|
| 51 |
+
except Exception as e:
|
| 52 |
+
logging.error(f"Search error: {e}")
|
| 53 |
+
return "Error during web search."
|
| 54 |
+
|
| 55 |
+
def summarize(self, input_text_or_url: str) -> str:
|
| 56 |
+
try:
|
| 57 |
+
if input_text_or_url.startswith("http"):
|
| 58 |
+
article = Article(input_text_or_url)
|
| 59 |
article.download()
|
| 60 |
article.parse()
|
| 61 |
+
input_text_or_url = article.text
|
| 62 |
+
if not input_text_or_url.strip():
|
| 63 |
+
return "No content to summarize."
|
| 64 |
+
summary = self.summarizer(input_text_or_url, max_length=160, min_length=40, do_sample=False)
|
| 65 |
+
return summary[0]['summary_text'].strip()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logging.error(f"Summarization error: {e}")
|
| 68 |
+
return "Error during summarization."
|
| 69 |
|
| 70 |
+
def generate_citation(self, url: str) -> str:
|
| 71 |
+
try:
|
| 72 |
+
citation_id = hashlib.md5(url.encode()).hexdigest()[:6]
|
| 73 |
+
year = datetime.datetime.now().year
|
| 74 |
+
citation = (
|
| 75 |
+
f"@article{{cite{citation_id},\n"
|
| 76 |
+
f" title={{Generated Reference}},\n"
|
| 77 |
+
f" author={{Unknown}},\n"
|
| 78 |
+
f" journal={{Online}},\n"
|
| 79 |
+
f" year={{ {year} }},\n"
|
| 80 |
+
f" url={{ {url} }}\n"
|
| 81 |
+
f"}}"
|
| 82 |
+
)
|
| 83 |
+
return citation
|
| 84 |
+
except Exception as e:
|
| 85 |
+
logging.error(f"Citation error: {e}")
|
| 86 |
+
return "Error during citation generation."
|
| 87 |
|
| 88 |
def __call__(self, question: str) -> str:
|
| 89 |
+
logging.info(f"Received question: {question}")
|
| 90 |
+
q_lower = question.lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
try:
|
| 93 |
+
if q_lower.startswith("search:"):
|
| 94 |
+
query = question.split(":", 1)[1].strip()
|
| 95 |
+
result = self.search_web(query)
|
| 96 |
+
elif q_lower.startswith("summarize:"):
|
| 97 |
+
target = question.split(":", 1)[1].strip()
|
| 98 |
+
result = self.summarize(target)
|
| 99 |
+
elif q_lower.startswith("generate citation:") or q_lower.startswith("cite:"):
|
| 100 |
+
url = question.split(":", 1)[1].strip()
|
| 101 |
+
result = self.generate_citation(url)
|
| 102 |
+
else:
|
| 103 |
+
# Default: search + summarize first link
|
| 104 |
+
search_result = self.search_web(question)
|
| 105 |
+
logging.debug(f"Search result:\n{search_result}")
|
| 106 |
+
first_url = next((line.split(": ", 1)[-1] for line in search_result.splitlines() if "http" in line), None)
|
| 107 |
if first_url:
|
| 108 |
summary = self.summarize(first_url)
|
| 109 |
+
result = f"{summary}\n\nSource: {first_url}"
|
| 110 |
+
else:
|
| 111 |
+
result = "Sorry, I couldn't find relevant information."
|
| 112 |
+
|
| 113 |
+
logging.info(f"Agent answer: {result}")
|
| 114 |
+
return result
|
| 115 |
|
| 116 |
+
except Exception as e:
|
| 117 |
+
logging.exception("Unhandled error in agent call")
|
| 118 |
+
return f"Agent error: {e}"
|
| 119 |
|
| 120 |
|
| 121 |
+
|
| 122 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 123 |
"""
|
| 124 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|