File size: 4,301 Bytes
c347d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268ef51
 
 
 
 
 
 
 
 
c347d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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)