import gradio as gr from processors.input_processor import ContentProcessor from core.note_generator import NoteGenerator from core.quiz_generator import QuizGenerator import os from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() # Verify API key is loaded api_key = os.getenv("GROQ_API_KEY") if not api_key: # Try getting from HF secret api_key = os.getenv("GROQ_API_KEY") if not api_key: raise ValueError("GROQ_API_KEY not found in environment variables") processor = ContentProcessor() note_gen = NoteGenerator(api_key) quiz_gen = QuizGenerator(api_key) def process_pdf(pdf_file, num_questions): if pdf_file is None: return "Please upload a PDF file.", "" # Save uploaded file temporarily temp_path = pdf_file.name # Process content documents = processor.process_pdf(temp_path) content = "\n".join([doc.page_content for doc in documents]) # Generate outputs notes = note_gen.generate_notes(content) quiz = quiz_gen.generate_quiz(content, num_questions) return notes, quiz def process_youtube(youtube_url, num_questions): if not youtube_url: return "Please enter a YouTube URL.", "" try: documents = processor.process_youtube(youtube_url) content = "\n".join([doc.page_content for doc in documents]) notes = note_gen.generate_notes(content) quiz = quiz_gen.generate_quiz(content, num_questions) return notes, quiz except Exception as e: error_message = ( "⚠️ Unable to process the video. Common issues:\n" "1. The video might not have captions enabled\n" "2. The video might be private or restricted\n" "3. The URL might be incorrect\n\n" f"Technical details: {str(e)}\n\n" "Please try another YouTube video that has captions enabled." ) return error_message, "" # Create Gradio interface with gr.Blocks(title="AI Teaching Assistant") as demo: gr.Markdown("# AI Teaching Assistant") gr.Markdown("Generate study notes and quizzes from PDFs or YouTube videos") with gr.Tabs(): with gr.TabItem("PDF Processing"): with gr.Row(): with gr.Column(): pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"]) pdf_num_questions = gr.Slider( minimum=1, maximum=10, value=5, step=1, label="Number of Quiz Questions" ) pdf_button = gr.Button("Process PDF") with gr.Row(): with gr.Column(): pdf_notes_output = gr.Textbox(label="Generated Notes", lines=10) with gr.Column(): pdf_quiz_output = gr.Textbox(label="Generated Quiz", lines=10) pdf_button.click( fn=process_pdf, inputs=[pdf_input, pdf_num_questions], outputs=[pdf_notes_output, pdf_quiz_output] ) with gr.TabItem("YouTube Processing"): with gr.Row(): with gr.Column(): youtube_input = gr.Textbox(label="YouTube URL") youtube_num_questions = gr.Slider( minimum=1, maximum=10, value=5, step=1, label="Number of Quiz Questions" ) youtube_button = gr.Button("Process YouTube Video") with gr.Row(): with gr.Column(): youtube_notes_output = gr.Textbox(label="Generated Notes", lines=10) with gr.Column(): youtube_quiz_output = gr.Textbox(label="Generated Quiz", lines=10) youtube_button.click( fn=process_youtube, inputs=[youtube_input, youtube_num_questions], outputs=[youtube_notes_output, youtube_quiz_output] ) if __name__ == "__main__": demo.launch(share=False)