Spaces:
Sleeping
Sleeping
import os | |
import re | |
import streamlit as st | |
import google.generativeai as genai | |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer | |
from db import get_entry_by_index | |
# Configure Gemini API key | |
GEMINI_API_KEY = os.getenv("gemini_api") | |
if GEMINI_API_KEY: | |
genai.configure(api_key=GEMINI_API_KEY) | |
else: | |
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.") | |
# Load pre-trained sentiment analysis model | |
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment" | |
try: | |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) | |
except Exception as e: | |
st.error(f"β Error loading sentiment model: {e}") | |
# Load Topic Extraction Model | |
try: | |
topic_pipeline = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") | |
except Exception as e: | |
st.error(f"β Error loading topic extraction model: {e}") | |
# Predefined topic labels for classification | |
TOPIC_LABELS = [ | |
"Technology", "Politics", "Business", "Sports", "Entertainment", | |
"Health", "Science", "Education", "Finance", "Travel", "Food" | |
] | |
def analyze_sentiment(text): | |
try: | |
result = sentiment_pipeline(text)[0] | |
label = result['label'] | |
score = result['score'] | |
sentiment_mapping = { | |
"LABEL_0": "Negative", | |
"LABEL_1": "Neutral", | |
"LABEL_2": "Positive" | |
} | |
return sentiment_mapping.get(label, "Unknown"), score | |
except Exception as e: | |
return f"Error analyzing sentiment: {e}", None | |
def extract_topic(text): | |
try: | |
result = topic_pipeline(text, TOPIC_LABELS) | |
top_topic = result["labels"][0] | |
confidence = result["scores"][0] | |
return top_topic, confidence | |
except Exception as e: | |
return f"Error extracting topic: {e}", None | |
# Helper function to parse a data entry index from the user's prompt. | |
# It looks for a pattern like "data entry 1" or "entry 1" (case insensitive). | |
def parse_entry_index(prompt): | |
match = re.search(r'(?:data\s+entry|entry)\s+(\d+)', prompt, re.IGNORECASE) | |
if match: | |
try: | |
# Convert to zero-based index. | |
return int(match.group(1)) - 1 | |
except ValueError: | |
return None | |
return None | |
def chatbot_response(user_prompt): | |
if not user_prompt: | |
return None, None, None, None, None | |
try: | |
# Check if the prompt contains a specific data entry request. | |
index = parse_entry_index(user_prompt) | |
if index is not None: | |
# Fetch the specified entry from MongoDB. | |
entry = get_entry_by_index(index) | |
if entry is None: | |
return f"β No entry found for data entry {index+1}.", None, None, None, None | |
entry_text = entry.get("text", "No text available.") | |
# Construct a simple generative prompt. | |
combined_prompt = f"Let's break down this tweet-like MongoDB entry:\n{entry_text}" | |
# Analyze sentiment and topic on the entry's text. | |
sentiment_label, sentiment_confidence = analyze_sentiment(entry_text) | |
topic_label, topic_confidence = extract_topic(entry_text) | |
else: | |
# For any other prompt, use it as is. | |
combined_prompt = user_prompt | |
sentiment_label, sentiment_confidence = analyze_sentiment(user_prompt) | |
topic_label, topic_confidence = extract_topic(user_prompt) | |
# Generate AI response using Gemini with the constructed prompt. | |
model_gen = genai.GenerativeModel("gemini-1.5-pro") | |
ai_response = model_gen.generate_content(combined_prompt) | |
# Return the generative response and the separately computed sentiment and category. | |
return ai_response.text, sentiment_label, sentiment_confidence, topic_label, topic_confidence | |
except Exception as e: | |
return f"β Error: {e}", None, None, None, None | |