KrSharangrav commited on
Commit
3280b9f
·
1 Parent(s): 5a94c8e

change in app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -26
app.py CHANGED
@@ -3,38 +3,48 @@ 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 Data is Inserted Before Display**
7
  insert_data_if_empty()
8
 
9
- #### **2. MongoDB Connection**
10
  collection = get_mongo_client()
11
 
12
- #### **3. Streamlit App UI**
13
- # st.title("📊 MongoDB Data Viewer with AI Sentiment Chatbot")
14
-
15
- # # Show first 5 rows from MongoDB
16
- # st.subheader("First 5 Rows from Database")
17
- # data = list(collection.find({}, {"_id": 0}).limit(5))
18
-
19
- # if data:
20
- # st.write(pd.DataFrame(data))
21
- # else:
22
- # st.warning("⚠️ No data found. Try refreshing the app.")
23
-
24
- # # Button to show full MongoDB data
25
- # if st.button("Show Complete Data"):
26
- # all_data = list(collection.find({}, {"_id": 0}))
27
- # st.write(pd.DataFrame(all_data))
 
 
 
 
 
 
 
 
28
 
29
- #### **4. AI Chatbot with Sentiment & Topic Analysis**
30
- st.subheader("🤖 AI Chatbot with Sentiment & Topic Analysis")
 
31
 
32
- # User input for chatbot
33
- user_prompt = st.text_area("Ask AI something or paste text for analysis:")
 
34
 
35
- if st.button("Analyze Sentiment & Get AI Response"):
36
  ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
37
-
38
  if ai_response:
39
  st.write("### AI Response:")
40
  st.write(ai_response)
@@ -42,7 +52,12 @@ if st.button("Analyze Sentiment & Get AI Response"):
42
  st.write("### Sentiment Analysis:")
43
  st.write(f"**Sentiment:** {sentiment_label} ({sentiment_confidence:.2f} confidence)")
44
 
45
- st.write("### Topic Extraction:")
46
- st.write(f"**Detected Topic:** {topic_label} ({topic_confidence:.2f} confidence)")
 
 
 
 
 
47
  else:
48
  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 Data is Inserted Before Display
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)
 
52
  st.write("### Sentiment Analysis:")
53
  st.write(f"**Sentiment:** {sentiment_label} ({sentiment_confidence:.2f} confidence)")
54
 
55
+ st.write("### Category Extraction:")
56
+ st.write(f"**Detected Category:** {topic_label} ({topic_confidence:.2f} confidence)")
57
+
58
+ # Additional: Compare with historical data
59
+ if data and 'target' in df.columns:
60
+ historical_count = df[df['sentiment_label'] == sentiment_label].shape[0]
61
+ st.info(f"In our historical dataset, there are {historical_count} tweets with a {sentiment_label} sentiment.")
62
  else:
63
  st.warning("⚠️ Please enter a question or text for analysis.")