Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
Β·
b6af5ee
1
Parent(s):
a51ac4c
change in model
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import google.generativeai as genai # Import Generative AI library
|
|
4 |
import os
|
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 sentiment analysis
|
8 |
|
9 |
# π Fetch API key from Hugging Face Secrets
|
10 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
@@ -20,8 +20,16 @@ insert_data_if_empty()
|
|
20 |
#### **2. MongoDB Connection**
|
21 |
collection = get_mongo_client()
|
22 |
|
23 |
-
#### **3.
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Show first 5 rows from MongoDB
|
27 |
st.subheader("First 5 Rows from Database")
|
@@ -37,38 +45,29 @@ if st.button("Show Complete Data"):
|
|
37 |
all_data = list(collection.find({}, {"_id": 0}))
|
38 |
st.write(pd.DataFrame(all_data))
|
39 |
|
40 |
-
#### **
|
41 |
-
st.subheader("π€ AI Sentiment Analysis
|
42 |
-
|
43 |
-
# Load Hugging Face sentiment analysis model (RoBERTa)
|
44 |
-
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
45 |
|
46 |
# User input for chatbot
|
47 |
-
user_prompt = st.text_input("
|
48 |
|
49 |
-
if st.button("
|
50 |
if user_prompt:
|
51 |
try:
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.write("### Sentiment Analysis Result:")
|
57 |
-
st.write(f"**Sentiment:** {sentiment_result['label']}")
|
58 |
-
st.write(f"**Confidence Score:** {sentiment_result['score']:.4f}")
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
matching_texts = list(collection.find({"sentiment": sentiment_label}, {"_id": 0, "text": 1}).limit(3))
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
else:
|
69 |
-
st.write("No similar sentiment examples found in MongoDB.")
|
70 |
|
71 |
except Exception as e:
|
72 |
st.error(f"β Error: {e}")
|
73 |
else:
|
74 |
-
st.warning("β οΈ Please enter
|
|
|
4 |
import os
|
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 sentiment analysis pipeline
|
8 |
|
9 |
# π Fetch API key from Hugging Face Secrets
|
10 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
|
|
20 |
#### **2. MongoDB Connection**
|
21 |
collection = get_mongo_client()
|
22 |
|
23 |
+
#### **3. Load Sentiment Analysis Model**
|
24 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
25 |
+
|
26 |
+
def analyze_sentiment(text):
|
27 |
+
"""Analyze sentiment using RoBERTa model."""
|
28 |
+
sentiment_result = sentiment_pipeline(text)[0]['label']
|
29 |
+
return sentiment_result # Returns "LABEL_0", "LABEL_1", or "LABEL_2"
|
30 |
+
|
31 |
+
#### **4. Streamlit App to Display Data**
|
32 |
+
st.title("π MongoDB Data Viewer with AI & Sentiment Analysis")
|
33 |
|
34 |
# Show first 5 rows from MongoDB
|
35 |
st.subheader("First 5 Rows from Database")
|
|
|
45 |
all_data = list(collection.find({}, {"_id": 0}))
|
46 |
st.write(pd.DataFrame(all_data))
|
47 |
|
48 |
+
#### **5. AI Chatbot with Sentiment Analysis**
|
49 |
+
st.subheader("π€ AI Chatbot with Sentiment Analysis")
|
|
|
|
|
|
|
50 |
|
51 |
# User input for chatbot
|
52 |
+
user_prompt = st.text_input("Paste text here for AI sentiment analysis:")
|
53 |
|
54 |
+
if st.button("Get AI Response & Sentiment"):
|
55 |
if user_prompt:
|
56 |
try:
|
57 |
+
# Generate AI response
|
58 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
59 |
+
response = model.generate_content(user_prompt)
|
60 |
+
ai_response = response.text
|
|
|
|
|
|
|
61 |
|
62 |
+
# Analyze sentiment
|
63 |
+
sentiment = analyze_sentiment(ai_response)
|
|
|
64 |
|
65 |
+
# Display results
|
66 |
+
st.write("### AI Response:")
|
67 |
+
st.write(ai_response)
|
68 |
+
st.write(f"**Sentiment Analysis:** {sentiment}")
|
|
|
|
|
69 |
|
70 |
except Exception as e:
|
71 |
st.error(f"β Error: {e}")
|
72 |
else:
|
73 |
+
st.warning("β οΈ Please enter a text input.")
|