Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import streamlit as st
|
|
| 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,6 +19,8 @@ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
|
| 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,7 +35,9 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
|
|
| 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,10 +49,12 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
|
|
| 46 |
delta = parsed.get("choices", [{}])[0].get("delta", {})
|
| 47 |
token = delta.get("content", "")
|
| 48 |
if token:
|
| 49 |
-
|
|
|
|
| 50 |
except json.JSONDecodeError:
|
| 51 |
pass
|
| 52 |
|
|
|
|
| 53 |
def get_sources(topic, domains=None):
|
| 54 |
query = topic
|
| 55 |
if domains:
|
|
@@ -139,7 +144,6 @@ def generate_download_button(file, label, mime_type):
|
|
| 139 |
</a>
|
| 140 |
"""
|
| 141 |
|
| 142 |
-
# --- Streamlit UI ---
|
| 143 |
st.set_page_config("Deep Research Bot", layout="centered")
|
| 144 |
st.markdown("""
|
| 145 |
<style>
|
|
@@ -186,15 +190,6 @@ if research_button and topic:
|
|
| 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]
|
|
@@ -203,10 +198,8 @@ if research_button and topic:
|
|
| 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 |
-
|
| 207 |
Sources:
|
| 208 |
{combined_text}
|
| 209 |
-
|
| 210 |
APA Citations:
|
| 211 |
{chr(10).join(citations)}
|
| 212 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import datetime
|
| 5 |
import time
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from tavily import TavilyClient
|
| 8 |
import feedparser
|
|
|
|
| 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 |
"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 |
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 |
</a>
|
| 145 |
"""
|
| 146 |
|
|
|
|
| 147 |
st.set_page_config("Deep Research Bot", layout="centered")
|
| 148 |
st.markdown("""
|
| 149 |
<style>
|
|
|
|
| 190 |
raise ValueError("Unable to fetch any sources. Please try again later.")
|
| 191 |
|
| 192 |
merged = merge_duplicates(all_sources)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
citations = [generate_apa_citation(m['title'], m['url'], m['source']) for m in merged]
|
| 194 |
combined_text = "\n\n".join(
|
| 195 |
[f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}..." for m in merged]
|
|
|
|
| 198 |
|
| 199 |
prompt = f"""
|
| 200 |
You are an expert assistant. Based on the following sources, write a {report_type.lower()} in a {tone.lower()} tone on the topic: {topic}.
|
|
|
|
| 201 |
Sources:
|
| 202 |
{combined_text}
|
|
|
|
| 203 |
APA Citations:
|
| 204 |
{chr(10).join(citations)}
|
| 205 |
"""
|