VirKapur's picture
Update app.py
a143784 verified
import gradio as gr
from transformers import pipeline
# Load sentiment analysis model locally
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
# Simple chatbot logic
def chatbot(user_input, history=[]):
# Analyse sentiment
sentiment = sentiment_pipeline(user_input)[0]
label = sentiment['label']
score = sentiment['score']
# Generate empathetic response
if label == "POSITIVE":
bot_reply = f"😊 I'm glad to hear that! You sound positive. (Confidence: {score:.2f})"
elif label == "NEGATIVE":
bot_reply = f"💙 I’m sorry you’re feeling this way. Remember, tough times pass. (Confidence: {score:.2f})"
else:
bot_reply = f"🤔 I see. Thanks for sharing with me. (Confidence: {score:.2f})"
# Maintain chat history
history.append((user_input, bot_reply))
return history, history
# Build UI
with gr.Blocks() as demo:
gr.Markdown("## 🧠 MindM8 – Offline Sentiment Chatbot")
chatbot_ui = gr.Chatbot()
msg = gr.Textbox(label="Type your message...")
state = gr.State([])
msg.submit(chatbot, [msg, state], [chatbot_ui, state])
msg.submit(lambda _: "", None, msg) # Clear input after send
# Launch
demo.launch()