import streamlit as st import pandas as pd from db import insert_data_if_empty, get_mongo_client from chatbot import chatbot_response # Import chatbot functionality # 1. Ensure the sentiment140 dataset is inserted into MongoDB insert_data_if_empty() # 2. MongoDB Connection collection = get_mongo_client() st.subheader("💬 Chatbot with Sentiment & Topic Analysis") # User input for chatbot user_prompt = st.text_area("Ask me something:") if st.button("Get AI Response"): # Generate real-time AI response, sentiment, and topic extraction ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt) if ai_response: st.write("### AI Response:") st.write(ai_response) st.write("### Sentiment Analysis:") st.write(f"**Sentiment:** {sentiment_label} ({sentiment_confidence:.2f} confidence)") st.write("### Category Extraction:") st.write(f"**Detected Category:** {topic_label} ({topic_confidence:.2f} confidence)") # 3. Historical Insight: Compare with historical data from sentiment140.csv historical_data = list(collection.find({}, {"_id": 0})) if historical_data: df = pd.DataFrame(historical_data) # Assume the CSV has a 'sentiment' column with numeric labels: # 0: Negative, 2: Neutral, 4: Positive. if 'sentiment' in df.columns: sentiment_map = {0: "Negative", 2: "Neutral", 4: "Positive"} # Ensure the sentiment column is numeric and map it to readable labels df['sentiment_label'] = df['sentiment'].astype(int).map(sentiment_map) matching_count = df[df['sentiment_label'] == sentiment_label].shape[0] st.write("### Historical Insights:") st.info(f"There are {matching_count} tweets in our dataset with a {sentiment_label} sentiment.") else: st.warning("Historical data does not contain a sentiment field.") else: st.warning("No historical data available.") else: st.warning("⚠️ Please enter a question or text for analysis.")