File size: 1,451 Bytes
ad9bf8d
 
af09235
e94ec88
5628a29
af09235
e94ec88
 
af09235
e94ec88
ad9bf8d
af09235
e94ec88
5628a29
af09235
ad9bf8d
 
 
af09235
e94ec88
af09235
e94ec88
 
 
 
 
 
 
5628a29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import streamlit as st
import pandas as pd
from pymongo import MongoClient
from db import insert_data_if_empty, get_mongo_client  # Import functions from db.py
import google.generativeai as genai  # Import Generative AI library

#### **1. Ensure Data is Inserted Before Display**
insert_data_if_empty()

#### **2. MongoDB Connection**
collection = get_mongo_client()

#### **3. Streamlit App to Display Data**
st.title("📊 MongoDB Data Viewer with AI Chatbot")

# Show first 5 rows from MongoDB
st.subheader("First 5 Rows from Database")
data = list(collection.find({}, {"_id": 0}).limit(5))

if data:
    st.write(pd.DataFrame(data))
else:
    st.warning("⚠️ No data found. Try refreshing the app.")

# Button to show full MongoDB data
if st.button("Show Complete Data"):
    all_data = list(collection.find({}, {"_id": 0}))
    st.write(pd.DataFrame(all_data))

#### **4. GenAI Chatbot Interface**
st.subheader("🤖 AI Chatbot")

# Configure Generative AI model (replace `your-api-key` with actual key)
genai.configure(api_key="secrets")  # Set up your API key

# Initialize model
model = genai.GenerativeModel("gemini-pro")

# User input for chatbot
user_prompt = st.text_input("Ask AI something:")

if st.button("Get AI Response"):
    if user_prompt:
        response = model.generate_content(user_prompt)
        st.write("### AI Response:")
        st.write(response.text)
    else:
        st.warning("⚠️ Please enter a question.")