Spaces:
Running
Running
KrSharangrav
commited on
Commit
·
472c7aa
1
Parent(s):
3280b9f
change done in app.py
Browse files
app.py
CHANGED
@@ -3,48 +3,21 @@ import pandas as pd
|
|
3 |
from db import insert_data_if_empty, get_mongo_client
|
4 |
from chatbot import chatbot_response # Import chatbot functionality
|
5 |
|
6 |
-
# 1. Ensure
|
7 |
insert_data_if_empty()
|
8 |
|
9 |
# 2. MongoDB Connection
|
10 |
collection = get_mongo_client()
|
11 |
|
12 |
-
st.title("📊 MongoDB Data Viewer with AI Sentiment Chatbot")
|
13 |
-
|
14 |
-
# --- Historical Data Analytics Section ---
|
15 |
-
st.subheader("Historical Data Analytics")
|
16 |
-
data = list(collection.find({}, {"_id": 0}))
|
17 |
-
|
18 |
-
if data:
|
19 |
-
df = pd.DataFrame(data)
|
20 |
-
# The sentiment140.csv dataset typically contains a column named 'target'
|
21 |
-
# which holds the sentiment labels (0=Negative, 2=Neutral, 4=Positive)
|
22 |
-
if 'target' in df.columns:
|
23 |
-
# Map numeric sentiment labels to readable text
|
24 |
-
sentiment_map = {0: "Negative", 2: "Neutral", 4: "Positive"}
|
25 |
-
df['sentiment_label'] = df['target'].map(sentiment_map)
|
26 |
-
|
27 |
-
# Compute sentiment distribution
|
28 |
-
sentiment_counts = df['sentiment_label'].value_counts()
|
29 |
-
|
30 |
-
st.write("### Sentiment Distribution in Historical Data")
|
31 |
-
st.bar_chart(sentiment_counts)
|
32 |
-
else:
|
33 |
-
st.warning("The dataset does not contain a 'target' column for sentiment.")
|
34 |
-
else:
|
35 |
-
st.warning("No historical data available.")
|
36 |
-
|
37 |
-
# Optionally, a button to display the complete historical data
|
38 |
-
if st.button("Show Complete Historical Data"):
|
39 |
-
st.write(pd.DataFrame(data))
|
40 |
-
|
41 |
-
# --- Chatbot Section with Sentiment & Topic Analysis ---
|
42 |
st.subheader("💬 Chatbot with Sentiment & Topic Analysis")
|
|
|
|
|
43 |
user_prompt = st.text_area("Ask me something:")
|
44 |
|
45 |
if st.button("Get AI Response"):
|
|
|
46 |
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
|
47 |
-
|
48 |
if ai_response:
|
49 |
st.write("### AI Response:")
|
50 |
st.write(ai_response)
|
@@ -55,9 +28,22 @@ if st.button("Get AI Response"):
|
|
55 |
st.write("### Category Extraction:")
|
56 |
st.write(f"**Detected Category:** {topic_label} ({topic_confidence:.2f} confidence)")
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
else:
|
63 |
st.warning("⚠️ Please enter a question or text for analysis.")
|
|
|
3 |
from db import insert_data_if_empty, get_mongo_client
|
4 |
from chatbot import chatbot_response # Import chatbot functionality
|
5 |
|
6 |
+
# 1. Ensure the sentiment140 dataset is inserted into MongoDB
|
7 |
insert_data_if_empty()
|
8 |
|
9 |
# 2. MongoDB Connection
|
10 |
collection = get_mongo_client()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
st.subheader("💬 Chatbot with Sentiment & Topic Analysis")
|
13 |
+
|
14 |
+
# User input for chatbot
|
15 |
user_prompt = st.text_area("Ask me something:")
|
16 |
|
17 |
if st.button("Get AI Response"):
|
18 |
+
# Generate real-time AI response, sentiment, and topic extraction
|
19 |
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
|
20 |
+
|
21 |
if ai_response:
|
22 |
st.write("### AI Response:")
|
23 |
st.write(ai_response)
|
|
|
28 |
st.write("### Category Extraction:")
|
29 |
st.write(f"**Detected Category:** {topic_label} ({topic_confidence:.2f} confidence)")
|
30 |
|
31 |
+
# 3. Historical Insight: Compare with historical data from sentiment140.csv
|
32 |
+
historical_data = list(collection.find({}, {"_id": 0}))
|
33 |
+
if historical_data:
|
34 |
+
df = pd.DataFrame(historical_data)
|
35 |
+
# Assume the CSV has a 'sentiment' column with numeric labels:
|
36 |
+
# 0: Negative, 2: Neutral, 4: Positive.
|
37 |
+
if 'sentiment' in df.columns:
|
38 |
+
sentiment_map = {0: "Negative", 2: "Neutral", 4: "Positive"}
|
39 |
+
# Ensure the sentiment column is numeric and map it to readable labels
|
40 |
+
df['sentiment_label'] = df['sentiment'].astype(int).map(sentiment_map)
|
41 |
+
matching_count = df[df['sentiment_label'] == sentiment_label].shape[0]
|
42 |
+
st.write("### Historical Insights:")
|
43 |
+
st.info(f"There are {matching_count} tweets in our dataset with a {sentiment_label} sentiment.")
|
44 |
+
else:
|
45 |
+
st.warning("Historical data does not contain a sentiment field.")
|
46 |
+
else:
|
47 |
+
st.warning("No historical data available.")
|
48 |
else:
|
49 |
st.warning("⚠️ Please enter a question or text for analysis.")
|