File size: 8,471 Bytes
72d16f0
 
 
 
 
0a99908
 
 
 
 
 
72d16f0
 
 
 
 
 
 
 
 
 
 
 
 
0a99908
 
 
72d16f0
 
 
 
 
0a99908
72d16f0
 
0a99908
 
72d16f0
 
 
 
 
 
 
 
 
 
 
0a99908
72d16f0
0a99908
 
72d16f0
0a99908
 
 
72d16f0
0a99908
 
72d16f0
 
0a99908
72d16f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a99908
 
72d16f0
 
0a99908
72d16f0
 
 
0a99908
72d16f0
0a99908
72d16f0
0a99908
72d16f0
 
0a99908
72d16f0
0a99908
72d16f0
 
 
 
 
 
0a99908
 
72d16f0
0a99908
72d16f0
0a99908
 
 
72d16f0
 
 
 
 
0a99908
 
72d16f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a99908
72d16f0
 
 
 
 
 
0a99908
72d16f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a99908
72d16f0
 
 
 
0a99908
72d16f0
0a99908
72d16f0
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import os
import tempfile
import random
import requests
from urllib.parse import quote_plus
from PIL import Image, ImageDraw, ImageFont
import pytesseract
from gtts import gTTS
from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
import gradio as gr

# ===========================
# Environment Variables
# ===========================
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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")
GROQ_MODEL = "llama-3.3-70b-versatile"

if not GROQ_API_KEY:
    print("⚠️ WARNING: GROQ_API_KEY is missing. Add it in HuggingFace Secrets!")


# ===========================
# Users Database
# ===========================
users_db = {}
verification_codes = {}


# ===========================
# Authentication Functions
# ===========================
def signup_user(username, email, password, contact):
    if email in users_db:
        return "Email already registered.", ""
    code = str(random.randint(100000, 999999))
    verification_codes[email] = code

    users_db[email] = {
        "username": username,
        "password": password,
        "contact": contact,
        "verified": False
    }

    return "Signup successful! Verification code:", code


def verify_user(email, code):
    if email not in users_db:
        return "Email not registered."
    if users_db[email]["verified"]:
        return "Already verified."

    if verification_codes.get(email) == code:
        users_db[email]["verified"] = True
        verification_codes.pop(email)
        return "Verification successful!"
    return "Invalid verification code."


def login_user(email, password):
    user = users_db.get(email)
    if not user:
        return False, "User not found."
    if not user["verified"]:
        return False, "Account not verified."
    if user["password"] != password:
        return False, "Incorrect password."
    return True, f"Welcome {user['username']}!"


# ===========================
# Groq API Call
# ===========================
def call_groq(prompt, max_tokens=500):
    if not GROQ_API_KEY:
        return "❌ Missing GROQ_API_KEY in environment variables."

    url = "https://api.groq.com/openai/v1/chat/completions"
    headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}

    payload = {
        "model": GROQ_MODEL,
        "messages": [{"role": "user", "content": prompt}],
        "max_tokens": max_tokens
    }

    try:
        r = requests.post(url, json=payload, headers=headers, timeout=20)
        if r.status_code == 200:
            return r.json()["choices"][0]["message"]["content"]
        return f"API Error {r.status_code}: {r.text}"
    except Exception as e:
        return f"API Exception: {e}"


# ===========================
# AI Helper Functions
# ===========================
def generate_mcqs_from_text(text, n=5):
    prompt = f"Create {n} MCQs with 4 options and answers:\n\n{text}"
    return call_groq(prompt)


def answer_text_query(query):
    prompt = f"Answer clearly:\n\n{query}"
    return call_groq(prompt)


# ===========================
# Video Generator
# ===========================
def create_video_simple(text, tts_lang="en", preview=False):
    if not text.strip():
        return None

    tmp = tempfile.gettempdir()

    lines = [l.strip() for l in text.split(".") if l.strip()]
    if preview:
        lines = lines[:1]  # one slide preview

    clips = []

    for i, chunk in enumerate(lines):
        slide_path = os.path.join(tmp, f"slide_{i}.png")

        # Create slide
        img = Image.new("RGB", (480, 320), (255, 182, 193))
        draw = ImageDraw.Draw(img)
        font = ImageFont.load_default()

        draw.text((20, 20), "AI Generated Slide", fill="blue", font=font)
        draw.text((20, 80), chunk[:220], fill="black", font=font)

        img.save(slide_path)

        # Audio
        audio_path = os.path.join(tmp, f"audio_{i}.mp3")
        gTTS(text=chunk, lang=tts_lang).save(audio_path)

        # Clip
        clip = ImageClip(slide_path).set_duration(4)
        clip = clip.set_audio(AudioFileClip(audio_path))
        clips.append(clip)

    output_path = os.path.join(tmp, f"video_preview.mp4" if preview else "video_full.mp4")
    video = concatenate_videoclips(clips)
    video.write_videofile(output_path, fps=24, codec="libx264", audio_codec="aac")

    return output_path


# ===========================
# Notes + Flowchart
# ===========================
def generate_notes_flowchart(text):
    notes = call_groq(f"Summarize into clean notes:\n\n{text}")
    img = Image.new("RGB", (500, 350), (180, 250, 180))
    draw = ImageDraw.Draw(img)
    font = ImageFont.load_default()

    y = 20
    for line in notes.split("\n"):
        draw.text((10, y), line[:60], fill="black", font=font)
        y += 18
        if y > 300:
            break
    return notes, img


# ===========================
# Quick Search Links
# ===========================
def get_links(topic):
    yt = f"https://www.youtube.com/results?search_query={quote_plus(topic)}"
    gg = f"https://www.google.com/search?q={quote_plus(topic)}"
    return yt, gg


# ===========================
# UI
# ===========================
with gr.Blocks(title="AI Study Wizard") as demo:

    gr.Markdown("# 🎓 AI Study Wizard — Full Assistant")

    # Signup
    with gr.Tab("Sign Up"):
        name = gr.Textbox(label="Name")
        email = gr.Textbox(label="Email")
        pwd = gr.Textbox(label="Password", type="password")
        contact = gr.Textbox(label="Contact")
        btn = gr.Button("Create Account")
        msg = gr.Textbox(label="Message")
        code_out = gr.Textbox(label="Verification Code (demo)")
        btn.click(signup_user, [name, email, pwd, contact], [msg, code_out])

    # Verify
    with gr.Tab("Verify"):
        v_email = gr.Textbox(label="Email")
        v_code = gr.Textbox(label="Code")
        v_btn = gr.Button("Verify")
        v_msg = gr.Textbox(label="Status")
        v_btn.click(verify_user, [v_email, v_code], v_msg)

    # Login
    with gr.Tab("Login"):
        l_email = gr.Textbox(label="Email")
        l_pass = gr.Textbox(label="Password", type="password")
        l_btn = gr.Button("Login")
        l_msg = gr.Textbox(label="Message")
        l_btn.click(lambda e, p: login_user(e, p), [l_email, l_pass], l_msg)

    # Ask Question
    with gr.Tab("Ask AI"):
        q = gr.Textbox(label="Your Question")
        ans = gr.Textbox(label="Answer", lines=8)
        mcq = gr.Textbox(label="MCQs", lines=8)
        pv = gr.Video(label="Preview Video")
        fv = gr.Video(label="Full Video")
        b = gr.Button("Generate")

        def process_query(q):
            a = answer_text_query(q)
            m = generate_mcqs_from_text(a)
            prev = create_video_simple(a, preview=True)
            full = create_video_simple(a, preview=False)
            return a, m, prev, full

        b.click(process_query, q, [ans, mcq, pv, fv])

    # Upload Notes
    with gr.Tab("Upload Image"):
        file = gr.File(label="Upload Image")
        lang = gr.Dropdown(["en", "ur", "hi"], value="en")
        out_text = gr.Textbox(label="Extracted Text", lines=6)
        out_mcq = gr.Textbox(label="MCQs", lines=6)
        out_notes = gr.Textbox(label="Notes", lines=6)
        out_flow = gr.Image(label="Flowchart")
        out_prev = gr.Video(label="Preview Video")
        out_full = gr.Video(label="Full Video")

        def process_upload(f, language):
            try:
                text = pytesseract.image_to_string(Image.open(f.name))
            except:
                text = ""

            mcqs = generate_mcqs_from_text(text) if text else ""
            notes, img = generate_notes_flowchart(text) if text else ("", None)
            prev = create_video_simple(text, tts_lang=language, preview=True) if text else None
            full = create_video_simple(text, tts_lang=language, preview=False) if text else None

            return text, mcqs, notes, img, prev, full

        btn = gr.Button("Process")
        btn.click(process_upload, [file, lang],
                  [out_text, out_mcq, out_notes, out_flow, out_prev, out_full])

    # Search Links
    with gr.Tab("Search"):
        topic = gr.Textbox(label="Topic")
        yt = gr.Textbox(label="YouTube", interactive=False)
        gg = gr.Textbox(label="Google", interactive=False)
        btn = gr.Button("Search")
        btn.click(get_links, topic, [yt, gg])

demo.launch()