Ani14 commited on
Commit
9c00982
·
verified ·
1 Parent(s): 0660708

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -13
app.py CHANGED
@@ -3,6 +3,7 @@ import streamlit as st
3
  import requests
4
  import datetime
5
  import time
 
6
  from dotenv import load_dotenv
7
  from tavily import TavilyClient
8
  import feedparser
@@ -19,8 +20,6 @@ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
19
  tavily = TavilyClient(api_key=TAVILY_API_KEY)
20
 
21
  # --- Helper Functions ---
22
- import json
23
-
24
  def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3500, temperature=0.7):
25
  url = "https://openrouter.ai/api/v1/chat/completions"
26
  headers = {
@@ -35,9 +34,7 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
35
  "temperature": temperature,
36
  "stream": True
37
  }
38
-
39
  with requests.post(url, headers=headers, json=data, stream=True) as response:
40
- content = ""
41
  for line in response.iter_lines():
42
  if line:
43
  decoded = line.decode("utf-8")
@@ -49,12 +46,10 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
49
  delta = parsed.get("choices", [{}])[0].get("delta", {})
50
  token = delta.get("content", "")
51
  if token:
52
- content += token
53
- yield token # Yield only the new token, not full content each time
54
  except json.JSONDecodeError:
55
  pass
56
 
57
-
58
  def get_sources(topic, domains=None):
59
  query = topic
60
  if domains:
@@ -144,6 +139,7 @@ def generate_download_button(file, label, mime_type):
144
  </a>
145
  """
146
 
 
147
  st.set_page_config("Deep Research Bot", layout="centered")
148
  st.markdown("""
149
  <style>
@@ -189,25 +185,22 @@ if research_button and topic:
189
  if not all_sources:
190
  raise ValueError("Unable to fetch any sources. Please try again later.")
191
 
192
-
193
  merged = merge_duplicates(all_sources)
194
-
195
  st.markdown("---")
196
  st.subheader("🖼 Source Previews")
197
-
198
  cols = st.columns(2)
199
  for i, m in enumerate(merged):
200
  if m.get("image_url"):
201
  with cols[i % 2]:
202
  st.image(m["image_url"], caption=m["title"], use_column_width=True)
203
-
204
  citations = [generate_apa_citation(m['title'], m['url'], m['source']) for m in merged]
205
  combined_text = "\n\n".join(
206
  [f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}..." for m in merged]
207
  )
208
  status.update(label="🧠 Synthesizing final report...")
209
 
210
-
211
  prompt = f"""
212
  You are an expert assistant. Based on the following sources, write a {report_type.lower()} in a {tone.lower()} tone on the topic: {topic}.
213
 
@@ -242,4 +235,4 @@ APA Citations:
242
  st.success("✅ No major overlaps found.")
243
 
244
  except Exception as e:
245
- st.error(f"❌ Error occurred: {e}")
 
3
  import requests
4
  import datetime
5
  import time
6
+ import json
7
  from dotenv import load_dotenv
8
  from tavily import TavilyClient
9
  import feedparser
 
20
  tavily = TavilyClient(api_key=TAVILY_API_KEY)
21
 
22
  # --- Helper Functions ---
 
 
23
  def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3500, temperature=0.7):
24
  url = "https://openrouter.ai/api/v1/chat/completions"
25
  headers = {
 
34
  "temperature": temperature,
35
  "stream": True
36
  }
 
37
  with requests.post(url, headers=headers, json=data, stream=True) as response:
 
38
  for line in response.iter_lines():
39
  if line:
40
  decoded = line.decode("utf-8")
 
46
  delta = parsed.get("choices", [{}])[0].get("delta", {})
47
  token = delta.get("content", "")
48
  if token:
49
+ yield token
 
50
  except json.JSONDecodeError:
51
  pass
52
 
 
53
  def get_sources(topic, domains=None):
54
  query = topic
55
  if domains:
 
139
  </a>
140
  """
141
 
142
+ # --- Streamlit UI ---
143
  st.set_page_config("Deep Research Bot", layout="centered")
144
  st.markdown("""
145
  <style>
 
185
  if not all_sources:
186
  raise ValueError("Unable to fetch any sources. Please try again later.")
187
 
 
188
  merged = merge_duplicates(all_sources)
189
+
190
  st.markdown("---")
191
  st.subheader("🖼 Source Previews")
 
192
  cols = st.columns(2)
193
  for i, m in enumerate(merged):
194
  if m.get("image_url"):
195
  with cols[i % 2]:
196
  st.image(m["image_url"], caption=m["title"], use_column_width=True)
197
+
198
  citations = [generate_apa_citation(m['title'], m['url'], m['source']) for m in merged]
199
  combined_text = "\n\n".join(
200
  [f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}..." for m in merged]
201
  )
202
  status.update(label="🧠 Synthesizing final report...")
203
 
 
204
  prompt = f"""
205
  You are an expert assistant. Based on the following sources, write a {report_type.lower()} in a {tone.lower()} tone on the topic: {topic}.
206
 
 
235
  st.success("✅ No major overlaps found.")
236
 
237
  except Exception as e:
238
+ st.error(f"❌ Error occurred: {e}")