Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,9 @@ import json
|
|
4 |
import logging
|
5 |
import uuid
|
6 |
import streamlit as st
|
|
|
7 |
sys.path.append(os.path.abspath("src"))
|
8 |
-
from src.chatbot import generate_response
|
9 |
|
10 |
# Set up logging for safeguard triggers
|
11 |
logging.basicConfig(filename="safeguard_logs.txt", level=logging.INFO)
|
@@ -13,66 +14,70 @@ logging.basicConfig(filename="safeguard_logs.txt", level=logging.INFO)
|
|
13 |
# Persistent storage for metrics
|
14 |
METRICS_FILE = "user_metrics.json"
|
15 |
|
16 |
-
# Load existing metrics or initialize an empty structure
|
17 |
def load_metrics():
|
|
|
18 |
if os.path.exists(METRICS_FILE):
|
19 |
with open(METRICS_FILE, "r") as f:
|
20 |
return json.load(f)
|
21 |
return {"sessions": [], "feedback": {"thumbs_up": 0, "thumbs_down": 0}, "harmful_content": []}
|
22 |
|
23 |
-
# Save metrics to file
|
24 |
def save_metrics(metrics):
|
|
|
25 |
with open(METRICS_FILE, "w") as f:
|
26 |
json.dump(metrics, f, indent=4)
|
27 |
|
28 |
-
# Initialize
|
29 |
if "metrics" not in st.session_state:
|
30 |
st.session_state.metrics = load_metrics()
|
31 |
|
32 |
-
# Initialize session ID
|
33 |
if "session_id" not in st.session_state:
|
34 |
-
st.session_state.session_id = str(uuid.uuid4())[:8] # Short unique ID
|
35 |
|
36 |
-
# Streamlit
|
37 |
st.title("Fine-Tuned GPT Chatbot π€")
|
38 |
|
39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if "chat_history" not in st.session_state:
|
41 |
st.session_state.chat_history = []
|
42 |
|
43 |
-
# User
|
44 |
user_input = st.text_input("Ask me anything:")
|
45 |
|
46 |
if st.button("Send"):
|
47 |
-
if user_input:
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
)
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
# Store conversation
|
68 |
-
st.session_state.chat_history.append(f"You: {user_input}")
|
69 |
-
st.session_state.chat_history.append(f"Bot: {response}")
|
70 |
-
|
71 |
-
# Display chat history
|
72 |
-
for msg in st.session_state.chat_history:
|
73 |
-
st.write(msg)
|
74 |
-
|
75 |
-
# Feedback system
|
76 |
st.markdown("---")
|
77 |
st.subheader("Feedback")
|
78 |
col1, col2 = st.columns(2)
|
@@ -89,7 +94,7 @@ with col2:
|
|
89 |
save_metrics(st.session_state.metrics)
|
90 |
st.error("We're sorry to hear that. We'll try to improve! π")
|
91 |
|
92 |
-
# User
|
93 |
total_feedback = (
|
94 |
st.session_state.metrics["feedback"]["thumbs_up"] + st.session_state.metrics["feedback"]["thumbs_down"]
|
95 |
)
|
@@ -106,6 +111,6 @@ else:
|
|
106 |
st.subheader("User Experience")
|
107 |
st.write("No feedback yet. Be the first to share your experience!")
|
108 |
|
109 |
-
#
|
110 |
st.markdown("---")
|
111 |
-
st.write(f"Session ID: {st.session_state.session_id}")
|
|
|
4 |
import logging
|
5 |
import uuid
|
6 |
import streamlit as st
|
7 |
+
|
8 |
sys.path.append(os.path.abspath("src"))
|
9 |
+
from src.chatbot import generate_response, COUNTRY_HELPLINES, get_helpline_for_country
|
10 |
|
11 |
# Set up logging for safeguard triggers
|
12 |
logging.basicConfig(filename="safeguard_logs.txt", level=logging.INFO)
|
|
|
14 |
# Persistent storage for metrics
|
15 |
METRICS_FILE = "user_metrics.json"
|
16 |
|
|
|
17 |
def load_metrics():
|
18 |
+
"""Load existing user metrics or initialize them."""
|
19 |
if os.path.exists(METRICS_FILE):
|
20 |
with open(METRICS_FILE, "r") as f:
|
21 |
return json.load(f)
|
22 |
return {"sessions": [], "feedback": {"thumbs_up": 0, "thumbs_down": 0}, "harmful_content": []}
|
23 |
|
|
|
24 |
def save_metrics(metrics):
|
25 |
+
"""Save updated user metrics to file."""
|
26 |
with open(METRICS_FILE, "w") as f:
|
27 |
json.dump(metrics, f, indent=4)
|
28 |
|
29 |
+
# Initialize session
|
30 |
if "metrics" not in st.session_state:
|
31 |
st.session_state.metrics = load_metrics()
|
32 |
|
|
|
33 |
if "session_id" not in st.session_state:
|
34 |
+
st.session_state.session_id = str(uuid.uuid4())[:8] # Short unique session ID
|
35 |
|
36 |
+
# Streamlit UI
|
37 |
st.title("Fine-Tuned GPT Chatbot π€")
|
38 |
|
39 |
+
# --- Disclaimer ---
|
40 |
+
st.markdown(
|
41 |
+
"**Disclaimer:** This chatbot is a prototype and is **not a substitute** for professional mental health advice. "
|
42 |
+
"If you are in crisis, please reach out to a professional or a mental health helpline."
|
43 |
+
)
|
44 |
+
|
45 |
+
# --- Country Selection ---
|
46 |
+
st.subheader("Select Your Country for Helpline Information:")
|
47 |
+
if "selected_country" not in st.session_state:
|
48 |
+
st.session_state.selected_country = "Other" # Default
|
49 |
+
|
50 |
+
st.session_state.selected_country = st.selectbox(
|
51 |
+
"Choose your country:", options=list(COUNTRY_HELPLINES.keys()), index=0
|
52 |
+
)
|
53 |
+
|
54 |
+
# --- Chat History ---
|
55 |
if "chat_history" not in st.session_state:
|
56 |
st.session_state.chat_history = []
|
57 |
|
58 |
+
# --- User Input ---
|
59 |
user_input = st.text_input("Ask me anything:")
|
60 |
|
61 |
if st.button("Send"):
|
62 |
+
if user_input.strip():
|
63 |
+
# Generate chatbot response using country selection
|
64 |
+
response = generate_response(user_input, st.session_state.selected_country)
|
65 |
+
|
66 |
+
# Store conversation
|
67 |
+
st.session_state.chat_history.append(f"You: {user_input}")
|
68 |
+
st.session_state.chat_history.append(f"Bot: {response}")
|
69 |
+
|
70 |
+
# Display chat history
|
71 |
+
for msg in st.session_state.chat_history:
|
72 |
+
st.write(msg)
|
73 |
+
|
74 |
+
# Display country-specific helpline
|
75 |
+
st.markdown(f"π **Helpline for {st.session_state.selected_country}:** {get_helpline_for_country(st.session_state.selected_country)}")
|
76 |
+
|
77 |
+
else:
|
78 |
+
st.warning("Please enter a message.")
|
79 |
+
|
80 |
+
# --- Feedback System ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
st.markdown("---")
|
82 |
st.subheader("Feedback")
|
83 |
col1, col2 = st.columns(2)
|
|
|
94 |
save_metrics(st.session_state.metrics)
|
95 |
st.error("We're sorry to hear that. We'll try to improve! π")
|
96 |
|
97 |
+
# --- User Experience Progress Bar ---
|
98 |
total_feedback = (
|
99 |
st.session_state.metrics["feedback"]["thumbs_up"] + st.session_state.metrics["feedback"]["thumbs_down"]
|
100 |
)
|
|
|
111 |
st.subheader("User Experience")
|
112 |
st.write("No feedback yet. Be the first to share your experience!")
|
113 |
|
114 |
+
# --- Session ID Display ---
|
115 |
st.markdown("---")
|
116 |
+
st.write(f"Session ID: {st.session_state.session_id}")
|