Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import google.generativeai as genai # Import Generative AI library | |
import os | |
from pymongo import MongoClient | |
from db import insert_data_if_empty, get_mongo_client # Import functions from db.py | |
# π Fetch API key from Hugging Face Secrets | |
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.") | |
#### **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") | |
# User input for chatbot | |
user_prompt = st.text_input("Ask AI something:") | |
if st.button("Get AI Response"): | |
if user_prompt: | |
try: | |
model = genai.GenerativeModel("gemini-pro") | |
response = model.generate_content(user_prompt) | |
st.write("### AI Response:") | |
st.write(response.text) | |
except Exception as e: | |
st.error(f"β Error: {e}") | |
else: | |
st.warning("β οΈ Please enter a question.") | |