File size: 2,187 Bytes
0105e3b
af09235
7268351
 
ea4634d
472c7aa
7268351
ea4634d
3280b9f
7268351
ea4634d
3280b9f
472c7aa
 
3280b9f
7268351
3280b9f
472c7aa
5a94c8e
472c7aa
7268351
 
 
 
 
5a94c8e
f763dd0
3280b9f
 
 
472c7aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")