Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
Β·
f37d2cc
1
Parent(s):
8d3fcda
more changes
Browse files- app.py +5 -6
- chatbot.py +34 -21
- db.py +3 -2
app.py
CHANGED
@@ -1,16 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
from db import insert_data_if_empty, get_mongo_client
|
4 |
from chatbot import chatbot_response
|
5 |
|
6 |
-
#
|
7 |
insert_data_if_empty()
|
8 |
-
|
9 |
-
# Connect to MongoDB (useful for potential visualizations)
|
10 |
collection = get_mongo_client()
|
11 |
|
12 |
-
st.subheader("π¬ Chatbot
|
13 |
-
|
|
|
|
|
14 |
|
15 |
if st.button("Get AI Response"):
|
16 |
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from db import insert_data_if_empty, get_mongo_client
|
3 |
from chatbot import chatbot_response
|
4 |
|
5 |
+
# Ensure MongoDB is populated.
|
6 |
insert_data_if_empty()
|
|
|
|
|
7 |
collection = get_mongo_client()
|
8 |
|
9 |
+
st.subheader("π¬ Chatbot for MongoDB Entry Analysis")
|
10 |
+
st.write("Ask me something (e.g., 'Provide analysis for the data entry 1 in the dataset'):")
|
11 |
+
|
12 |
+
user_prompt = st.text_area("Your query:")
|
13 |
|
14 |
if st.button("Get AI Response"):
|
15 |
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
|
chatbot.py
CHANGED
@@ -38,8 +38,12 @@ def analyze_sentiment(text):
|
|
38 |
result = sentiment_pipeline(text)[0]
|
39 |
label = result['label']
|
40 |
score = result['score']
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
except Exception as e:
|
44 |
return f"Error analyzing sentiment: {e}", None
|
45 |
|
@@ -52,38 +56,47 @@ def extract_topic(text):
|
|
52 |
except Exception as e:
|
53 |
return f"Error extracting topic: {e}", None
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
match = re.search(
|
59 |
if match:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
def chatbot_response(user_prompt):
|
66 |
if not user_prompt:
|
67 |
return None, None, None, None, None
|
|
|
68 |
try:
|
69 |
-
|
70 |
-
|
|
|
|
|
71 |
entry = get_entry_by_index(index)
|
72 |
if entry is None:
|
73 |
-
return "β No entry found for
|
74 |
entry_text = entry.get("text", "No text available.")
|
75 |
-
#
|
76 |
-
|
77 |
-
# Analyze the entry's text
|
78 |
sentiment_label, sentiment_confidence = analyze_sentiment(entry_text)
|
79 |
topic_label, topic_confidence = extract_topic(entry_text)
|
80 |
-
return ai_response_text, sentiment_label, sentiment_confidence, topic_label, topic_confidence
|
81 |
else:
|
82 |
-
# For
|
83 |
-
|
84 |
-
ai_response = model_gen.generate_content(user_prompt)
|
85 |
sentiment_label, sentiment_confidence = analyze_sentiment(user_prompt)
|
86 |
topic_label, topic_confidence = extract_topic(user_prompt)
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
except Exception as e:
|
89 |
return f"β Error: {e}", None, None, None, None
|
|
|
38 |
result = sentiment_pipeline(text)[0]
|
39 |
label = result['label']
|
40 |
score = result['score']
|
41 |
+
sentiment_mapping = {
|
42 |
+
"LABEL_0": "Negative",
|
43 |
+
"LABEL_1": "Neutral",
|
44 |
+
"LABEL_2": "Positive"
|
45 |
+
}
|
46 |
+
return sentiment_mapping.get(label, "Unknown"), score
|
47 |
except Exception as e:
|
48 |
return f"Error analyzing sentiment: {e}", None
|
49 |
|
|
|
56 |
except Exception as e:
|
57 |
return f"Error extracting topic: {e}", None
|
58 |
|
59 |
+
# Helper function to parse a data entry index from the user's prompt.
|
60 |
+
# It looks for a pattern like "data entry 1" or "entry 1" (case insensitive).
|
61 |
+
def parse_entry_index(prompt):
|
62 |
+
match = re.search(r'(?:data\s+entry|entry)\s+(\d+)', prompt, re.IGNORECASE)
|
63 |
if match:
|
64 |
+
try:
|
65 |
+
# Convert to zero-based index.
|
66 |
+
return int(match.group(1)) - 1
|
67 |
+
except ValueError:
|
68 |
+
return None
|
69 |
+
return None
|
70 |
|
71 |
def chatbot_response(user_prompt):
|
72 |
if not user_prompt:
|
73 |
return None, None, None, None, None
|
74 |
+
|
75 |
try:
|
76 |
+
# Check if the prompt contains a specific data entry request.
|
77 |
+
index = parse_entry_index(user_prompt)
|
78 |
+
if index is not None:
|
79 |
+
# Fetch the specified entry from MongoDB.
|
80 |
entry = get_entry_by_index(index)
|
81 |
if entry is None:
|
82 |
+
return f"β No entry found for data entry {index+1}.", None, None, None, None
|
83 |
entry_text = entry.get("text", "No text available.")
|
84 |
+
# Construct a simple generative prompt.
|
85 |
+
combined_prompt = f"Let's break down this tweet-like MongoDB entry:\n{entry_text}"
|
86 |
+
# Analyze sentiment and topic on the entry's text.
|
87 |
sentiment_label, sentiment_confidence = analyze_sentiment(entry_text)
|
88 |
topic_label, topic_confidence = extract_topic(entry_text)
|
|
|
89 |
else:
|
90 |
+
# For any other prompt, use it as is.
|
91 |
+
combined_prompt = user_prompt
|
|
|
92 |
sentiment_label, sentiment_confidence = analyze_sentiment(user_prompt)
|
93 |
topic_label, topic_confidence = extract_topic(user_prompt)
|
94 |
+
|
95 |
+
# Generate AI response using Gemini with the constructed prompt.
|
96 |
+
model_gen = genai.GenerativeModel("gemini-1.5-pro")
|
97 |
+
ai_response = model_gen.generate_content(combined_prompt)
|
98 |
+
|
99 |
+
# Return the generative response and the separately computed sentiment and category.
|
100 |
+
return ai_response.text, sentiment_label, sentiment_confidence, topic_label, topic_confidence
|
101 |
except Exception as e:
|
102 |
return f"β Error: {e}", None, None, None, None
|
db.py
CHANGED
@@ -24,8 +24,9 @@ def insert_data_if_empty():
|
|
24 |
|
25 |
def get_entry_by_index(index=0):
|
26 |
collection = get_mongo_client()
|
27 |
-
|
28 |
-
|
|
|
29 |
if docs:
|
30 |
return docs[0]
|
31 |
return None
|
|
|
24 |
|
25 |
def get_entry_by_index(index=0):
|
26 |
collection = get_mongo_client()
|
27 |
+
# Skip the first "index" documents and return the next one.
|
28 |
+
cursor = collection.find({}, {"_id": 0}).skip(index).limit(1)
|
29 |
+
docs = list(cursor)
|
30 |
if docs:
|
31 |
return docs[0]
|
32 |
return None
|