Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
Β·
38207ff
1
Parent(s):
5b2cd3f
integration
Browse files- app.py +29 -10
- backup.py +17 -10
- requirements.txt +1 -1
app.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,7 +21,7 @@ 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")
|
@@ -36,20 +37,38 @@ if st.button("Show Complete Data"):
|
|
36 |
all_data = list(collection.find({}, {"_id": 0}))
|
37 |
st.write(pd.DataFrame(all_data))
|
38 |
|
39 |
-
#### **4.
|
40 |
-
st.subheader("π€ AI Chatbot")
|
|
|
|
|
|
|
41 |
|
42 |
# User input for chatbot
|
43 |
-
user_prompt = st.text_input("
|
44 |
|
45 |
-
if st.button("
|
46 |
if user_prompt:
|
47 |
try:
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
except Exception as e:
|
53 |
st.error(f"β Error: {e}")
|
54 |
else:
|
55 |
-
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 model
|
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")
|
|
|
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.")
|
backup.py
CHANGED
@@ -1,8 +1,17 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
3 |
from pymongo import MongoClient
|
4 |
from db import insert_data_if_empty, get_mongo_client # Import functions from db.py
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
#### **1. Ensure Data is Inserted Before Display**
|
8 |
insert_data_if_empty()
|
@@ -30,19 +39,17 @@ if st.button("Show Complete Data"):
|
|
30 |
#### **4. GenAI Chatbot Interface**
|
31 |
st.subheader("π€ AI Chatbot")
|
32 |
|
33 |
-
# Configure Generative AI model (replace `your-api-key` with actual key)
|
34 |
-
genai.configure(api_key="secrets") # Set up your API key
|
35 |
-
|
36 |
-
# Initialize model
|
37 |
-
model = genai.GenerativeModel("gemini-pro")
|
38 |
-
|
39 |
# User input for chatbot
|
40 |
user_prompt = st.text_input("Ask AI something:")
|
41 |
|
42 |
if st.button("Get AI Response"):
|
43 |
if user_prompt:
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
else:
|
48 |
st.warning("β οΈ Please enter a question.")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
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")
|
10 |
+
|
11 |
+
if GEMINI_API_KEY:
|
12 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
13 |
+
else:
|
14 |
+
st.error("β οΈ Google API key is missing! Set it in Hugging Face Secrets.")
|
15 |
|
16 |
#### **1. Ensure Data is Inserted Before Display**
|
17 |
insert_data_if_empty()
|
|
|
39 |
#### **4. GenAI Chatbot Interface**
|
40 |
st.subheader("π€ AI Chatbot")
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# User input for chatbot
|
43 |
user_prompt = st.text_input("Ask AI something:")
|
44 |
|
45 |
if st.button("Get AI Response"):
|
46 |
if user_prompt:
|
47 |
+
try:
|
48 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
49 |
+
response = model.generate_content(user_prompt)
|
50 |
+
st.write("### AI Response:")
|
51 |
+
st.write(response.text)
|
52 |
+
except Exception as e:
|
53 |
+
st.error(f"β Error: {e}")
|
54 |
else:
|
55 |
st.warning("β οΈ Please enter a question.")
|
requirements.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
pandas==1.5.3
|
2 |
pandasai==1.4.1
|
3 |
pymongo==4.3.3
|
4 |
-
transformers==4.
|
5 |
torch==2.1.0
|
6 |
streamlit==1.30.0
|
7 |
sentencepiece==0.1.99
|
|
|
1 |
pandas==1.5.3
|
2 |
pandasai==1.4.1
|
3 |
pymongo==4.3.3
|
4 |
+
transformers==4.49.0
|
5 |
torch==2.1.0
|
6 |
streamlit==1.30.0
|
7 |
sentencepiece==0.1.99
|