KrSharangrav commited on
Commit
e94ec88
Β·
1 Parent(s): 58c2482

change in app.py and backup.py

Browse files
Files changed (2) hide show
  1. app.py +22 -1
  2. backup.py +14 -45
app.py CHANGED
@@ -2,6 +2,7 @@ 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
  #### **1. Ensure Data is Inserted Before Display**
7
  insert_data_if_empty()
@@ -10,7 +11,7 @@ insert_data_if_empty()
10
  collection = get_mongo_client()
11
 
12
  #### **3. Streamlit App to Display Data**
13
- st.title("πŸ“Š MongoDB Data Viewer")
14
 
15
  # Show first 5 rows from MongoDB
16
  st.subheader("First 5 Rows from Database")
@@ -25,3 +26,23 @@ else:
25
  if st.button("Show Complete Data"):
26
  all_data = list(collection.find({}, {"_id": 0}))
27
  st.write(pd.DataFrame(all_data))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import google.generativeai as genai # Import Generative AI library
6
 
7
  #### **1. Ensure Data is Inserted Before Display**
8
  insert_data_if_empty()
 
11
  collection = get_mongo_client()
12
 
13
  #### **3. Streamlit App to Display Data**
14
+ st.title("πŸ“Š MongoDB Data Viewer with AI Chatbot")
15
 
16
  # Show first 5 rows from MongoDB
17
  st.subheader("First 5 Rows from Database")
 
26
  if st.button("Show Complete Data"):
27
  all_data = list(collection.find({}, {"_id": 0}))
28
  st.write(pd.DataFrame(all_data))
29
+
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="your-api-key") # 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
+ response = model.generate_content(user_prompt)
45
+ st.write("### AI Response:")
46
+ st.write(response.text)
47
+ else:
48
+ st.warning("⚠️ Please enter a question.")
backup.py CHANGED
@@ -1,58 +1,27 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import requests
4
- import io
5
  from pymongo import MongoClient
6
- from transformers import pipeline
7
 
8
- #### **1. MongoDB Connection**
9
- def get_mongo_client():
10
- client = MongoClient("mongodb+srv://groupA:[email protected]/?retryWrites=true&w=majority&appName=SentimentCluster")
11
- db = client["sentiment_db"]
12
- return db["tweets"]
13
 
 
14
  collection = get_mongo_client()
15
 
16
- #### **2. Load Dataset from Hugging Face**
17
- csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"
18
-
19
- try:
20
- response = requests.get(csv_url)
21
- response.raise_for_status() # Ensure the request was successful
22
- df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")
23
- st.success("Dataset Loaded Successfully!")
24
- except Exception as e:
25
- st.error(f"Error loading dataset: {e}")
26
- st.stop()
27
-
28
- #### **3. Sentiment Analysis using BERT-ROBERTA**
29
- st.info("Running Sentiment Analysis...")
30
-
31
- sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
32
-
33
- # Function to analyze sentiment
34
- def analyze_sentiment(text):
35
- return sentiment_pipeline(text)[0]['label']
36
-
37
- df["sentiment"] = df["text"].apply(analyze_sentiment)
38
-
39
- #### **4. Upload Data to MongoDB**
40
- collection.delete_many({}) # Optional: Clear existing data before inserting
41
- collection.insert_many(df.to_dict("records"))
42
- st.success("Data Uploaded to MongoDB!")
43
-
44
- #### **5. Build Streamlit Dashboard**
45
- st.title("πŸ“Š Sentiment Analysis Dashboard")
46
 
47
  # Show first 5 rows from MongoDB
48
  st.subheader("First 5 Rows from Database")
49
  data = list(collection.find({}, {"_id": 0}).limit(5))
50
- st.write(pd.DataFrame(data))
51
-
52
- # Buttons to display more data
53
- if st.button("Show Complete Data"):
54
- st.write(df)
55
 
56
- if st.button("Show MongoDB Data"):
57
- data = list(collection.find({}, {"_id": 0}))
58
  st.write(pd.DataFrame(data))
 
 
 
 
 
 
 
 
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
+ #### **1. Ensure Data is Inserted Before Display**
7
+ insert_data_if_empty()
 
 
 
8
 
9
+ #### **2. MongoDB Connection**
10
  collection = get_mongo_client()
11
 
12
+ #### **3. Streamlit App to Display Data**
13
+ st.title("πŸ“Š MongoDB Data Viewer")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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))