Ani14 commited on
Commit
82bf609
·
verified ·
1 Parent(s): bc0e336

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -33,14 +33,14 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=2
33
  raise RuntimeError(f"Invalid response: {result}")
34
  return result["choices"][0]["message"]["content"]
35
 
36
- # --- Search Utilities ---
37
  def get_arxiv_papers(query, max_results=3):
38
  from urllib.parse import quote_plus
39
  url = f"http://export.arxiv.org/api/query?search_query=all:{quote_plus(query)}&start=0&max_results={max_results}"
40
  feed = feedparser.parse(url)
41
  return [{
42
- "title": e.title,
43
- "summary": e.summary.strip().replace("\n", " "),
44
  "url": next((l.href for l in e.links if l.type == "application/pdf"), "")
45
  } for e in feed.entries]
46
 
@@ -48,25 +48,26 @@ def get_semantic_scholar_papers(query, max_results=3):
48
  url = "https://api.semanticscholar.org/graph/v1/paper/search"
49
  params = {"query": query, "limit": max_results, "fields": "title,abstract,url"}
50
  response = requests.get(url, params=params)
 
51
  return [{
52
- "title": p.get("title", ""),
53
- "summary": p.get("abstract", "No abstract").strip(),
54
  "url": p.get("url", "")
55
- } for p in response.json().get("data", [])]
56
 
57
  def search_duckduckgo(query, max_results=3):
58
  with DDGS() as ddgs:
59
  return [{
60
- "title": r["title"],
61
- "snippet": r["body"],
62
- "url": r["href"]
63
  } for r in ddgs.text(query, max_results=max_results)]
64
 
65
  def get_image_urls(query, max_images=3):
66
  with DDGS() as ddgs:
67
  return [img["image"] for img in ddgs.images(query, max_results=max_images)]
68
 
69
- # --- Autonomous Research Agent ---
70
  def autonomous_research_agent(topic):
71
  arxiv = get_arxiv_papers(topic)
72
  scholar = get_semantic_scholar_papers(topic)
@@ -102,6 +103,7 @@ Now synthesize this information into:
102
  2. A novel research direction
103
  3. A full markdown-formatted research article (continuous, no section labels, academic tone)
104
  """
 
105
  response = call_llm([{"role": "user", "content": prompt}], max_tokens=3000)
106
 
107
  # Append Sources
@@ -140,7 +142,7 @@ if st.button("Run Research Agent"):
140
  except Exception as e:
141
  st.error(f"Failed: {e}")
142
 
143
- # --- Follow-up Q&A ---
144
  st.divider()
145
  st.subheader("💬 Follow-up Q&A")
146
  followup = st.text_input("Ask a follow-up question:")
 
33
  raise RuntimeError(f"Invalid response: {result}")
34
  return result["choices"][0]["message"]["content"]
35
 
36
+ # --- Source Utilities ---
37
  def get_arxiv_papers(query, max_results=3):
38
  from urllib.parse import quote_plus
39
  url = f"http://export.arxiv.org/api/query?search_query=all:{quote_plus(query)}&start=0&max_results={max_results}"
40
  feed = feedparser.parse(url)
41
  return [{
42
+ "title": e.title or "Untitled",
43
+ "summary": (e.summary or "No summary available").replace("\n", " ").strip(),
44
  "url": next((l.href for l in e.links if l.type == "application/pdf"), "")
45
  } for e in feed.entries]
46
 
 
48
  url = "https://api.semanticscholar.org/graph/v1/paper/search"
49
  params = {"query": query, "limit": max_results, "fields": "title,abstract,url"}
50
  response = requests.get(url, params=params)
51
+ papers = response.json().get("data", [])
52
  return [{
53
+ "title": p.get("title") or "Untitled",
54
+ "summary": (p.get("abstract") or "No abstract available").strip(),
55
  "url": p.get("url", "")
56
+ } for p in papers]
57
 
58
  def search_duckduckgo(query, max_results=3):
59
  with DDGS() as ddgs:
60
  return [{
61
+ "title": r["title"] or "Untitled",
62
+ "snippet": r["body"] or "",
63
+ "url": r["href"] or ""
64
  } for r in ddgs.text(query, max_results=max_results)]
65
 
66
  def get_image_urls(query, max_images=3):
67
  with DDGS() as ddgs:
68
  return [img["image"] for img in ddgs.images(query, max_results=max_images)]
69
 
70
+ # --- Research Agent ---
71
  def autonomous_research_agent(topic):
72
  arxiv = get_arxiv_papers(topic)
73
  scholar = get_semantic_scholar_papers(topic)
 
103
  2. A novel research direction
104
  3. A full markdown-formatted research article (continuous, no section labels, academic tone)
105
  """
106
+
107
  response = call_llm([{"role": "user", "content": prompt}], max_tokens=3000)
108
 
109
  # Append Sources
 
142
  except Exception as e:
143
  st.error(f"Failed: {e}")
144
 
145
+ # --- Follow-up Chat ---
146
  st.divider()
147
  st.subheader("💬 Follow-up Q&A")
148
  followup = st.text_input("Ask a follow-up question:")