Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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>
|
|
@@ -190,6 +186,15 @@ if research_button and topic:
|
|
| 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]
|
|
@@ -197,9 +202,23 @@ if research_button and topic:
|
|
| 197 |
status.update(label="🧠 Synthesizing final report...")
|
| 198 |
|
| 199 |
prompt = f"""
|
| 200 |
-
You are an expert assistant.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
Sources:
|
| 202 |
{combined_text}
|
|
|
|
| 203 |
APA Citations:
|
| 204 |
{chr(10).join(citations)}
|
| 205 |
"""
|
|
@@ -228,4 +247,4 @@ APA Citations:
|
|
| 228 |
st.success("✅ No major overlaps found.")
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
-
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>
|
|
|
|
| 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]
|
|
|
|
| 202 |
status.update(label="🧠 Synthesizing final report...")
|
| 203 |
|
| 204 |
prompt = f"""
|
| 205 |
+
You are an expert research assistant.
|
| 206 |
+
1. Analyze the following sources for gaps or limitations in current research.
|
| 207 |
+
2. Propose a novel research topic that fills the most promising gap.
|
| 208 |
+
3. Write a full academic-style report on the newly proposed topic using a {tone.lower()} tone.
|
| 209 |
+
|
| 210 |
+
Report Format:
|
| 211 |
+
- **Introduction**: Context and importance of the new topic.
|
| 212 |
+
- **Identified Research Gap**: Summary of missing or underexplored areas.
|
| 213 |
+
- **Proposed Novel Topic**: Explanation of the new idea and its significance.
|
| 214 |
+
- **Methodology**: Outline methods or frameworks to explore the idea.
|
| 215 |
+
- **Comparative Insight**: How the new idea differs/improves over previous works.
|
| 216 |
+
- **Future Work**: Possible expansions or questions.
|
| 217 |
+
- **Citations**: Include APA style citations at the end.
|
| 218 |
+
|
| 219 |
Sources:
|
| 220 |
{combined_text}
|
| 221 |
+
|
| 222 |
APA Citations:
|
| 223 |
{chr(10).join(citations)}
|
| 224 |
"""
|
|
|
|
| 247 |
st.success("✅ No major overlaps found.")
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
+
st.error(f"❌ Error occurred: {e}")
|