import streamlit as st from datetime import datetime from ai_classifier import classify_proverb from pymongo import MongoClient import pandas as pd # --- MongoDB Setup & Caching (Best Practice) --- # Use @st.cache_resource to establish the database connection only once. @st.cache_resource def init_connection(): """Initializes a connection to the MongoDB database.""" return MongoClient(st.secrets["MONGO_URI"]) client = init_connection() collection = client["proverbs_and_meanings"]["proverbs"] # Use @st.cache_data to load data only when it changes. @st.cache_data def load_proverbs(): """Retrieves all proverbs, sorted by newest first.""" data = list(collection.find({}, {'_id': 0}).sort("Timestamp", -1)) return pd.DataFrame(data) if data else pd.DataFrame() def save_proverb(proverb, meaning, context): """Saves a new proverb to the database and clears the data cache.""" text_for_classification = proverb + " " + meaning category = classify_proverb(text_for_classification) new_entry = { "Proverb": proverb.strip(), "Meaning": meaning.strip(), "Context": context.strip(), "Category": category, "Timestamp": datetime.now() # Store as a native datetime object } collection.insert_one(new_entry) # Clear the cache so the gallery updates immediately load_proverbs.clear() # --- Streamlit Page Configuration --- st.set_page_config( page_title="తెలుగు సామెతల ఖజానా", page_icon="📜", layout="centered" ) # --- Title & Introduction --- st.title("📜 తెలుగు సామెతల ఖజానా") st.markdown("### మీకు తెలిసిన సామెతను పంచుకోండి ✍️") st.markdown("వెనుక తరాల నుంచి ముందుకు, మనం భద్రపరిద్దాం మన తెలుగు జ్ఞానం.") st.markdown("