Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
Β·
b03e8ad
1
Parent(s):
e603b31
chatbot.py push
Browse files
app.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()
|
|
@@ -24,60 +13,33 @@ collection = get_mongo_client()
|
|
| 24 |
st.title("π MongoDB Data Viewer with AI Sentiment Chatbot")
|
| 25 |
|
| 26 |
# Show first 5 rows from MongoDB
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
# Button to show full MongoDB data
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#### **4. Load Sentiment Analysis Model (RoBERTa)**
|
| 41 |
-
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 42 |
|
| 43 |
-
|
| 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 |
-
# Sentiment Analysis
|
| 71 |
-
sentiment_label, confidence = analyze_sentiment(user_prompt)
|
| 72 |
-
|
| 73 |
-
# Display AI Response & Sentiment Analysis
|
| 74 |
-
st.write("### AI Response:")
|
| 75 |
-
st.write(ai_response.text)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
else:
|
| 83 |
st.warning("β οΈ Please enter a question or text for sentiment analysis.")
|
|
|
|
| 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 function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
#### **1. Ensure Data is Inserted Before Display**
|
| 7 |
insert_data_if_empty()
|
|
|
|
| 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**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|
backup.py
CHANGED
|
@@ -4,6 +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 |
|
| 8 |
# π Fetch API key from Hugging Face Secrets
|
| 9 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
|
@@ -20,36 +21,64 @@ insert_data_if_empty()
|
|
| 20 |
collection = get_mongo_client()
|
| 21 |
|
| 22 |
#### **3. Streamlit App to Display Data**
|
| 23 |
-
st.title("π MongoDB Data Viewer with AI Chatbot")
|
| 24 |
|
| 25 |
# Show first 5 rows from MongoDB
|
| 26 |
-
st.subheader("First 5 Rows from Database")
|
| 27 |
-
data = list(collection.find({}, {"_id": 0}).limit(5))
|
| 28 |
|
| 29 |
-
if data:
|
| 30 |
-
st.write(pd.DataFrame(data))
|
| 31 |
-
else:
|
| 32 |
-
st.warning("β οΈ No data found. Try refreshing the app.")
|
| 33 |
|
| 34 |
# Button to show full MongoDB data
|
| 35 |
-
if st.button("Show Complete Data"):
|
| 36 |
-
all_data = list(collection.find({}, {"_id": 0}))
|
| 37 |
-
st.write(pd.DataFrame(all_data))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# User input for chatbot
|
| 43 |
-
user_prompt = st.
|
| 44 |
|
| 45 |
-
if st.button("Get AI Response"):
|
| 46 |
if user_prompt:
|
| 47 |
try:
|
|
|
|
| 48 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
st.write("### AI Response:")
|
| 51 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
st.error(f"β Error: {e}")
|
| 54 |
else:
|
| 55 |
-
st.warning("β οΈ Please enter a question.")
|
|
|
|
|
|
| 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 Hugging Face transformers for sentiment analysis
|
| 8 |
|
| 9 |
# π Fetch API key from Hugging Face Secrets
|
| 10 |
GEMINI_API_KEY = os.getenv("gemini_api")
|
|
|
|
| 21 |
collection = get_mongo_client()
|
| 22 |
|
| 23 |
#### **3. Streamlit App to Display Data**
|
| 24 |
+
st.title("π MongoDB Data Viewer with AI Sentiment Chatbot")
|
| 25 |
|
| 26 |
# Show first 5 rows from MongoDB
|
| 27 |
+
#st.subheader("First 5 Rows from Database")
|
| 28 |
+
#data = list(collection.find({}, {"_id": 0}).limit(5))
|
| 29 |
|
| 30 |
+
#if data:
|
| 31 |
+
# st.write(pd.DataFrame(data))
|
| 32 |
+
#else:
|
| 33 |
+
# st.warning("β οΈ No data found. Try refreshing the app.")
|
| 34 |
|
| 35 |
# Button to show full MongoDB data
|
| 36 |
+
#if st.button("Show Complete Data"):
|
| 37 |
+
# all_data = list(collection.find({}, {"_id": 0}))
|
| 38 |
+
# st.write(pd.DataFrame(all_data))
|
| 39 |
+
|
| 40 |
+
#### **4. Load Sentiment Analysis Model (RoBERTa)**
|
| 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 |
if user_prompt:
|
| 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 |
+
# Sentiment Analysis
|
| 71 |
+
sentiment_label, confidence = analyze_sentiment(user_prompt)
|
| 72 |
+
|
| 73 |
+
# Display AI Response & Sentiment Analysis
|
| 74 |
st.write("### AI Response:")
|
| 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 |
+
|
chatbot.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# π Fetch API key from Hugging Face Secrets
|
| 7 |
+
GEMINI_API_KEY = os.getenv("gemini_api")
|
| 8 |
+
|
| 9 |
+
if GEMINI_API_KEY:
|
| 10 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 11 |
+
else:
|
| 12 |
+
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.")
|
| 13 |
+
|
| 14 |
+
# Load Sentiment Analysis Model (RoBERTa)
|
| 15 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 16 |
+
|
| 17 |
+
# Function to analyze sentiment
|
| 18 |
+
def analyze_sentiment(text):
|
| 19 |
+
sentiment_result = sentiment_pipeline(text)[0]
|
| 20 |
+
label = sentiment_result['label'] # Extract sentiment label (POSITIVE, NEGATIVE, NEUTRAL)
|
| 21 |
+
score = sentiment_result['score'] # Extract confidence score
|
| 22 |
+
|
| 23 |
+
# Convert labels to a readable format
|
| 24 |
+
sentiment_mapping = {
|
| 25 |
+
"LABEL_0": "Negative",
|
| 26 |
+
"LABEL_1": "Neutral",
|
| 27 |
+
"LABEL_2": "Positive"
|
| 28 |
+
}
|
| 29 |
+
return sentiment_mapping.get(label, "Unknown"), score
|
| 30 |
+
|
| 31 |
+
# Function to generate AI response & analyze sentiment
|
| 32 |
+
def chatbot_response(user_prompt):
|
| 33 |
+
if not user_prompt:
|
| 34 |
+
return None, None, None
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
# AI Response from Gemini
|
| 38 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 39 |
+
ai_response = model.generate_content(user_prompt)
|
| 40 |
+
|
| 41 |
+
# Sentiment Analysis
|
| 42 |
+
sentiment_label, confidence = analyze_sentiment(user_prompt)
|
| 43 |
+
|
| 44 |
+
return ai_response.text, sentiment_label, confidence
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"β Error: {e}", None, None
|
db.py
CHANGED
|
@@ -28,6 +28,3 @@ def insert_data_if_empty():
|
|
| 28 |
print("β
Data Inserted into MongoDB!")
|
| 29 |
except Exception as e:
|
| 30 |
print(f"β Error loading dataset: {e}")
|
| 31 |
-
|
| 32 |
-
# Uncomment the below line if running `db.py` independently
|
| 33 |
-
# insert_data_if_empty()
|
|
|
|
| 28 |
print("β
Data Inserted into MongoDB!")
|
| 29 |
except Exception as e:
|
| 30 |
print(f"β Error loading dataset: {e}")
|
|
|
|
|
|
|
|
|