Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
Β·
f89cec9
1
Parent(s):
f37d2cc
changes made in all 3
Browse files- app.py +8 -6
- chatbot.py +36 -28
- db.py +8 -3
app.py
CHANGED
@@ -1,20 +1,22 @@
|
|
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
|
6 |
insert_data_if_empty()
|
7 |
-
collection = get_mongo_client()
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
|
|
|
|
13 |
|
14 |
if st.button("Get AI Response"):
|
15 |
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
|
16 |
if ai_response:
|
17 |
-
st.write("###
|
18 |
st.write(ai_response)
|
19 |
st.write("### Sentiment Analysis:")
|
20 |
st.write(f"**Sentiment:** {sentiment_label} ({sentiment_confidence:.2f} confidence)")
|
|
|
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 |
+
# Ensure that historical data is inserted if not already present.
|
7 |
insert_data_if_empty()
|
|
|
8 |
|
9 |
+
# Connect to MongoDB (optional: can be used for additional visualizations).
|
10 |
+
collection = get_mongo_client()
|
11 |
|
12 |
+
st.subheader("π¬ Chatbot with Analysis for MongoDB Entries")
|
13 |
+
# Updated hint: ask for analysis of a specific data entry.
|
14 |
+
user_prompt = st.text_area("Ask me something (e.g., 'Provide analysis for the data entry 1 in the dataset'):")
|
15 |
|
16 |
if st.button("Get AI Response"):
|
17 |
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence = chatbot_response(user_prompt)
|
18 |
if ai_response:
|
19 |
+
st.write("### Response:")
|
20 |
st.write(ai_response)
|
21 |
st.write("### Sentiment Analysis:")
|
22 |
st.write(f"**Sentiment:** {sentiment_label} ({sentiment_confidence:.2f} confidence)")
|
chatbot.py
CHANGED
@@ -3,7 +3,7 @@ import re
|
|
3 |
import streamlit as st
|
4 |
import google.generativeai as genai
|
5 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
6 |
-
from db import get_entry_by_index
|
7 |
|
8 |
# Configure Gemini API key
|
9 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
@@ -56,16 +56,13 @@ def extract_topic(text):
|
|
56 |
except Exception as e:
|
57 |
return f"Error extracting topic: {e}", None
|
58 |
|
59 |
-
# Helper
|
60 |
-
#
|
61 |
-
def
|
62 |
-
match = re.search(r'(
|
63 |
if match:
|
64 |
-
|
65 |
-
|
66 |
-
return int(match.group(1)) - 1
|
67 |
-
except ValueError:
|
68 |
-
return None
|
69 |
return None
|
70 |
|
71 |
def chatbot_response(user_prompt):
|
@@ -73,30 +70,41 @@ def chatbot_response(user_prompt):
|
|
73 |
return None, None, None, None, None
|
74 |
|
75 |
try:
|
76 |
-
# Check if the
|
77 |
-
|
78 |
-
if
|
79 |
-
# Fetch the
|
80 |
-
entry = get_entry_by_index(
|
81 |
if entry is None:
|
82 |
-
return
|
|
|
83 |
entry_text = entry.get("text", "No text available.")
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
sentiment_label, sentiment_confidence = analyze_sentiment(entry_text)
|
88 |
topic_label, topic_confidence = extract_topic(entry_text)
|
|
|
|
|
89 |
else:
|
90 |
-
# For
|
91 |
-
|
|
|
|
|
|
|
|
|
92 |
sentiment_label, sentiment_confidence = analyze_sentiment(user_prompt)
|
93 |
topic_label, topic_confidence = extract_topic(user_prompt)
|
94 |
-
|
95 |
-
|
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
|
|
|
3 |
import streamlit as st
|
4 |
import google.generativeai as genai
|
5 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
6 |
+
from db import get_entry_by_index # For fetching a specific entry from MongoDB
|
7 |
|
8 |
# Configure Gemini API key
|
9 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
|
|
56 |
except Exception as e:
|
57 |
return f"Error extracting topic: {e}", None
|
58 |
|
59 |
+
# Helper: extract an entry index from a query string.
|
60 |
+
# For example, "data entry 1" or "entry 2" will return index 0 or 1 respectively.
|
61 |
+
def extract_entry_index(prompt):
|
62 |
+
match = re.search(r'(data entry|entry)\s+(\d+)', prompt, re.IGNORECASE)
|
63 |
if match:
|
64 |
+
index = int(match.group(2)) - 1 # Convert to 0-based index
|
65 |
+
return index
|
|
|
|
|
|
|
66 |
return None
|
67 |
|
68 |
def chatbot_response(user_prompt):
|
|
|
70 |
return None, None, None, None, None
|
71 |
|
72 |
try:
|
73 |
+
# Check if the user query asks for a specific dataset entry.
|
74 |
+
entry_index = extract_entry_index(user_prompt)
|
75 |
+
if entry_index is not None:
|
76 |
+
# Fetch the requested entry from MongoDB.
|
77 |
+
entry = get_entry_by_index(entry_index)
|
78 |
if entry is None:
|
79 |
+
return "β No entry found for the requested index.", None, None, None, None
|
80 |
+
# Extract the required fields.
|
81 |
entry_text = entry.get("text", "No text available.")
|
82 |
+
entry_user = entry.get("user", "Unknown")
|
83 |
+
entry_date = entry.get("date", "Unknown")
|
84 |
+
|
85 |
+
# Build a static response message with only the desired parts.
|
86 |
+
ai_response = (
|
87 |
+
"Let's break down this tweet-like MongoDB entry:\n\n"
|
88 |
+
f"Text: {entry_text}\n"
|
89 |
+
f"User: {entry_user}\n"
|
90 |
+
f"Date: {entry_date}"
|
91 |
+
)
|
92 |
+
|
93 |
+
# Perform sentiment and topic analysis on the entry's text.
|
94 |
sentiment_label, sentiment_confidence = analyze_sentiment(entry_text)
|
95 |
topic_label, topic_confidence = extract_topic(entry_text)
|
96 |
+
|
97 |
+
return ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence
|
98 |
else:
|
99 |
+
# For other queries, use the generative model flow.
|
100 |
+
model_gen = genai.GenerativeModel("gemini-1.5-pro")
|
101 |
+
ai_response_obj = model_gen.generate_content(user_prompt)
|
102 |
+
ai_response = ai_response_obj.text
|
103 |
+
|
104 |
+
# Perform sentiment and topic analysis on the user prompt.
|
105 |
sentiment_label, sentiment_confidence = analyze_sentiment(user_prompt)
|
106 |
topic_label, topic_confidence = extract_topic(user_prompt)
|
107 |
+
|
108 |
+
return ai_response, sentiment_label, sentiment_confidence, topic_label, topic_confidence
|
|
|
|
|
|
|
|
|
|
|
109 |
except Exception as e:
|
110 |
return f"β Error: {e}", None, None, None, None
|
db.py
CHANGED
@@ -17,6 +17,11 @@ def insert_data_if_empty():
|
|
17 |
response = requests.get(csv_url)
|
18 |
response.raise_for_status()
|
19 |
df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")
|
|
|
|
|
|
|
|
|
|
|
20 |
collection.insert_many(df.to_dict("records"))
|
21 |
print("β
Data Inserted into MongoDB!")
|
22 |
except Exception as e:
|
@@ -24,9 +29,9 @@ def insert_data_if_empty():
|
|
24 |
|
25 |
def get_entry_by_index(index=0):
|
26 |
collection = get_mongo_client()
|
27 |
-
#
|
28 |
-
|
29 |
-
docs = list(
|
30 |
if docs:
|
31 |
return docs[0]
|
32 |
return None
|
|
|
17 |
response = requests.get(csv_url)
|
18 |
response.raise_for_status()
|
19 |
df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")
|
20 |
+
# Add default fields if not present.
|
21 |
+
if "user" not in df.columns:
|
22 |
+
df["user"] = "Unknown"
|
23 |
+
if "date" not in df.columns:
|
24 |
+
df["date"] = "Unknown"
|
25 |
collection.insert_many(df.to_dict("records"))
|
26 |
print("β
Data Inserted into MongoDB!")
|
27 |
except Exception as e:
|
|
|
29 |
|
30 |
def get_entry_by_index(index=0):
|
31 |
collection = get_mongo_client()
|
32 |
+
# Fetch the document by skipping "index" entries.
|
33 |
+
doc_cursor = collection.find({}, {"_id": 0}).skip(index).limit(1)
|
34 |
+
docs = list(doc_cursor)
|
35 |
if docs:
|
36 |
return docs[0]
|
37 |
return None
|