Spaces:
Sleeping
Sleeping
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 Data is Inserted Before Display | |
insert_data_if_empty() | |
# 2. MongoDB Connection | |
collection = get_mongo_client() | |
st.title("π MongoDB Data Viewer with AI Sentiment Chatbot") | |
# --- Historical Data Analytics Section --- | |
st.subheader("Historical Data Analytics") | |
data = list(collection.find({}, {"_id": 0})) | |
if data: | |
df = pd.DataFrame(data) | |
# The sentiment140.csv dataset typically contains a column named 'target' | |
# which holds the sentiment labels (0=Negative, 2=Neutral, 4=Positive) | |
if 'target' in df.columns: | |
# Map numeric sentiment labels to readable text | |
sentiment_map = {0: "Negative", 2: "Neutral", 4: "Positive"} | |
df['sentiment_label'] = df['target'].map(sentiment_map) | |
# Compute sentiment distribution | |
sentiment_counts = df['sentiment_label'].value_counts() | |
st.write("### Sentiment Distribution in Historical Data") | |
st.bar_chart(sentiment_counts) | |
else: | |
st.warning("The dataset does not contain a 'target' column for sentiment.") | |
else: | |
st.warning("No historical data available.") | |
# Optionally, a button to display the complete historical data | |
if st.button("Show Complete Historical Data"): | |
st.write(pd.DataFrame(data)) | |
# --- Chatbot Section with Sentiment & Topic Analysis --- | |
st.subheader("π¬ Chatbot with Sentiment & Topic Analysis") | |
user_prompt = st.text_area("Ask me something:") | |
if st.button("Get AI Response"): | |
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)") | |
# Additional: Compare with historical data | |
if data and 'target' in df.columns: | |
historical_count = df[df['sentiment_label'] == sentiment_label].shape[0] | |
st.info(f"In our historical dataset, there are {historical_count} tweets with a {sentiment_label} sentiment.") | |
else: | |
st.warning("β οΈ Please enter a question or text for analysis.") | |