import os import time from datetime import datetime import streamlit as st from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline # -- SETUP -- os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" @st.cache_resource def load_respondent(): model_id = "mistralai/Mistral-7B-Instruct-v0.1" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, device_map="auto", trust_remote_code=True, torch_dtype="auto" ) return pipeline("text-generation", model=model, tokenizer=tokenizer) generator = load_respondent() if "history" not in st.session_state: st.session_state.history = [] st.session_state.summary = "" # -- STYLING -- st.markdown(""" """, 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") # -- SAFETY 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) # -- INPUT -- user_input = st.text_input("How are you feeling today?", placeholder="Start typing...") # -- REPLY FUNCTION -- def generate_reply(user_input, context): prompt = f"""You are a kind and empathetic AI assistant. Respond thoughtfully based on the following conversation: {context} User: {user_input} AI:""" response = generator(prompt, max_new_tokens=150, temperature=0.7)[0]['generated_text'] return response.split("AI:")[-1].strip() # -- CONVERSATION FLOW -- 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: full_context = context + f"\nUser: {user_input}" response = generate_reply(user_input, context) timestamp = datetime.now().strftime("%H:%M") st.session_state.history.append(("๐Ÿง You", user_input, timestamp)) st.session_state.history.append(("๐Ÿค– TARS", response, timestamp)) # -- DISPLAY HISTORY -- st.markdown("## ๐Ÿ—จ๏ธ Session") for speaker, msg, time in st.session_state.history: st.markdown(f"**{speaker} [{time}]:** {msg}") # -- SESSION SUMMARY -- if st.button("๐Ÿงพ Generate Session Summary"): convo = "\n".join([f"{s}: {m}" for s, m, _ in st.session_state.history]) prompt = f"""Summarize the emotional tone and key themes from this conversation in 3 sentences: {convo} Summary:""" try: output = generator(prompt, max_new_tokens=200, temperature=0.5)[0]['generated_text'] st.session_state.summary = output.split("Summary:")[-1].strip() except Exception as e: st.error("โŒ Summary generation failed.") st.exception(e) 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.")