KrSharangrav commited on
Commit
b6af5ee
Β·
1 Parent(s): a51ac4c

change in model

Browse files
Files changed (1) hide show
  1. app.py +26 -27
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 model
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. 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")
@@ -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
- #### **4. Sentiment Analysis Chatbot**
41
- st.subheader("πŸ€– AI Sentiment Analysis Chatbot")
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("Enter a text for sentiment analysis:")
48
 
49
- if st.button("Analyze Sentiment"):
50
  if user_prompt:
51
  try:
52
- # Perform sentiment analysis
53
- sentiment_result = sentiment_pipeline(user_prompt)[0]
54
-
55
- # Display sentiment results
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
- # Fetch similar sentiment examples from MongoDB
61
- sentiment_label = sentiment_result["label"].lower()
62
- matching_texts = list(collection.find({"sentiment": sentiment_label}, {"_id": 0, "text": 1}).limit(3))
63
 
64
- if matching_texts:
65
- st.write("### Similar Sentiment Examples from MongoDB:")
66
- for item in matching_texts:
67
- st.write(f"- {item['text']}")
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 some text.")
 
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.")