Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
Β·
5a94c8e
1
Parent(s):
f763dd0
changes in the topic extraction model
Browse files- app.py +7 -8
- chatbot.py +25 -15
app.py
CHANGED
|
@@ -26,24 +26,23 @@ collection = get_mongo_client()
|
|
| 26 |
# all_data = list(collection.find({}, {"_id": 0}))
|
| 27 |
# st.write(pd.DataFrame(all_data))
|
| 28 |
|
| 29 |
-
#### **4. AI Chatbot with Sentiment
|
| 30 |
-
st.subheader("π€ AI Chatbot with Sentiment
|
| 31 |
|
| 32 |
# User input for chatbot
|
| 33 |
-
user_prompt = st.text_area("Ask AI something or paste text for
|
| 34 |
|
| 35 |
if st.button("Analyze Sentiment & Get AI Response"):
|
| 36 |
-
ai_response, sentiment_label,
|
| 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} ({
|
| 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.")
|
|
|
|
| 26 |
# all_data = list(collection.find({}, {"_id": 0}))
|
| 27 |
# st.write(pd.DataFrame(all_data))
|
| 28 |
|
| 29 |
+
#### **4. AI Chatbot with Sentiment & Topic Analysis**
|
| 30 |
+
st.subheader("π€ AI Chatbot with Sentiment & Topic Analysis")
|
| 31 |
|
| 32 |
# User input for chatbot
|
| 33 |
+
user_prompt = st.text_area("Ask AI something or paste text for analysis:")
|
| 34 |
|
| 35 |
if st.button("Analyze Sentiment & Get AI Response"):
|
| 36 |
+
ai_response, sentiment_label, sentiment_confidence, topic_label, topic_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} ({sentiment_confidence:.2f} confidence)")
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
st.write("### Topic Extraction:")
|
| 46 |
+
st.write(f"**Detected Topic:** {topic_label} ({topic_confidence:.2f} confidence)")
|
| 47 |
else:
|
| 48 |
st.warning("β οΈ Please enter a question or text for analysis.")
|
chatbot.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 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")
|
|
@@ -12,7 +11,7 @@ if GEMINI_API_KEY:
|
|
| 12 |
else:
|
| 13 |
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 17 |
|
| 18 |
# Load Sentiment Analysis Model
|
|
@@ -22,8 +21,17 @@ try:
|
|
| 22 |
except Exception as e:
|
| 23 |
st.error(f"β Error loading sentiment model: {e}")
|
| 24 |
|
| 25 |
-
# Load
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Function to analyze sentiment
|
| 29 |
def analyze_sentiment(text):
|
|
@@ -42,18 +50,20 @@ def analyze_sentiment(text):
|
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error analyzing sentiment: {e}", None
|
| 44 |
|
| 45 |
-
# Function to extract
|
| 46 |
-
def
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return
|
| 52 |
|
| 53 |
-
# Function to generate AI response,
|
| 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
|
|
@@ -61,11 +71,11 @@ def chatbot_response(user_prompt):
|
|
| 61 |
ai_response = model.generate_content(user_prompt)
|
| 62 |
|
| 63 |
# Sentiment Analysis
|
| 64 |
-
sentiment_label,
|
| 65 |
|
| 66 |
# Topic Extraction
|
| 67 |
-
|
| 68 |
|
| 69 |
-
return ai_response.text, sentiment_label,
|
| 70 |
except Exception as e:
|
| 71 |
-
return f"β Error: {e}", None, None, None
|
|
|
|
| 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")
|
|
|
|
| 11 |
else:
|
| 12 |
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.")
|
| 13 |
|
| 14 |
+
# Model for Sentiment Analysis
|
| 15 |
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 16 |
|
| 17 |
# Load Sentiment Analysis Model
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
st.error(f"β Error loading sentiment model: {e}")
|
| 23 |
|
| 24 |
+
# Load Topic Extraction Model
|
| 25 |
+
try:
|
| 26 |
+
topic_pipeline = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
st.error(f"β Error loading topic extraction model: {e}")
|
| 29 |
+
|
| 30 |
+
# Predefined topic labels for classification
|
| 31 |
+
TOPIC_LABELS = [
|
| 32 |
+
"Technology", "Politics", "Business", "Sports", "Entertainment",
|
| 33 |
+
"Health", "Science", "Education", "Finance", "Travel", "Food"
|
| 34 |
+
]
|
| 35 |
|
| 36 |
# Function to analyze sentiment
|
| 37 |
def analyze_sentiment(text):
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
return f"Error analyzing sentiment: {e}", None
|
| 52 |
|
| 53 |
+
# Function to extract topic
|
| 54 |
+
def extract_topic(text):
|
| 55 |
try:
|
| 56 |
+
topic_result = topic_pipeline(text, TOPIC_LABELS)
|
| 57 |
+
top_topic = topic_result["labels"][0] # Get the highest confidence topic
|
| 58 |
+
confidence = topic_result["scores"][0] # Confidence score for the topic
|
| 59 |
+
return top_topic, confidence
|
| 60 |
except Exception as e:
|
| 61 |
+
return f"Error extracting topic: {e}", None
|
| 62 |
|
| 63 |
+
# Function to generate AI response, sentiment, and topic
|
| 64 |
def chatbot_response(user_prompt):
|
| 65 |
if not user_prompt:
|
| 66 |
+
return None, None, None, None, None
|
| 67 |
|
| 68 |
try:
|
| 69 |
# AI Response from Gemini
|
|
|
|
| 71 |
ai_response = model.generate_content(user_prompt)
|
| 72 |
|
| 73 |
# Sentiment Analysis
|
| 74 |
+
sentiment_label, sentiment_confidence = analyze_sentiment(user_prompt)
|
| 75 |
|
| 76 |
# Topic Extraction
|
| 77 |
+
topic_label, topic_confidence = extract_topic(user_prompt)
|
| 78 |
|
| 79 |
+
return ai_response.text, sentiment_label, sentiment_confidence, topic_label, topic_confidence
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"β Error: {e}", None, None, None, None
|