|
import sys |
|
import os |
|
import json |
|
import logging |
|
import uuid |
|
import streamlit as st |
|
|
|
|
|
sys.path.append(os.path.abspath("src")) |
|
|
|
|
|
from src.chatbot import generate_response_with_history, COUNTRY_HELPLINES, get_helpline_for_country |
|
|
|
|
|
logging.basicConfig(filename="safeguard_logs.txt", level=logging.INFO) |
|
|
|
|
|
METRICS_FILE = "user_metrics.json" |
|
|
|
def load_metrics(): |
|
"""Load existing user metrics or initialize them.""" |
|
if os.path.exists(METRICS_FILE): |
|
with open(METRICS_FILE, "r") as f: |
|
return json.load(f) |
|
return {"sessions": [], "feedback": {"thumbs_up": 0, "thumbs_down": 0}, "harmful_content": []} |
|
|
|
def save_metrics(metrics): |
|
"""Save updated user metrics to file.""" |
|
with open(METRICS_FILE, "w") as f: |
|
json.dump(metrics, f, indent=4) |
|
|
|
|
|
if "metrics" not in st.session_state: |
|
st.session_state.metrics = load_metrics() |
|
|
|
if "session_id" not in st.session_state: |
|
st.session_state.session_id = str(uuid.uuid4())[:8] |
|
|
|
|
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = [] |
|
|
|
|
|
st.title("Fine-Tuned GPT Chatbot π€") |
|
|
|
|
|
st.markdown( |
|
"**Disclaimer:** This chatbot is a prototype and is **not a substitute** for professional mental health advice. " |
|
"If you are in crisis, please reach out to a professional or a mental health helpline." |
|
) |
|
|
|
|
|
st.subheader("Select Your Country for Helpline Information:") |
|
if "selected_country" not in st.session_state: |
|
st.session_state.selected_country = "Other" |
|
|
|
st.session_state.selected_country = st.selectbox( |
|
"Choose your country:", options=list(COUNTRY_HELPLINES.keys()), index=0 |
|
) |
|
|
|
|
|
user_input = st.text_input("Ask me anything:") |
|
|
|
if st.button("Send"): |
|
|
|
if user_input.strip(): |
|
|
|
st.session_state.chat_history.append({"role": "user", "content": user_input}) |
|
|
|
|
|
response = generate_response_with_history( |
|
st.session_state.chat_history, |
|
st.session_state.selected_country |
|
) |
|
|
|
|
|
st.session_state.chat_history.append({"role": "bot", "content": response}) |
|
|
|
|
|
for turn in st.session_state.chat_history: |
|
if turn["role"] == "user": |
|
st.write(f"**You:** {turn['content']}") |
|
else: |
|
st.write(f"**Bot:** {turn['content']}") |
|
|
|
|
|
st.markdown( |
|
f"π **Helpline for {st.session_state.selected_country}:** " |
|
f"{get_helpline_for_country(st.session_state.selected_country)}" |
|
) |
|
|
|
else: |
|
st.warning("Please enter a message.") |
|
|
|
|
|
st.markdown("---") |
|
st.subheader("Feedback") |
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
if st.button("π Thumbs Up"): |
|
st.session_state.metrics["feedback"]["thumbs_up"] += 1 |
|
save_metrics(st.session_state.metrics) |
|
st.success("Thank you for your feedback! π") |
|
|
|
with col2: |
|
if st.button("π Thumbs Down"): |
|
st.session_state.metrics["feedback"]["thumbs_down"] += 1 |
|
save_metrics(st.session_state.metrics) |
|
st.error("We're sorry to hear that. We'll try to improve! π") |
|
|
|
|
|
total_feedback = ( |
|
st.session_state.metrics["feedback"]["thumbs_up"] |
|
+ st.session_state.metrics["feedback"]["thumbs_down"] |
|
) |
|
if total_feedback > 0: |
|
thumbs_up_percentage = ( |
|
st.session_state.metrics["feedback"]["thumbs_up"] / total_feedback |
|
) * 100 |
|
st.markdown("---") |
|
st.subheader("User Experience") |
|
st.write(f"Positive Feedback: {thumbs_up_percentage:.2f}%") |
|
st.progress(thumbs_up_percentage / 100) |
|
else: |
|
st.markdown("---") |
|
st.subheader("User Experience") |
|
st.write("No feedback yet. Be the first to share your experience!") |
|
|
|
|
|
st.markdown("---") |
|
st.write(f"Session ID: {st.session_state.session_id}") |
|
|