File size: 2,498 Bytes
0105e3b
af09235
7268351
 
ea4634d
3280b9f
7268351
ea4634d
3280b9f
7268351
ea4634d
3280b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7268351
3280b9f
 
 
7268351
3280b9f
 
 
7268351
3280b9f
5a94c8e
3280b9f
7268351
 
 
 
 
5a94c8e
f763dd0
3280b9f
 
 
 
 
 
 
e94ec88
f763dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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.")