Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Configure the Streamlit page
|
5 |
+
st.set_page_config(page_title="AI Friend Therapist Assistant", layout="centered")
|
6 |
+
|
7 |
+
# Load the Hugging Face summarization model (cached to avoid reloading)
|
8 |
+
@st.cache(allow_output_mutation=True)
|
9 |
+
def load_summarizer():
|
10 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
11 |
+
|
12 |
+
summarizer = load_summarizer()
|
13 |
+
|
14 |
+
# Initialize session state to store conversation history
|
15 |
+
if 'conversation' not in st.session_state:
|
16 |
+
st.session_state.conversation = []
|
17 |
+
|
18 |
+
# App title and disclaimer
|
19 |
+
st.title("AI Friend Therapist Assistant")
|
20 |
+
st.write(
|
21 |
+
"I'm here to listen. Share your thoughts below.\n\n"
|
22 |
+
"*Disclaimer: I am not a licensed therapist. If you're in crisis, please seek professional help immediately.*"
|
23 |
+
)
|
24 |
+
|
25 |
+
# Text area for user input
|
26 |
+
user_input = st.text_area("Your message", height=150)
|
27 |
+
|
28 |
+
# When the user clicks "Send", process the input
|
29 |
+
if st.button("Send"):
|
30 |
+
if user_input.strip():
|
31 |
+
# Append the message to conversation history
|
32 |
+
st.session_state.conversation.append(user_input)
|
33 |
+
|
34 |
+
# Define keywords that might indicate suicidal ideation or distress
|
35 |
+
flagged_keywords = [
|
36 |
+
"suicide", "kill myself", "end my life", "self-harm",
|
37 |
+
"self harm", "hopeless", "despair", "worthless", "can't go on"
|
38 |
+
]
|
39 |
+
# Check if any flagged keyword is present in the input
|
40 |
+
if any(keyword in user_input.lower() for keyword in flagged_keywords):
|
41 |
+
st.error(
|
42 |
+
"Your message indicates you may be experiencing distress. "
|
43 |
+
"If you're in immediate danger, please call **911** immediately. "
|
44 |
+
"For non-immediate crisis support in Canada, call **Crisis Services Canada at 1.833.456.4566** "
|
45 |
+
"or visit [crisisservicescanada.ca](https://www.crisisservicescanada.ca/)."
|
46 |
+
)
|
47 |
+
else:
|
48 |
+
st.success("Message received. Thank you for sharing.")
|
49 |
+
else:
|
50 |
+
st.warning("Please enter a message.")
|
51 |
+
|
52 |
+
# Option to generate a therapist summary of the conversation
|
53 |
+
if st.button("Generate Therapist Summary"):
|
54 |
+
if st.session_state.conversation:
|
55 |
+
conversation_text = "\n".join(st.session_state.conversation)
|
56 |
+
summary = summarizer(conversation_text, max_length=100, min_length=30, do_sample=False)
|
57 |
+
st.subheader("Therapist Summary")
|
58 |
+
st.write(summary[0]['summary_text'])
|
59 |
+
else:
|
60 |
+
st.info("No conversation to summarize yet.")
|