Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -99,38 +99,29 @@ def merge_duplicates(entries):
|
|
| 99 |
return unique
|
| 100 |
|
| 101 |
def generate_pdf(text):
|
| 102 |
-
def clean_utf8(s):
|
| 103 |
-
return s.encode("utf-8", "ignore").decode("utf-8")
|
| 104 |
-
|
| 105 |
pdf = FPDF()
|
| 106 |
pdf.add_page()
|
| 107 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 108 |
pdf.set_font("Arial", size=12)
|
| 109 |
for line in text.split('\n'):
|
| 110 |
-
pdf.multi_cell(0, 10,
|
| 111 |
pdf_output = BytesIO()
|
| 112 |
pdf.output(pdf_output)
|
| 113 |
pdf_output.seek(0)
|
| 114 |
return pdf_output
|
| 115 |
|
| 116 |
-
|
| 117 |
def generate_latex(text):
|
| 118 |
-
def clean_utf8(s):
|
| 119 |
-
return s.encode("utf-8", "ignore").decode("utf-8")
|
| 120 |
-
|
| 121 |
latex = "\\documentclass{article}\n\\usepackage{hyperref}\n\\begin{document}\n"
|
| 122 |
for line in text.split('\n'):
|
| 123 |
-
|
| 124 |
-
latex += safe_line + "\\\\\n"
|
| 125 |
latex += "\\end{document}"
|
| 126 |
return BytesIO(latex.encode("utf-8"))
|
| 127 |
|
| 128 |
-
|
| 129 |
def generate_download_button(file, label, mime_type):
|
| 130 |
b64 = base64.b64encode(file.read()).decode()
|
| 131 |
return f"""
|
| 132 |
-
<a href
|
| 133 |
-
|
| 134 |
</a>
|
| 135 |
"""
|
| 136 |
|
|
@@ -138,28 +129,27 @@ def generate_download_button(file, label, mime_type):
|
|
| 138 |
st.set_page_config("Deep Research Bot", layout="wide")
|
| 139 |
|
| 140 |
with st.sidebar:
|
| 141 |
-
st.title("
|
| 142 |
-
topic = st.text_input("
|
| 143 |
-
report_type = st.selectbox("
|
| 144 |
"Summary - Short and fast (~2 min)",
|
| 145 |
"Detailed Report (~5 min)",
|
| 146 |
"Thorough Academic Research (~10 min)"
|
| 147 |
])
|
| 148 |
-
tone = st.selectbox("
|
| 149 |
"Objective - Impartial and unbiased presentation of facts and findings",
|
| 150 |
"Persuasive - Advocating a specific point of view",
|
| 151 |
"Narrative - Storytelling tone for layperson readers"
|
| 152 |
])
|
| 153 |
-
source_type = st.selectbox("
|
| 154 |
-
custom_domains = st.text_input("
|
| 155 |
research_button = st.button("Research")
|
| 156 |
|
| 157 |
st.title("π Research Output")
|
| 158 |
|
| 159 |
-
|
| 160 |
if research_button and topic:
|
| 161 |
try:
|
| 162 |
-
with st.status("
|
| 163 |
st.info("Fetching from sources...")
|
| 164 |
|
| 165 |
all_sources = []
|
|
@@ -184,58 +174,31 @@ if research_button and topic:
|
|
| 184 |
combined_text += f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}...\n\n"
|
| 185 |
citations.append(generate_apa_citation(m['title'], m['url'], m['source']))
|
| 186 |
|
| 187 |
-
with st.spinner("
|
| 188 |
-
|
| 189 |
-
prompt = f"""
|
| 190 |
-
# Topic Overview: {topic}
|
| 191 |
-
Tone: {tone}
|
| 192 |
-
Sources:
|
| 193 |
-
{combined_text}
|
| 194 |
-
Write a brief summary that introduces the topic, key findings, and general importance. Use markdown.
|
| 195 |
-
"""
|
| 196 |
-
elif "Detailed Report" in report_type:
|
| 197 |
-
prompt = f"""
|
| 198 |
# Research Topic: {topic}
|
| 199 |
Tone: {tone}
|
|
|
|
| 200 |
Sources:
|
| 201 |
{combined_text}
|
| 202 |
-
Write
|
| 203 |
1. Introduction
|
| 204 |
2. Research Gap
|
| 205 |
3. Novel Insight
|
| 206 |
-
4.
|
| 207 |
-
5.
|
| 208 |
-
|
| 209 |
-
else:
|
| 210 |
-
prompt = f"""
|
| 211 |
-
# Thorough Academic Research Paper
|
| 212 |
-
Topic: {topic}
|
| 213 |
-
Tone: {tone}
|
| 214 |
-
Sources:
|
| 215 |
-
{combined_text}
|
| 216 |
-
Write a detailed research paper in academic markdown with these sections:
|
| 217 |
-
1. Abstract
|
| 218 |
-
2. Introduction
|
| 219 |
-
3. Literature Review
|
| 220 |
-
4. Research Gap
|
| 221 |
-
5. Proposed Methodology or Novel Insight
|
| 222 |
-
6. Applications and Implications
|
| 223 |
-
7. Conclusion
|
| 224 |
-
8. References in APA format
|
| 225 |
-
Also, suggest 1-2 relevant open-license images and include their links.
|
| 226 |
-
"""
|
| 227 |
-
|
| 228 |
final_output = call_llm([{"role": "user", "content": prompt}])
|
| 229 |
|
| 230 |
-
st.markdown(f"###
|
| 231 |
st.markdown(final_output, unsafe_allow_html=True)
|
| 232 |
|
| 233 |
-
st.markdown("###
|
| 234 |
for cite in citations:
|
| 235 |
st.markdown(f"- {cite}")
|
| 236 |
|
| 237 |
-
if "Thorough Academic Research
|
| 238 |
-
with st.spinner("
|
| 239 |
pdf_file = generate_pdf(final_output)
|
| 240 |
latex_file = generate_latex(final_output)
|
| 241 |
st.markdown(generate_download_button(pdf_file, "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
|
@@ -243,11 +206,11 @@ Also, suggest 1-2 relevant open-license images and include their links.
|
|
| 243 |
|
| 244 |
overlaps = check_plagiarism(final_output, topic)
|
| 245 |
if overlaps:
|
| 246 |
-
st.warning("
|
| 247 |
for hit in overlaps:
|
| 248 |
st.markdown(f"- [{hit['title']}]({hit['url']})")
|
| 249 |
else:
|
| 250 |
-
st.success("
|
| 251 |
|
| 252 |
except Exception as e:
|
| 253 |
-
st.error(f"Error: {e}")
|
|
|
|
| 99 |
return unique
|
| 100 |
|
| 101 |
def generate_pdf(text):
|
|
|
|
|
|
|
|
|
|
| 102 |
pdf = FPDF()
|
| 103 |
pdf.add_page()
|
| 104 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 105 |
pdf.set_font("Arial", size=12)
|
| 106 |
for line in text.split('\n'):
|
| 107 |
+
pdf.multi_cell(0, 10, line)
|
| 108 |
pdf_output = BytesIO()
|
| 109 |
pdf.output(pdf_output)
|
| 110 |
pdf_output.seek(0)
|
| 111 |
return pdf_output
|
| 112 |
|
|
|
|
| 113 |
def generate_latex(text):
|
|
|
|
|
|
|
|
|
|
| 114 |
latex = "\\documentclass{article}\n\\usepackage{hyperref}\n\\begin{document}\n"
|
| 115 |
for line in text.split('\n'):
|
| 116 |
+
latex += line.replace('_', '\\_') + "\\\\\n"
|
|
|
|
| 117 |
latex += "\\end{document}"
|
| 118 |
return BytesIO(latex.encode("utf-8"))
|
| 119 |
|
|
|
|
| 120 |
def generate_download_button(file, label, mime_type):
|
| 121 |
b64 = base64.b64encode(file.read()).decode()
|
| 122 |
return f"""
|
| 123 |
+
<a href="data:{mime_type};base64,{b64}" download="{label}">
|
| 124 |
+
π₯ Download {label}
|
| 125 |
</a>
|
| 126 |
"""
|
| 127 |
|
|
|
|
| 129 |
st.set_page_config("Deep Research Bot", layout="wide")
|
| 130 |
|
| 131 |
with st.sidebar:
|
| 132 |
+
st.title("π§ Deep Research Assistant")
|
| 133 |
+
topic = st.text_input("π‘ Topic to research")
|
| 134 |
+
report_type = st.selectbox("π Type of report", [
|
| 135 |
"Summary - Short and fast (~2 min)",
|
| 136 |
"Detailed Report (~5 min)",
|
| 137 |
"Thorough Academic Research (~10 min)"
|
| 138 |
])
|
| 139 |
+
tone = st.selectbox("π― Tone of the report", [
|
| 140 |
"Objective - Impartial and unbiased presentation of facts and findings",
|
| 141 |
"Persuasive - Advocating a specific point of view",
|
| 142 |
"Narrative - Storytelling tone for layperson readers"
|
| 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 |
|
|
|
|
| 150 |
if research_button and topic:
|
| 151 |
try:
|
| 152 |
+
with st.status("π Gathering data..."):
|
| 153 |
st.info("Fetching from sources...")
|
| 154 |
|
| 155 |
all_sources = []
|
|
|
|
| 174 |
combined_text += f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}...\n\n"
|
| 175 |
citations.append(generate_apa_citation(m['title'], m['url'], m['source']))
|
| 176 |
|
| 177 |
+
with st.spinner("βοΈ Synthesizing report..."):
|
| 178 |
+
prompt = f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# Research Topic: {topic}
|
| 180 |
Tone: {tone}
|
| 181 |
+
Type: {report_type}
|
| 182 |
Sources:
|
| 183 |
{combined_text}
|
| 184 |
+
Write the report in academic markdown with paragraphs (use bullet points only when necessary). Include:
|
| 185 |
1. Introduction
|
| 186 |
2. Research Gap
|
| 187 |
3. Novel Insight
|
| 188 |
+
4. Application
|
| 189 |
+
5. Full Academic Writeup if Thorough Report
|
| 190 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
final_output = call_llm([{"role": "user", "content": prompt}])
|
| 192 |
|
| 193 |
+
st.markdown(f"### π {report_type}")
|
| 194 |
st.markdown(final_output, unsafe_allow_html=True)
|
| 195 |
|
| 196 |
+
st.markdown("### π Citations (APA Format)")
|
| 197 |
for cite in citations:
|
| 198 |
st.markdown(f"- {cite}")
|
| 199 |
|
| 200 |
+
if report_type == "Thorough Academic Research (~10 min)":
|
| 201 |
+
with st.spinner("π¦ Preparing PDF and LaTeX..."):
|
| 202 |
pdf_file = generate_pdf(final_output)
|
| 203 |
latex_file = generate_latex(final_output)
|
| 204 |
st.markdown(generate_download_button(pdf_file, "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
|
|
|
| 206 |
|
| 207 |
overlaps = check_plagiarism(final_output, topic)
|
| 208 |
if overlaps:
|
| 209 |
+
st.warning("β οΈ Potential overlaps detected:")
|
| 210 |
for hit in overlaps:
|
| 211 |
st.markdown(f"- [{hit['title']}]({hit['url']})")
|
| 212 |
else:
|
| 213 |
+
st.success("β
No major overlaps found.")
|
| 214 |
|
| 215 |
except Exception as e:
|
| 216 |
+
st.error(f"Error: {e}")
|