Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,21 +32,10 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
|
|
| 32 |
"max_tokens": max_tokens,
|
| 33 |
"temperature": temperature
|
| 34 |
}
|
| 35 |
-
|
| 36 |
response = requests.post(url, headers=headers, json=data)
|
| 37 |
-
|
| 38 |
-
try:
|
| 39 |
-
result = response.json()
|
| 40 |
-
except Exception:
|
| 41 |
-
raise RuntimeError(f"LLM API returned invalid JSON: {response.text}")
|
| 42 |
-
|
| 43 |
if response.status_code != 200:
|
| 44 |
-
|
| 45 |
-
raise RuntimeError(f"LLM API error: {error_msg}")
|
| 46 |
-
|
| 47 |
-
if "choices" not in result:
|
| 48 |
-
raise RuntimeError(f"LLM API returned unexpected response format. Full response:\n{result}")
|
| 49 |
-
|
| 50 |
return result["choices"][0]["message"]["content"]
|
| 51 |
|
| 52 |
def get_sources(topic, domains=None):
|
|
@@ -54,19 +43,15 @@ def get_sources(topic, domains=None):
|
|
| 54 |
if domains:
|
| 55 |
domain_filters = [d.strip() for d in domains.split(",") if d.strip()]
|
| 56 |
query += " site:" + " OR site:".join(domain_filters)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
return sources
|
| 67 |
-
except Exception as e:
|
| 68 |
-
st.error(f"Error retrieving sources: {e}")
|
| 69 |
-
return []
|
| 70 |
|
| 71 |
def get_arxiv_papers(query):
|
| 72 |
from urllib.parse import quote_plus
|
|
@@ -136,14 +121,10 @@ def generate_download_button(file, label, mime_type):
|
|
| 136 |
b64 = base64.b64encode(file.read()).decode()
|
| 137 |
return f"""
|
| 138 |
<a href=\"data:{mime_type};base64,{b64}\" download=\"{label}\">
|
| 139 |
-
\ud83d\
|
| 140 |
</a>
|
| 141 |
"""
|
| 142 |
|
| 143 |
-
def fetch_related_image(topic):
|
| 144 |
-
search_url = f"https://source.unsplash.com/800x400/?{topic}"
|
| 145 |
-
return search_url
|
| 146 |
-
|
| 147 |
# --- Streamlit UI ---
|
| 148 |
st.set_page_config("Deep Research Bot", layout="wide")
|
| 149 |
|
|
@@ -162,7 +143,7 @@ with st.sidebar:
|
|
| 162 |
])
|
| 163 |
source_type = st.selectbox(" Sources to include", ["Web Only", "Academic Only", "Hybrid"])
|
| 164 |
custom_domains = st.text_input(" Query Domains (Optional)", placeholder="techcrunch.com, forbes.com")
|
| 165 |
-
research_button = st.button("
|
| 166 |
|
| 167 |
st.title(" Research Output")
|
| 168 |
|
|
@@ -194,33 +175,56 @@ if research_button and topic:
|
|
| 194 |
citations.append(generate_apa_citation(m['title'], m['url'], m['source']))
|
| 195 |
|
| 196 |
with st.spinner(" Synthesizing report..."):
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
# Research Topic: {topic}
|
| 199 |
Tone: {tone}
|
| 200 |
-
Type: {report_type}
|
| 201 |
Sources:
|
| 202 |
{combined_text}
|
| 203 |
-
Write
|
| 204 |
1. Introduction
|
| 205 |
2. Research Gap
|
| 206 |
3. Novel Insight
|
| 207 |
-
4.
|
| 208 |
-
5.
|
| 209 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
final_output = call_llm([{"role": "user", "content": prompt}])
|
| 211 |
|
| 212 |
st.markdown(f" {report_type}")
|
| 213 |
st.markdown(final_output, unsafe_allow_html=True)
|
| 214 |
|
| 215 |
-
|
| 216 |
-
image_url = fetch_related_image(topic)
|
| 217 |
-
st.image(image_url, caption="Related Image", use_column_width=True)
|
| 218 |
-
|
| 219 |
-
st.markdown(" Citations (APA Format)")
|
| 220 |
for cite in citations:
|
| 221 |
st.markdown(f"- {cite}")
|
| 222 |
|
| 223 |
-
if
|
| 224 |
with st.spinner(" Preparing PDF and LaTeX..."):
|
| 225 |
pdf_file = generate_pdf(final_output)
|
| 226 |
latex_file = generate_latex(final_output)
|
|
@@ -236,4 +240,4 @@ Write the report in academic markdown with paragraphs (use bullet points only wh
|
|
| 236 |
st.success(" No major overlaps found.")
|
| 237 |
|
| 238 |
except Exception as e:
|
| 239 |
-
st.error(f"Error: {e}")
|
|
|
|
| 32 |
"max_tokens": max_tokens,
|
| 33 |
"temperature": temperature
|
| 34 |
}
|
|
|
|
| 35 |
response = requests.post(url, headers=headers, json=data)
|
| 36 |
+
result = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if response.status_code != 200:
|
| 38 |
+
raise RuntimeError(result.get("error", {}).get("message", "LLM API error"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return result["choices"][0]["message"]["content"]
|
| 40 |
|
| 41 |
def get_sources(topic, domains=None):
|
|
|
|
| 43 |
if domains:
|
| 44 |
domain_filters = [d.strip() for d in domains.split(",") if d.strip()]
|
| 45 |
query += " site:" + " OR site:".join(domain_filters)
|
| 46 |
+
response = tavily.search(query=query, search_depth="advanced", max_results=10)
|
| 47 |
+
sources = []
|
| 48 |
+
for item in response.get("results", []):
|
| 49 |
+
sources.append({
|
| 50 |
+
"title": item.get("title"),
|
| 51 |
+
"url": item.get("url"),
|
| 52 |
+
"snippet": item.get("content", "")
|
| 53 |
+
})
|
| 54 |
+
return sources
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def get_arxiv_papers(query):
|
| 57 |
from urllib.parse import quote_plus
|
|
|
|
| 121 |
b64 = base64.b64encode(file.read()).decode()
|
| 122 |
return f"""
|
| 123 |
<a href=\"data:{mime_type};base64,{b64}\" download=\"{label}\">
|
| 124 |
+
\ud83d\udcbe Download {label}
|
| 125 |
</a>
|
| 126 |
"""
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# --- Streamlit UI ---
|
| 129 |
st.set_page_config("Deep Research Bot", layout="wide")
|
| 130 |
|
|
|
|
| 143 |
])
|
| 144 |
source_type = st.selectbox(" Sources to include", ["Web Only", "Academic Only", "Hybrid"])
|
| 145 |
custom_domains = st.text_input(" Query Domains (Optional)", placeholder="techcrunch.com, forbes.com")
|
| 146 |
+
research_button = st.button("Research")
|
| 147 |
|
| 148 |
st.title(" Research Output")
|
| 149 |
|
|
|
|
| 175 |
citations.append(generate_apa_citation(m['title'], m['url'], m['source']))
|
| 176 |
|
| 177 |
with st.spinner(" Synthesizing report..."):
|
| 178 |
+
if "Summary" in report_type:
|
| 179 |
+
prompt = f"""
|
| 180 |
+
# Topic Overview: {topic}
|
| 181 |
+
Tone: {tone}
|
| 182 |
+
Sources:
|
| 183 |
+
{combined_text}
|
| 184 |
+
Write a brief summary that introduces the topic, key findings, and general importance. Use markdown.
|
| 185 |
+
"""
|
| 186 |
+
elif "Detailed Report" in report_type:
|
| 187 |
+
prompt = f"""
|
| 188 |
# Research Topic: {topic}
|
| 189 |
Tone: {tone}
|
|
|
|
| 190 |
Sources:
|
| 191 |
{combined_text}
|
| 192 |
+
Write a structured report in markdown including:
|
| 193 |
1. Introduction
|
| 194 |
2. Research Gap
|
| 195 |
3. Novel Insight
|
| 196 |
+
4. Path Forward to Bridge the Research Gap
|
| 197 |
+
5. Citations
|
| 198 |
+
"""
|
| 199 |
+
else:
|
| 200 |
+
prompt = f"""
|
| 201 |
+
# Thorough Academic Research Paper
|
| 202 |
+
Topic: {topic}
|
| 203 |
+
Tone: {tone}
|
| 204 |
+
Sources:
|
| 205 |
+
{combined_text}
|
| 206 |
+
Write a detailed research paper in academic markdown with these sections:
|
| 207 |
+
1. Abstract
|
| 208 |
+
2. Introduction
|
| 209 |
+
3. Literature Review
|
| 210 |
+
4. Research Gap
|
| 211 |
+
5. Proposed Methodology or Novel Insight
|
| 212 |
+
6. Applications and Implications
|
| 213 |
+
7. Conclusion
|
| 214 |
+
8. References in APA format
|
| 215 |
+
Also, suggest 1-2 relevant open-license images and include their links.
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
final_output = call_llm([{"role": "user", "content": prompt}])
|
| 219 |
|
| 220 |
st.markdown(f" {report_type}")
|
| 221 |
st.markdown(final_output, unsafe_allow_html=True)
|
| 222 |
|
| 223 |
+
st.markdown("Citations (APA Format)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
for cite in citations:
|
| 225 |
st.markdown(f"- {cite}")
|
| 226 |
|
| 227 |
+
if "Thorough Academic Research" in report_type:
|
| 228 |
with st.spinner(" Preparing PDF and LaTeX..."):
|
| 229 |
pdf_file = generate_pdf(final_output)
|
| 230 |
latex_file = generate_latex(final_output)
|
|
|
|
| 240 |
st.success(" No major overlaps found.")
|
| 241 |
|
| 242 |
except Exception as e:
|
| 243 |
+
st.error(f"Error: {e}")
|