Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -189,28 +189,18 @@ def generate_download_button(file, label, mime_type):
|
|
| 189 |
|
| 190 |
# --- Streamlit UI ---
|
| 191 |
st.set_page_config("Deep Research Assistant", layout="centered")
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
| 193 |
if "follow_up_input" not in st.session_state:
|
| 194 |
st.session_state["follow_up_input"] = ""
|
| 195 |
if "methodology_recommended" not in st.session_state:
|
| 196 |
st.session_state["methodology_recommended"] = False
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
|
| 199 |
-
# Mermaid.js for mind map rendering
|
| 200 |
-
st.markdown("""
|
| 201 |
-
<script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
|
| 202 |
-
<script>
|
| 203 |
-
mermaid.initialize({ startOnLoad: true });
|
| 204 |
-
</script>
|
| 205 |
-
""", unsafe_allow_html=True)
|
| 206 |
-
|
| 207 |
-
st.markdown("""
|
| 208 |
-
<style>
|
| 209 |
-
.stApp { background-color: #0f172a; color: white; }
|
| 210 |
-
h1, h2, h3 { color: #facc15; }
|
| 211 |
-
</style>
|
| 212 |
-
""", unsafe_allow_html=True)
|
| 213 |
-
|
| 214 |
with st.sidebar:
|
| 215 |
st.title("π§ Deep Research Assistant")
|
| 216 |
topic = st.text_input("π Enter your research topic")
|
|
@@ -222,6 +212,7 @@ with st.sidebar:
|
|
| 222 |
|
| 223 |
st.title("π Research Output")
|
| 224 |
|
|
|
|
| 225 |
if research_button and topic:
|
| 226 |
try:
|
| 227 |
with st.status("π Gathering and analyzing sources...") as status:
|
|
@@ -284,15 +275,15 @@ APA Citations:
|
|
| 284 |
full_output += chunk
|
| 285 |
output_placeholder.markdown(full_output, unsafe_allow_html=True)
|
| 286 |
|
| 287 |
-
# Save output for follow-up
|
| 288 |
st.session_state["last_report"] = full_output
|
| 289 |
|
| 290 |
-
#
|
| 291 |
st.subheader("π Downloads")
|
| 292 |
st.markdown(generate_download_button(generate_pdf(full_output), "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
| 293 |
st.markdown(generate_download_button(generate_latex(full_output), "Research_Report.tex", "application/x-latex"), unsafe_allow_html=True)
|
| 294 |
|
| 295 |
-
#
|
| 296 |
st.subheader("π Plagiarism Check")
|
| 297 |
overlaps = check_plagiarism(full_output, topic)
|
| 298 |
if overlaps:
|
|
@@ -302,50 +293,52 @@ APA Citations:
|
|
| 302 |
else:
|
| 303 |
st.success("β
No major overlaps found.")
|
| 304 |
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
st.session_state["follow_up_input"] = st.text_input(
|
| 308 |
-
"Ask a question about the report:", value=st.session_state["follow_up_input"]
|
| 309 |
-
)
|
| 310 |
-
|
| 311 |
-
if st.button("π Submit Follow-Up") and st.session_state["follow_up_input"]:
|
| 312 |
-
if st.session_state["last_report"]:
|
| 313 |
-
try:
|
| 314 |
-
follow_prompt = [
|
| 315 |
-
{"role": "system", "content": "You are a helpful academic assistant."},
|
| 316 |
-
{"role": "user", "content": st.session_state["last_report"]},
|
| 317 |
-
{"role": "user", "content": st.session_state["follow_up_input"]}
|
| 318 |
-
]
|
| 319 |
-
follow_output = ""
|
| 320 |
-
follow_box = st.empty()
|
| 321 |
-
for chunk in call_llm(follow_prompt):
|
| 322 |
-
follow_output += chunk
|
| 323 |
-
follow_box.markdown(follow_output, unsafe_allow_html=True)
|
| 324 |
-
except Exception as e:
|
| 325 |
-
st.error(f"β Follow-up failed: {e}")
|
| 326 |
-
else:
|
| 327 |
-
st.warning("β οΈ Generate the research report first.")
|
| 328 |
-
|
| 329 |
-
st.subheader("π§ͺ Methodology Recommender")
|
| 330 |
-
|
| 331 |
-
if st.button("π§ Suggest Research Methodologies"):
|
| 332 |
-
if st.session_state["last_report"]:
|
| 333 |
-
try:
|
| 334 |
-
method_prompt = [
|
| 335 |
-
{"role": "system", "content": "You are a research advisor. Based on the following research report, suggest suitable research methodologies (quantitative, qualitative, ML/AI techniques, etc.). Be concise and give bullet-point suggestions."},
|
| 336 |
-
{"role": "user", "content": st.session_state["last_report"]}
|
| 337 |
-
]
|
| 338 |
-
method_output = ""
|
| 339 |
-
method_box = st.empty()
|
| 340 |
-
for chunk in call_llm(method_prompt):
|
| 341 |
-
method_output += chunk
|
| 342 |
-
method_box.markdown(method_output, unsafe_allow_html=True)
|
| 343 |
-
except Exception as e:
|
| 344 |
-
st.error(f"β Methodology suggestion failed: {e}")
|
| 345 |
-
else:
|
| 346 |
-
st.warning("β οΈ Generate the research report first.")
|
| 347 |
|
|
|
|
|
|
|
|
|
|
| 348 |
|
|
|
|
| 349 |
|
| 350 |
-
|
| 351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# --- Streamlit UI ---
|
| 191 |
st.set_page_config("Deep Research Assistant", layout="centered")
|
| 192 |
+
|
| 193 |
+
# Initialize session state
|
| 194 |
+
if "last_report" not in st.session_state:
|
| 195 |
+
st.session_state["last_report"] = ""
|
| 196 |
if "follow_up_input" not in st.session_state:
|
| 197 |
st.session_state["follow_up_input"] = ""
|
| 198 |
if "methodology_recommended" not in st.session_state:
|
| 199 |
st.session_state["methodology_recommended"] = False
|
| 200 |
+
if "chat_history" not in st.session_state:
|
| 201 |
+
st.session_state["chat_history"] = []
|
| 202 |
|
| 203 |
+
# --- Sidebar Inputs ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
with st.sidebar:
|
| 205 |
st.title("π§ Deep Research Assistant")
|
| 206 |
topic = st.text_input("π Enter your research topic")
|
|
|
|
| 212 |
|
| 213 |
st.title("π Research Output")
|
| 214 |
|
| 215 |
+
# --- Main Report Generation ---
|
| 216 |
if research_button and topic:
|
| 217 |
try:
|
| 218 |
with st.status("π Gathering and analyzing sources...") as status:
|
|
|
|
| 275 |
full_output += chunk
|
| 276 |
output_placeholder.markdown(full_output, unsafe_allow_html=True)
|
| 277 |
|
| 278 |
+
# Save output for follow-up and methodology
|
| 279 |
st.session_state["last_report"] = full_output
|
| 280 |
|
| 281 |
+
# --- Downloads ---
|
| 282 |
st.subheader("π Downloads")
|
| 283 |
st.markdown(generate_download_button(generate_pdf(full_output), "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
| 284 |
st.markdown(generate_download_button(generate_latex(full_output), "Research_Report.tex", "application/x-latex"), unsafe_allow_html=True)
|
| 285 |
|
| 286 |
+
# --- Plagiarism Check ---
|
| 287 |
st.subheader("π Plagiarism Check")
|
| 288 |
overlaps = check_plagiarism(full_output, topic)
|
| 289 |
if overlaps:
|
|
|
|
| 293 |
else:
|
| 294 |
st.success("β
No major overlaps found.")
|
| 295 |
|
| 296 |
+
except Exception as e:
|
| 297 |
+
st.error(f"β Error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
+
# --- Follow-up Q&A (always visible) ---
|
| 300 |
+
st.divider()
|
| 301 |
+
st.subheader("π¬ Follow-up Q&A")
|
| 302 |
|
| 303 |
+
followup = st.text_input("Ask a follow-up question:", key="follow_up_input")
|
| 304 |
|
| 305 |
+
if st.button("Ask"):
|
| 306 |
+
if followup:
|
| 307 |
+
try:
|
| 308 |
+
chat = st.session_state.chat_history + [{"role": "user", "content": followup}]
|
| 309 |
+
response = ""
|
| 310 |
+
for chunk in call_llm(chat, max_tokens=1500):
|
| 311 |
+
response += chunk
|
| 312 |
+
st.session_state.chat_history.append({"role": "user", "content": followup})
|
| 313 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 314 |
+
st.markdown(response)
|
| 315 |
+
except Exception as e:
|
| 316 |
+
st.error(f"Follow-up error: {e}")
|
| 317 |
+
|
| 318 |
+
# --- Chat history display (optional) ---
|
| 319 |
+
with st.expander("π View Full Chat History", expanded=False):
|
| 320 |
+
for msg in st.session_state.chat_history:
|
| 321 |
+
role = msg["role"]
|
| 322 |
+
prefix = "π€ You" if role == "user" else "π€ Assistant"
|
| 323 |
+
st.markdown(f"**{prefix}:** {msg['content']}")
|
| 324 |
+
|
| 325 |
+
# --- Methodology Recommender ---
|
| 326 |
+
st.divider()
|
| 327 |
+
st.subheader("π§ͺ Methodology Recommender")
|
| 328 |
+
|
| 329 |
+
if st.button("π§ Suggest Research Methodologies"):
|
| 330 |
+
if st.session_state["last_report"]:
|
| 331 |
+
try:
|
| 332 |
+
method_prompt = [
|
| 333 |
+
{"role": "system", "content": "You are a research advisor. Based on the following research report, suggest suitable research methodologies (quantitative, qualitative, ML/AI techniques, etc.). Be concise and give bullet-point suggestions."},
|
| 334 |
+
{"role": "user", "content": st.session_state["last_report"]}
|
| 335 |
+
]
|
| 336 |
+
method_output = ""
|
| 337 |
+
method_box = st.empty()
|
| 338 |
+
for chunk in call_llm(method_prompt):
|
| 339 |
+
method_output += chunk
|
| 340 |
+
method_box.markdown(method_output, unsafe_allow_html=True)
|
| 341 |
+
except Exception as e:
|
| 342 |
+
st.error(f"β Methodology suggestion failed: {e}")
|
| 343 |
+
else:
|
| 344 |
+
st.warning("β οΈ Generate the research report first.")
|