Spaces:
Sleeping
Sleeping
Download app.py from Jaamie/SafeSpace_Agent: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/Jaamie/SafeSpace_Agent/resolve/main/app.py
- Command line
-
hf download hf://spaces/Jaamie/SafeSpace_Agent/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Jaamie/SafeSpace_Agent/resolve/main/app.py
3.06 kB
| # -*- coding: utf-8 -*- | |
| """app.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1eFgao8MF2-w0wFEdoXkd6vFRHYANPnrP | |
| """ | |
| import streamlit as st | |
| from utils import agent | |
| import folium | |
| from streamlit_folium import st_folium | |
| from utils import agent, find_clinics | |
| from datetime import datetime | |
| st.set_page_config(page_title="๐ง Mental Health Q&A Agent", layout="centered") | |
| # --- Header --- | |
| st.markdown("## ๐ฌ Ask Your Mental Health Assistant") | |
| st.markdown("An AI-powered assistant to support your mental health questions using RAG + LLM") | |
| # --- Input Box with Placeholder --- | |
| user_question = st.text_input( | |
| "Ask a question:", | |
| placeholder="e.g. Why do I feel anxious before exams?" | |
| ) | |
| def render_clinic_map(clinics): | |
| if not clinics: | |
| st.warning("No clinics found to display.") | |
| return | |
| map_center = [clinics[0]["lat"], clinics[0]["lng"]] | |
| m = folium.Map(location=map_center, zoom_start=13) | |
| for clinic in clinics: | |
| folium.Marker( | |
| location=[clinic["lat"], clinic["lng"]], | |
| popup=f"<b>{clinic['name']}</b><br>{clinic['address']}<br>{clinic['phone']}", | |
| tooltip=clinic["name"] | |
| ).add_to(m) | |
| st.markdown("### ๐บ๏ธ Map of Nearby Clinics") | |
| st_folium(m, width=700, height=500) | |
| # --- Session history setup --- | |
| if "history" not in st.session_state: | |
| st.session_state.history = [] | |
| # --- Processing user input --- | |
| # if user_question: | |
| # with st.spinner("๐ค Thinking..."): | |
| # try: | |
| # response = agent.run(user_question) | |
| # # Save Q&A to session history | |
| # st.session_state.history.append((user_question, response)) | |
| # # Display answer | |
| # st.markdown("### ๐ง Response:") | |
| # st.success(response) | |
| # st.caption(f"Answered on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| # except Exception as e: | |
| # st.error(f"An error occurred: {e}") | |
| if user_question: | |
| with st.spinner("๐ค Thinking..."): | |
| try: | |
| response = agent.run(user_question) | |
| # ๐ Check if it's a clinic-related query | |
| if "clinic" in user_question.lower() or "hospital" in user_question.lower(): | |
| clinics = find_clinics(user_question) | |
| render_clinic_map(clinics) | |
| # Save Q&A to history | |
| st.session_state.history.append((user_question, response)) | |
| # Display text response | |
| st.markdown("### ๐ง Response:") | |
| st.success(response) | |
| st.caption(f"Answered on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| # --- Show Previous Q&A --- | |
| if st.session_state.history: | |
| with st.expander("๐ View Previous Questions"): | |
| for i, (q, a) in enumerate(reversed(st.session_state.history), 1): | |
| st.markdown(f"**Q{i}:** {q}") | |
| st.markdown(f"**A{i}:** {a}") | |
| st.markdown("---") |