Kishorekumar7/Suggestion_System
0
1import streamlit as st2import pickle3import re4import nltk5from nltk.corpus import stopwords6from catboost import CatBoostClassifier7from sklearn.feature_extraction.text import TfidfVectorizer8from groq import Groq9 10# ------------------------------------------------------------11# Setup and downloads12# ------------------------------------------------------------13nltk.download('stopwords')14stop_words = set(stopwords.words('english'))15 16# ------------------------------------------------------------17# Load vectorizer and model18# ------------------------------------------------------------19with open('vectorizer.pkl', 'rb') as f:20 vectorizer = pickle.load(f)21with open('catboost_model.pkl', 'rb') as f:22 model = pickle.load(f)23 24# ------------------------------------------------------------25# Text cleaning function (same as used in training)26# ------------------------------------------------------------27def clean_text(text):28 text = text.lower()29 text = re.sub(r'@[\w_]+', '', text)30 text = re.sub(r'http\S+|www\S+', '', text)31 text = re.sub(r'[^a-z\s]', '', text)32 text = re.sub(r'\s+', ' ', text).strip()33 tokens = [word for word in text.split() if word not in stop_words]34 return ' '.join(tokens)35 36# ------------------------------------------------------------37# Groq Client Setup38# ------------------------------------------------------------39client = Groq(api_key=st.secrets["GROQ_API_KEY"])40 41# ------------------------------------------------------------42# Streamlit UI Configuration43# ------------------------------------------------------------44st.set_page_config(page_title="Mood Reset Chatbot", page_icon="๐", layout="centered")45 46# Custom CSS for Twitter-like look47st.markdown(48 """49 <style>50 body {51 background-color: #F5F8FA;52 color: #14171A;53 font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;54 }55 .main-title {56 color: #1DA1F2;57 text-align: center;58 font-size: 40px;59 font-weight: bold;60 margin-bottom: 20px;61 }62 .tweet-box {63 background-color: white;64 padding: 20px;65 border-radius: 20px;66 box-shadow: 0px 2px 10px rgba(0,0,0,0.1);67 margin-top: 10px;68 }69 .prediction {70 font-size: 20px;71 font-weight: 600;72 color: #1DA1F2;73 margin-top: 10px;74 }75 .response-box {76 background-color: #E8F5FD;77 border-radius: 20px;78 padding: 15px;79 margin-top: 10px;80 font-size: 18px;81 }82 </style>83 """,84 unsafe_allow_html=True85)86 87# ------------------------------------------------------------88# App layout89# ------------------------------------------------------------90st.markdown('<div class="main-title">๐ Mood Reset Chatbot</div>', unsafe_allow_html=True)91 92username = st.text_input("Enter your name:", placeholder="e.g. Kishore")93user_text = st.text_area("What's on your mind? (like posting on Twitter)", height=150)94 95if st.button("Analyze My Mood"):96 if user_text.strip() == "":97 st.warning("Please enter some text to analyze.")98 else:99 cleaned = clean_text(user_text)100 vectorized = vectorizer.transform([cleaned])101 prediction = model.predict(vectorized)[0]102 103 mood_emoji = {104 "Sad/Depressed": "๐",105 "Suicidal": "๐",106 "Anxious/Angry": "๐ก",107 "Positive": "๐",108 "Neutral": "๐"109 }110 111 emoji = mood_emoji.get(prediction, "๐ญ")112 st.markdown(f'<div class="tweet-box"><div class="prediction">{emoji} Detected Mood: {prediction}</div></div>', unsafe_allow_html=True)113 114 # Compose prompt for Groq GPT model115 prompt = f"""116 You are a friendly AI companion. The user's detected emotion is '{prediction}'.117 Respond as if talking to {username or 'your friend'} in a warm, friendly tone.118 Your goal: lift their spirits, calm their mind, and help them feel better.119 Suggest something meaningful โ it could be a joke, motivational quote, calming playlist idea, or short pep talk.120 If emotion is 'Suicidal', include India's helpline number 9152987821 and a strong hopeful message.121 Be impactful, positive, and empathetic.122 """123 124 try:125 with st.spinner("Generating a supportive message..."):126 completion = client.chat.completions.create(127 model="openai/gpt-oss-120b",128 messages=[{"role": "user", "content": prompt}],129 temperature=1,130 max_completion_tokens=500,131 top_p=1,132 reasoning_effort="medium"133 )134 135 response = completion.choices[0].message.content.strip()136 st.markdown(f'<div class="response-box">{response}</div>', unsafe_allow_html=True)137 138 except Exception as e:139 st.error(f"Error generating response: {e}")140 141st.markdown("---")142st.caption("Developed with ๐ by Kishore | Powered by CatBoost + Groq GPT-OSS-120B")143 