File size: 3,832 Bytes
8078fde
 
 
 
 
 
64d49e2
8078fde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64d49e2
8078fde
 
64d49e2
8078fde
 
 
64d49e2
8078fde
 
 
 
 
 
 
 
 
 
 
 
 
64d49e2
8078fde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64d49e2
8078fde
 
 
 
64d49e2
44b8e54
8078fde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_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:
        return f"Error processing YouTube URL: {str(e)}", ""

# 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()