import gradio as gr from rag import RAG # Assuming your rag.py is in the same directory import os import time # --- Configuration --- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") if not GOOGLE_API_KEY: raise ValueError("GOOGLE_API_KEY environment variable not set. Please set it before running the app.") COLLECTION_NAME = os.getenv("COLLECTION_NAME", "ca_documents") # --- Initialize RAG System --- try: rag = RAG(GOOGLE_API_KEY, COLLECTION_NAME) except Exception as e: print(f"Fatal Error initializing RAG system: {e}") raise # --- Initial Chat State --- welcome_message = { "role": "assistant", "content": """ 👋 **Welcome to your CA Study Assistant!** """ } initial_chat_history = [welcome_message] # --- Core Functions --- def upload_file(file): if file is None: return """
""" try: start_time = time.time() file_path = file.name success = rag.upload_document(file_path) duration = time.time() - start_time if success: return f""" """ else: return f""" """ except Exception as e: return f""" """ def chat_with_docs(message: str, history: list): if not message or not message.strip(): return "", history history.append({"role": "user", "content": message}) answer = rag.ask_question(message) history.append({"role": "assistant", "content": answer}) return "", history def clear_chat(): return initial_chat_history, "" # --- Load Custom CSS --- with open("style.css") as f: css = f.read() # --- Gradio App Layout --- with gr.Blocks(css=css, title="CA Study Assistant", theme=gr.themes.Base(), mode="auto") as app: with gr.Column(elem_id="app-container"): gr.HTML("""The smartest way to study your materials.
Drag & drop or click below to select a file.