TARS.help / app.py
Divymakesml's picture
Update app.py
25a7813 verified
raw
history blame
3.37 kB
import os
import time
from datetime import datetime
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# -- SETUP --
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
@st.cache_resource
def load_model():
model_id = "google/flan-t5-base"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
return tokenizer, model
tokenizer, model = load_model()
if "history" not in st.session_state:
st.session_state.history = []
st.session_state.summary = ""
# -- TEXT GENERATION FUNCTION --
def generate_text(prompt, max_new_tokens=150):
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
# -- HIGH-RISK FILTER --
TRIGGER_PHRASES = ["kill myself", "end it all", "suicide", "not worth living", "can't go on"]
def is_high_risk(text):
return any(phrase in text.lower() for phrase in TRIGGER_PHRASES)
# -- STYLING --
st.markdown("""
<style>
body {
background-color: #111827;
color: #f3f4f6;
}
.stTextInput > div > div > input {
color: #f3f4f6;
}
</style>
""", unsafe_allow_html=True)
# -- HEADER --
st.title("🧠 TARS.help")
st.markdown("### A minimal AI that listens, reflects, and replies.")
st.markdown(f"πŸ—“οΈ {datetime.now().strftime('%B %d, %Y')} | {len(st.session_state.history)//2} exchanges")
# -- USER INPUT --
user_input = st.text_input("How are you feeling today?", placeholder="Start typing...")
# -- MAIN CHAT LOGIC --
if user_input:
context = "\n".join([f"{s}: {m}" for s, m, _ in st.session_state.history[-4:]])
with st.spinner("TARS is reflecting..."):
time.sleep(1.2)
if is_high_risk(user_input):
response = "I'm really sorry you're feeling this way. You're not alone β€” please talk to someone you trust or a mental health professional. πŸ’™"
else:
prompt = f"Respond with empathy:\n{context}\nUser: {user_input}"
response = generate_text(prompt, max_new_tokens=100)
timestamp = datetime.now().strftime("%H:%M")
st.session_state.history.append(("🧍 You", user_input, timestamp))
st.session_state.history.append(("πŸ€– TARS", response, timestamp))
# -- DISPLAY CHAT --
st.markdown("## πŸ—¨οΈ Session")
for speaker, msg, time in st.session_state.history:
st.markdown(f"**{speaker} [{time}]:** {msg}")
# -- SUMMARY GENERATION --
if st.button("🧾 Generate Session Summary"):
convo = "\n".join([f"{s}: {m}" for s, m, _ in st.session_state.history])
summary_prompt = f"Summarize this conversation in 2-3 thoughtful sentences:\n{convo}"
try:
summary = generate_text(summary_prompt, max_new_tokens=150)
st.session_state.summary = summary
except Exception as e:
st.error("❌ Summary generation failed.")
st.exception(e)
# -- DISPLAY SUMMARY --
if st.session_state.summary:
st.markdown("### 🧠 Session Note")
st.markdown(st.session_state.summary)
st.download_button("πŸ“₯ Download Summary", st.session_state.summary, file_name="tars_session.txt")
# -- FOOTER --
st.markdown("---")
st.caption("TARS is not a therapist but a quiet assistant that reflects with you.")