Spaces:
Running
Running
KrSharangrav
commited on
Commit
Β·
f763dd0
1
Parent(s):
a51be6a
changes in the model with topic extraction
Browse files- app.py +20 -16
- backup.py +68 -49
- chatbot.py +20 -5
app.py
CHANGED
@@ -10,30 +10,30 @@ insert_data_if_empty()
|
|
10 |
collection = get_mongo_client()
|
11 |
|
12 |
#### **3. Streamlit App UI**
|
13 |
-
st.title("π AI Sentiment
|
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 |
-
#
|
21 |
-
#else:
|
22 |
-
#
|
23 |
|
24 |
-
# Button to show full MongoDB data
|
25 |
-
#if st.button("Show Complete Data"):
|
26 |
-
#
|
27 |
-
#
|
28 |
|
29 |
-
#### **4. AI Chatbot with Sentiment Analysis**
|
30 |
-
st.subheader("π€ AI Chatbot with Sentiment Analysis")
|
31 |
|
32 |
# User input for chatbot
|
33 |
user_prompt = st.text_area("Ask AI something or paste text for sentiment analysis:")
|
34 |
|
35 |
if st.button("Analyze Sentiment & Get AI Response"):
|
36 |
-
ai_response, sentiment_label, confidence = chatbot_response(user_prompt)
|
37 |
|
38 |
if ai_response:
|
39 |
st.write("### AI Response:")
|
@@ -41,5 +41,9 @@ if st.button("Analyze Sentiment & Get AI Response"):
|
|
41 |
|
42 |
st.write("### Sentiment Analysis:")
|
43 |
st.write(f"**Sentiment:** {sentiment_label} ({confidence:.2f} confidence)")
|
|
|
|
|
|
|
|
|
44 |
else:
|
45 |
-
st.warning("β οΈ Please enter a question or text for
|
|
|
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 Analysis & Topic Extraction**
|
30 |
+
st.subheader("π€ AI Chatbot with Sentiment Analysis & Topic Extraction")
|
31 |
|
32 |
# User input for chatbot
|
33 |
user_prompt = st.text_area("Ask AI something or paste text for sentiment analysis:")
|
34 |
|
35 |
if st.button("Analyze Sentiment & Get AI Response"):
|
36 |
+
ai_response, sentiment_label, confidence, topics = chatbot_response(user_prompt)
|
37 |
|
38 |
if ai_response:
|
39 |
st.write("### AI Response:")
|
|
|
41 |
|
42 |
st.write("### Sentiment Analysis:")
|
43 |
st.write(f"**Sentiment:** {sentiment_label} ({confidence:.2f} confidence)")
|
44 |
+
|
45 |
+
st.write("### Extracted Topics:")
|
46 |
+
st.write(", ".join(topics) if topics else "No topics identified.")
|
47 |
+
|
48 |
else:
|
49 |
+
st.warning("β οΈ Please enter a question or text for analysis.")
|
backup.py
CHANGED
@@ -1,18 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
|
4 |
-
import
|
5 |
-
from pymongo import MongoClient
|
6 |
-
from db import insert_data_if_empty, get_mongo_client # Import functions from db.py
|
7 |
-
from transformers import pipeline # Import Hugging Face transformers for sentiment analysis
|
8 |
-
|
9 |
-
# π Fetch API key from Hugging Face Secrets
|
10 |
-
GEMINI_API_KEY = os.getenv("gemini_api")
|
11 |
-
|
12 |
-
if GEMINI_API_KEY:
|
13 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
14 |
-
else:
|
15 |
-
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.")
|
16 |
|
17 |
#### **1. Ensure Data is Inserted Before Display**
|
18 |
insert_data_if_empty()
|
@@ -20,8 +9,8 @@ insert_data_if_empty()
|
|
20 |
#### **2. MongoDB Connection**
|
21 |
collection = get_mongo_client()
|
22 |
|
23 |
-
#### **3. Streamlit App
|
24 |
-
st.title("π
|
25 |
|
26 |
# Show first 5 rows from MongoDB
|
27 |
#st.subheader("First 5 Rows from Database")
|
@@ -37,48 +26,78 @@ st.title("π MongoDB Data Viewer with AI Sentiment Chatbot")
|
|
37 |
# all_data = list(collection.find({}, {"_id": 0}))
|
38 |
# st.write(pd.DataFrame(all_data))
|
39 |
|
40 |
-
#### **4.
|
41 |
-
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
42 |
-
|
43 |
-
# Function to analyze sentiment
|
44 |
-
def analyze_sentiment(text):
|
45 |
-
sentiment_result = sentiment_pipeline(text)[0]
|
46 |
-
label = sentiment_result['label'] # Extract sentiment label (POSITIVE, NEGATIVE, NEUTRAL)
|
47 |
-
score = sentiment_result['score'] # Extract confidence score
|
48 |
-
|
49 |
-
# Convert labels to a readable format
|
50 |
-
sentiment_mapping = {
|
51 |
-
"LABEL_0": "Negative",
|
52 |
-
"LABEL_1": "Neutral",
|
53 |
-
"LABEL_2": "Positive"
|
54 |
-
}
|
55 |
-
return sentiment_mapping.get(label, "Unknown"), score
|
56 |
-
|
57 |
-
#### **5. AI Chatbot with Sentiment Analysis**
|
58 |
st.subheader("π€ AI Chatbot with Sentiment Analysis")
|
59 |
|
60 |
# User input for chatbot
|
61 |
user_prompt = st.text_area("Ask AI something or paste text for sentiment analysis:")
|
62 |
|
63 |
if st.button("Analyze Sentiment & Get AI Response"):
|
64 |
-
|
65 |
-
try:
|
66 |
-
# AI Response from Gemini
|
67 |
-
model = genai.GenerativeModel("gemini-1.5-pro")
|
68 |
-
ai_response = model.generate_content(user_prompt)
|
69 |
|
70 |
-
|
71 |
-
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
st.write(ai_response.text)
|
76 |
-
|
77 |
-
st.write("### Sentiment Analysis:")
|
78 |
-
st.write(f"**Sentiment:** {sentiment_label} ({confidence:.2f} confidence)")
|
79 |
-
|
80 |
-
except Exception as e:
|
81 |
-
st.error(f"β Error: {e}")
|
82 |
else:
|
83 |
st.warning("β οΈ Please enter a question or text for sentiment analysis.")
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 # Import chatbot functionality
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
#### **1. Ensure Data is Inserted Before Display**
|
7 |
insert_data_if_empty()
|
|
|
9 |
#### **2. MongoDB Connection**
|
10 |
collection = get_mongo_client()
|
11 |
|
12 |
+
#### **3. Streamlit App UI**
|
13 |
+
st.title("π AI Sentiment Analysis Chatbot")
|
14 |
|
15 |
# Show first 5 rows from MongoDB
|
16 |
#st.subheader("First 5 Rows from Database")
|
|
|
26 |
# all_data = list(collection.find({}, {"_id": 0}))
|
27 |
# st.write(pd.DataFrame(all_data))
|
28 |
|
29 |
+
#### **4. AI Chatbot with Sentiment Analysis**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
st.subheader("π€ AI Chatbot with Sentiment Analysis")
|
31 |
|
32 |
# User input for chatbot
|
33 |
user_prompt = st.text_area("Ask AI something or paste text for sentiment analysis:")
|
34 |
|
35 |
if st.button("Analyze Sentiment & Get AI Response"):
|
36 |
+
ai_response, sentiment_label, confidence = chatbot_response(user_prompt)
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
if ai_response:
|
39 |
+
st.write("### AI Response:")
|
40 |
+
st.write(ai_response)
|
41 |
|
42 |
+
st.write("### Sentiment Analysis:")
|
43 |
+
st.write(f"**Sentiment:** {sentiment_label} ({confidence:.2f} confidence)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
else:
|
45 |
st.warning("β οΈ Please enter a question or text for sentiment analysis.")
|
46 |
|
47 |
+
#chatbot.py
|
48 |
+
import os
|
49 |
+
import streamlit as st
|
50 |
+
import google.generativeai as genai
|
51 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
52 |
+
|
53 |
+
# π Fetch API key from Hugging Face Secrets
|
54 |
+
GEMINI_API_KEY = os.getenv("gemini_api")
|
55 |
+
|
56 |
+
if GEMINI_API_KEY:
|
57 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
58 |
+
else:
|
59 |
+
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.")
|
60 |
+
|
61 |
+
# Correct Model Path
|
62 |
+
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
63 |
+
|
64 |
+
# Load Sentiment Analysis Model (Ensure the correct model is used)
|
65 |
+
try:
|
66 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
67 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model=MODEL_NAME, tokenizer=tokenizer)
|
68 |
+
except Exception as e:
|
69 |
+
st.error(f"β Error loading sentiment model: {e}")
|
70 |
+
|
71 |
+
# Function to analyze sentiment
|
72 |
+
def analyze_sentiment(text):
|
73 |
+
try:
|
74 |
+
sentiment_result = sentiment_pipeline(text)[0]
|
75 |
+
label = sentiment_result['label'] # Extract sentiment label (POSITIVE, NEGATIVE, NEUTRAL)
|
76 |
+
score = sentiment_result['score'] # Extract confidence score
|
77 |
+
|
78 |
+
# Convert labels to readable format
|
79 |
+
sentiment_mapping = {
|
80 |
+
"LABEL_0": "Negative",
|
81 |
+
"LABEL_1": "Neutral",
|
82 |
+
"LABEL_2": "Positive"
|
83 |
+
}
|
84 |
+
return sentiment_mapping.get(label, "Unknown"), score
|
85 |
+
except Exception as e:
|
86 |
+
return f"Error analyzing sentiment: {e}", None
|
87 |
+
|
88 |
+
# Function to generate AI response & analyze sentiment
|
89 |
+
def chatbot_response(user_prompt):
|
90 |
+
if not user_prompt:
|
91 |
+
return None, None, None
|
92 |
+
|
93 |
+
try:
|
94 |
+
# AI Response from Gemini
|
95 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
96 |
+
ai_response = model.generate_content(user_prompt)
|
97 |
+
|
98 |
+
# Sentiment Analysis
|
99 |
+
sentiment_label, confidence = analyze_sentiment(user_prompt)
|
100 |
+
|
101 |
+
return ai_response.text, sentiment_label, confidence
|
102 |
+
except Exception as e:
|
103 |
+
return f"β Error: {e}", None, None
|
chatbot.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import streamlit as st
|
3 |
import google.generativeai as genai
|
4 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
|
|
5 |
|
6 |
# π Fetch API key from Hugging Face Secrets
|
7 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
@@ -14,13 +15,16 @@ else:
|
|
14 |
# Correct Model Path
|
15 |
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
16 |
|
17 |
-
# Load Sentiment Analysis Model
|
18 |
try:
|
19 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
20 |
sentiment_pipeline = pipeline("sentiment-analysis", model=MODEL_NAME, tokenizer=tokenizer)
|
21 |
except Exception as e:
|
22 |
st.error(f"β Error loading sentiment model: {e}")
|
23 |
|
|
|
|
|
|
|
24 |
# Function to analyze sentiment
|
25 |
def analyze_sentiment(text):
|
26 |
try:
|
@@ -38,10 +42,18 @@ def analyze_sentiment(text):
|
|
38 |
except Exception as e:
|
39 |
return f"Error analyzing sentiment: {e}", None
|
40 |
|
41 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
def chatbot_response(user_prompt):
|
43 |
if not user_prompt:
|
44 |
-
return None, None, None
|
45 |
|
46 |
try:
|
47 |
# AI Response from Gemini
|
@@ -51,6 +63,9 @@ def chatbot_response(user_prompt):
|
|
51 |
# Sentiment Analysis
|
52 |
sentiment_label, confidence = analyze_sentiment(user_prompt)
|
53 |
|
54 |
-
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
-
return f"β Error: {e}", None, None
|
|
|
2 |
import streamlit as st
|
3 |
import google.generativeai as genai
|
4 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
5 |
+
from keybert import KeyBERT # Topic Extraction
|
6 |
|
7 |
# π Fetch API key from Hugging Face Secrets
|
8 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
|
|
15 |
# Correct Model Path
|
16 |
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
17 |
|
18 |
+
# Load Sentiment Analysis Model
|
19 |
try:
|
20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
21 |
sentiment_pipeline = pipeline("sentiment-analysis", model=MODEL_NAME, tokenizer=tokenizer)
|
22 |
except Exception as e:
|
23 |
st.error(f"β Error loading sentiment model: {e}")
|
24 |
|
25 |
+
# Load KeyBERT for topic extraction
|
26 |
+
kw_model = KeyBERT()
|
27 |
+
|
28 |
# Function to analyze sentiment
|
29 |
def analyze_sentiment(text):
|
30 |
try:
|
|
|
42 |
except Exception as e:
|
43 |
return f"Error analyzing sentiment: {e}", None
|
44 |
|
45 |
+
# Function to extract key topics
|
46 |
+
def extract_topics(text, num_keywords=3):
|
47 |
+
try:
|
48 |
+
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), top_n=num_keywords)
|
49 |
+
return [word[0] for word in keywords] # Return only the keywords
|
50 |
+
except Exception as e:
|
51 |
+
return [f"Error extracting topics: {e}"]
|
52 |
+
|
53 |
+
# Function to generate AI response, analyze sentiment, and extract topics
|
54 |
def chatbot_response(user_prompt):
|
55 |
if not user_prompt:
|
56 |
+
return None, None, None, None
|
57 |
|
58 |
try:
|
59 |
# AI Response from Gemini
|
|
|
63 |
# Sentiment Analysis
|
64 |
sentiment_label, confidence = analyze_sentiment(user_prompt)
|
65 |
|
66 |
+
# Topic Extraction
|
67 |
+
topics = extract_topics(user_prompt)
|
68 |
+
|
69 |
+
return ai_response.text, sentiment_label, confidence, topics
|
70 |
except Exception as e:
|
71 |
+
return f"β Error: {e}", None, None, None
|