File size: 5,933 Bytes
570019f
 
 
 
 
 
2c568d7
 
570019f
 
 
f9a73cb
570019f
 
5e45a4d
 
 
 
 
 
 
 
 
570019f
 
 
 
 
 
 
5e45a4d
 
 
 
 
 
 
 
570019f
 
 
 
 
 
 
5e45a4d
 
 
 
570019f
 
 
 
 
 
 
 
 
 
 
 
 
 
5e45a4d
570019f
5e45a4d
 
 
 
 
 
 
 
570019f
 
 
 
 
b471731
2c568d7
 
 
 
 
 
 
 
 
 
570019f
 
2c568d7
 
 
b471731
2c568d7
 
 
 
 
 
 
570019f
2c568d7
5e45a4d
2c568d7
 
5e45a4d
2c568d7
570019f
 
 
2c568d7
 
 
 
b471731
 
570019f
2c568d7
570019f
5e45a4d
 
 
 
 
 
 
 
 
 
 
 
570019f
13b7a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570019f
5e45a4d
 
 
13b7a86
 
 
 
 
570019f
 
5e45a4d
 
 
13b7a86
 
 
570019f
13b7a86
 
570019f
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import gradio as gr
import google.generativeai as genai
from dotenv import load_dotenv
from pytube import YouTube
from PyPDF2 import PdfReader
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
from urllib.parse import urlparse, parse_qs

load_dotenv()
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
model = genai.GenerativeModel("gemini-2.5-flash")

def explain_topic(topic):
    prompt = f"""
I want to study '{topic}'. Please explain this topic clearly as if I am a student.

Instructions:
- Use Markdown formatting
- Include **headings**, **bullet points**, and **examples**
- Keep it concise but deep
- Add **tips to remember** at the end
"""
    try:
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error: {e}"

def generate_flashcards(topic):
    prompt = f"""
Create 5 flashcards in Q&A format for the topic **'{topic}'**.
Return the flashcards using:
- Markdown formatting
- Numbered list
- Bold questions
- Regular answers
"""
    try:
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error: {e}"

def follow_up_questions(topic):
    prompt = f"""
List 5 thoughtful follow-up questions for a student after learning about **'{topic}'**.
Use Markdown bullet points.
"""
    try:
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error: {e}"

def summarize_pdf(pdf_file):
    try:
        reader = PdfReader(pdf_file)
        text = ""
        for page in reader.pages[:5]:
            text += page.extract_text() or ""

        if not text.strip():
            return "❌ Could not extract text from the PDF."

        prompt = f"""
Summarize the following academic PDF content in Markdown format:
- Provide bullet-point summary
- Organize into sections
- Emphasize key concepts

Content:\n\n{text[:5000]}
"""
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error reading PDF: {e}"


def get_video_id(url):
    try:
        parsed_url = urlparse(url)
        if parsed_url.hostname in ['www.youtube.com', 'youtube.com']:
            return parse_qs(parsed_url.query)['v'][0]
        elif parsed_url.hostname == 'youtu.be':
            return parsed_url.path[1:]
    except Exception:
        return None

def summarize_youtube(url):
    try:
        video_id = get_video_id(url)
        if not video_id:
            return "❌ Invalid YouTube URL."

        # Fetch English transcript
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        transcript = None
        try:
            transcript = transcript_list.find_transcript(['en'])  # Try manual transcript first
        except NoTranscriptFound:
            transcript = transcript_list.find_transcript(['en']).fetch()  # Try auto-generated

        transcript_text = " ".join([t['text'] for t in transcript.fetch()])

        if len(transcript_text.strip()) == 0:
            return "❌ Transcript is empty."

        prompt = f"Summarize the key educational points in the following YouTube video transcript:\n\n{transcript_text[:5000]} in markdown format and Organize into sections."
        response = model.generate_content(prompt)
        return response.text

    except TranscriptsDisabled:
        return "❌ Transcripts are disabled for this video."
    except NoTranscriptFound:
        return "❌ No English transcript available for this video."
    except Exception as e:
        return f"❌ Error summarizing video: {e}"


with gr.Blocks() as demo:
    gr.Markdown("# πŸ“˜ Study Buddy\nYour smart AI learning assistant!")

    with gr.Tab("πŸ“š Study a Topic"):
        topic_input = gr.Textbox(label="πŸ” What do you want to study?", placeholder="Quantum Mechanics, Photosynthesis, Backend Development, Deep Learning")
        with gr.Row():
            explain_btn = gr.Button("πŸ“– Explain")
            flashcard_btn = gr.Button("🧠 Flashcards")
            followup_btn = gr.Button("❓ Follow-up Qs")

        explain_out = gr.Markdown()
        flashcard_out = gr.Markdown()
        followup_out = gr.Markdown()

        def explain_with_status(topic):
            yield "⏳ Generating explanation, please wait..."
            yield explain_topic(topic)
        
        explain_btn.click(fn=explain_with_status, inputs=topic_input, outputs=explain_out)
        def flashcard_with_status(topic):
            yield "⏳ Generating flashcards..."
            yield generate_flashcards(topic)
        
        def followup_with_status(topic):
            yield "⏳ Generating follow-up questions..."
            yield follow_up_questions(topic)
        
        flashcard_btn.click(flashcard_with_status, inputs=topic_input, outputs=flashcard_out)
        followup_btn.click(followup_with_status, inputs=topic_input, outputs=followup_out)
        
    with gr.Tab("πŸ“„ Summarize PDF"):
        pdf_input = gr.File(label="πŸ“Ž Upload a PDF", file_types=[".pdf"])
        pdf_btn = gr.Button("πŸ“‘ Summarize")
        pdf_out = gr.Markdown()
        def summarize_pdf_with_status(pdf_file):
            yield "⏳ Extracting and summarizing PDF..."
            yield summarize_pdf(pdf_file)
        
        pdf_btn.click(summarize_pdf_with_status, inputs=pdf_input, outputs=pdf_out)

    with gr.Tab("πŸŽ₯ Summarize YouTube Video"):
        yt_url = gr.Textbox(label="🎬 YouTube URL")
        yt_btn = gr.Button("πŸ“ Summarize")
        yt_out = gr.Markdown()
        def summarize_youtube_with_status(url):
            yield "⏳ Fetching and summarizing YouTube video..."
            yield summarize_youtube(url)

        
        yt_btn.click(summarize_youtube_with_status, inputs=yt_url, outputs=yt_out)

if __name__ == "__main__":
    demo.launch()