File size: 12,490 Bytes
a73dfcc
 
e36f9bd
 
a73dfcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e36f9bd
a73dfcc
 
e36f9bd
a73dfcc
 
 
 
 
 
e8d6059
e36f9bd
 
 
 
 
 
a73dfcc
 
 
 
 
d51e105
 
a73dfcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e36f9bd
a73dfcc
e36f9bd
e8d6059
a73dfcc
e8d6059
 
 
 
e36f9bd
a73dfcc
 
e36f9bd
e8d6059
e36f9bd
e8d6059
 
 
 
e36f9bd
a73dfcc
 
e36f9bd
e8d6059
e36f9bd
e8d6059
 
 
 
e36f9bd
a73dfcc
 
d51e105
a73dfcc
 
 
 
 
 
e36f9bd
a73dfcc
 
 
 
 
 
 
 
 
 
e36f9bd
a73dfcc
e36f9bd
 
a73dfcc
 
 
 
 
 
 
 
 
 
e36f9bd
e8d6059
a73dfcc
 
 
 
 
 
 
 
 
 
 
 
 
e36f9bd
a73dfcc
 
 
e36f9bd
 
a73dfcc
 
 
e36f9bd
a73dfcc
 
 
 
 
e8d6059
 
a73dfcc
 
 
 
 
 
 
e36f9bd
 
 
 
 
 
 
a73dfcc
 
e8d6059
e36f9bd
e8d6059
e36f9bd
 
 
e8d6059
e36f9bd
 
 
 
e8d6059
e36f9bd
e8d6059
a73dfcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d51e105
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
import os
import time
import json
import base64
from datetime import datetime
import gradio as gr
from google import genai
from google.genai import types

# ── Gemini Client Setup ──
api_key = os.environ.get("GEMINI_API_KEY", "")
client = genai.Client(api_key=api_key) if api_key else None

# ── Mock Data & Configuration ──
SUBJECTS = [
    {"id": "math", "label": "Mathematics", "emoji": "📐", "description": "Algebra, Calculus, Geometry & more"},
    {"id": "science", "label": "Science", "emoji": "🔬", "description": "Physics, Chemistry, Biology & more"},
    {"id": "history", "label": "History", "emoji": "🏛️", "description": "World events, civilizations & culture"},
    {"id": "literature", "label": "Literature", "emoji": "📖", "description": "Essays, poetry, novels & analysis"},
    {"id": "geography", "label": "Geography", "emoji": "🌍", "description": "Maps, climate, human geography"},
    {"id": "cs", "label": "Computer Science", "emoji": "💻", "description": "Algorithms, data structures & coding"},
]

SUGGESTIONS = {
    "math": ["Explain the quadratic formula", "What is a derivative?", "How do matrices work?"],
    "science": ["Explain Newton's third law", "What is DNA replication?", "How does nuclear fusion work?"],
    "history": ["What caused WWI?", "Explain the French Revolution", "Who was Genghis Khan?"],
    "literature": ["Analyse Hamlet's soliloquy", "What is a metaphor?", "Explain stream of consciousness"],
    "geography": ["What causes earthquakes?", "Explain plate tectonics", "What is the water cycle?"],
    "cs": ["Explain Big O notation", "What is recursion?", "How does a hash table work?"],
}

def get_greeting():
    h = datetime.now().hour
    if h < 12: return "Good morning 👋"
    elif h < 17: return "Good afternoon 👋"
    else: return "Good evening 👋"

# ── Gemini Stream & Logic Functions ──
def predict(message, history, subject_id, file_input):
    if not client:
        yield "⚠️ Please configure GEMINI_API_KEY in your Hugging Face Space secrets."
        return

    subj = next((s["label"] for s in SUBJECTS if s["id"] == subject_id), "General Studies")
    system_instruction = f"You are an expert AI Tutor specializing in {subj}. Help the student understand concepts clearly."
    
    formatted_contents = []
    if history:
        for turn in history:
            if isinstance(turn, dict):
                formatted_contents.append(types.Content(role=turn.get("role", "user"), parts=[types.Part.from_text(str(turn.get("content", "")))]))
            elif isinstance(turn, (list, tuple)) and len(turn) >= 2:
                if turn[0]: formatted_contents.append(types.Content(role="user", parts=[types.Part.from_text(str(turn[0]))]))
                if turn[1]: formatted_contents.append(types.Content(role="model", parts=[types.Part.from_text(str(turn[1]))]))

    current_parts = [types.Part.from_text(message)]
    
    if file_input is not None:
        try:
            filepath = file_input if isinstance(file_input, str) else getattr(file_input, "name", str(file_input))
            with open(filepath, "rb") as f:
                pdf_bytes = f.read()
            current_parts.append(
                types.Part.from_bytes(
                    data=pdf_bytes,
                    mime_type="application/pdf"
                )
            )
        except Exception as e:
            yield f"Error reading PDF file: {str(e)}"
            return

    formatted_contents.append(types.Content(role="user", parts=current_parts))

    try:
        response_stream = client.models.generate_content_stream(
            model='gemini-2.5-flash',
            contents=formatted_contents,
            config=types.GenerateContentConfig(
                system_instruction=system_instruction,
                temperature=0.7,
            )
        )
        
        partial_message = ""
        for chunk in response_stream:
            if chunk.text:
                partial_message += chunk.text
                yield partial_message
    except Exception as e:
        yield f"An error occurred: {str(e)}"

# ── Dynamic Features Generators ──
def generate_quiz(subject_id):
    if not client: return "API key missing."
    subj = next((s["label"] for s in SUBJECTS if s["id"] == subject_id), "General Studies")
    prompt = f"Generate a 3-question multiple choice quiz about {subj} with answers at the bottom. Format beautifully with markdown."
    try:
        response = client.models.generate_content(model='gemini-2.5-flash', contents=prompt)
        return response.text
    except Exception as e:
        return f"Error generating quiz: {str(e)}"

def generate_notes(history, subject_id):
    if not client: return "API key missing."
    subj = next((s["label"] for s in SUBJECTS if s["id"] == subject_id), "General Studies")
    prompt = f"Based on this study history for {subj}: {str(history)}. Create highly structured, clean summary revision notes with key bullet points."
    try:
        response = client.models.generate_content(model='gemini-2.5-flash', contents=prompt)
        return response.text
    except Exception as e:
        return f"Error generating notes: {str(e)}"

def generate_flashcards(history, subject_id):
    if not client: return "API key missing."
    subj = next((s["label"] for s in SUBJECTS if s["id"] == subject_id), "General Studies")
    prompt = f"Based on this study history for {subj}: {str(history)}. Generate 4 interactive flashcards formatted as 'Question: [Text] | Answer: [Text]'."
    try:
        response = client.models.generate_content(model='gemini-2.5-flash', contents=prompt)
        return response.text
    except Exception as e:
        return f"Error generating flashcards: {str(e)}"

# ── Gradio UI Construction ──
with gr.Blocks() as demo:
    current_subject_id = gr.State(value="math")
    
    with gr.Row():
        # SIDEBAR
        with gr.Column(scale=1, min_width=280):
            gr.Markdown("## ⚡ StudyAI Dashboard")
            greeting_md = gr.Markdown(f"*{get_greeting()}*")
            
            subject_dropdown = gr.Dropdown(
                choices=[(s["label"] + " " + s["emoji"], s["id"]) for s in SUBJECTS],
                value="math",
                label="📚 Select Active Subject",
                interactive=True
            )
            
            gr.Markdown("---")
            gr.Markdown("⏱️ **Focus Pomodoro Timer**")
            gr.HTML("<div style='font-size:24px; font-weight:bold; text-align:center;'>25:00</div>")
            with gr.Row():
                gr.Button("▶️ Start", size="sm")
                gr.Button("⏹️ Reset", size="sm")
            
            gr.Markdown("---")
            gr.Markdown("📝 **Todo Checklist**")
            todo_list = gr.CheckboxGroup(
                choices=["Read textbook chapters", "Complete AI Quiz assignment", "Review Generated Notes"],
                label="Tasks for Session"
            )
            new_todo = gr.Textbox(placeholder="+ Add custom task & press Enter", show_label=False)
            
            def add_todo(task, current_todos):
                if task and task.strip(): 
                    current_todos.append(task.strip())
                return gr.update(choices=current_todos, value=current_todos), ""
            new_todo.submit(add_todo, [new_todo, todo_list], [todo_list, new_todo])

        # MAIN WORKSPACE
        with gr.Column(scale=4):
            with gr.Tabs() as main_tabs:
                
                # TAB 1: Chat
                with gr.Tab("💬 AI Chat Workspace", id="chat_tab"):
                    subject_info_banner = gr.Markdown("### 📐 Mathematics \n*Algebra, Calculus, Geometry & more*")
                    
                    suggestion_box = gr.Radio(
                        choices=SUGGESTIONS["math"], 
                        label="💡 Instant Suggestions (Click to choose, then type or send)",
                        interactive=True
                    )
                    
                    file_upload = gr.File(label="📂 Upload Study Material (PDF up to 50MB)", file_types=[".pdf"])
                    chatbot = gr.Chatbot(height=400)
                    
                    with gr.Row():
                        msg_input = gr.Textbox(placeholder="Ask anything...", scale=4, show_label=False)
                        audio_input = gr.Audio(sources=["microphone"], type="filepath", label="🎙️ Voice", scale=1)
                        submit_btn = gr.Button("Send 🚀", variant="primary", scale=1)

                    def update_subject_view(sub_id):
                        subj_obj = next((s for s in SUBJECTS if s["id"] == sub_id), SUBJECTS[0])
                        banner_text = f"### {subj_obj['emoji']} {subj_obj['label']} \n*{subj_obj['description']}*"
                        new_radio_choices = SUGGESTIONS.get(sub_id, [])
                        return banner_text, gr.update(choices=new_radio_choices, value=None), sub_id, []

                    subject_dropdown.change(
                        update_subject_view, 
                        inputs=[subject_dropdown], 
                        outputs=[subject_info_banner, suggestion_box, current_subject_id, chatbot]
                    )
                    
                    suggestion_box.select(lambda x: x.value, inputs=[suggestion_box], outputs=[msg_input])

                    def handle_audio(audio_path):
                        if audio_path: 
                            return "[Voice Note Uploaded: Analyzing audio content]"
                        return ""
                    audio_input.change(handle_audio, inputs=[audio_input], outputs=[msg_input])

                    def respond(message, chat_history, sub_id, pdf_file):
                        if not message or not message.strip(): 
                            return "", chat_history
                        
                        chat_history = chat_history or []
                        chat_history.append({"role": "user", "metadata": {"title": ""}, "content": message})
                        chat_history.append({"role": "assistant", "metadata": {"title": ""}, "content": ""})
                        
                        raw_history = []
                        for i in range(0, len(chat_history)-2, 2):
                            if i+1 < len(chat_history):
                                raw_history.append((chat_history[i]["content"], chat_history[i+1]["content"]))

                        for chunk in predict(message, raw_history, sub_id, pdf_file):
                            chat_history[-1]["content"] = chunk
                            yield "", chat_history

                    submit_btn.click(respond, [msg_input, chatbot, current_subject_id, file_upload], [msg_input, chatbot])
                    msg_input.submit(respond, [msg_input, chatbot, current_subject_id, file_upload], [msg_input, chatbot])

                # TAB 2: Quiz
                with gr.Tab("🎯 AI Quiz Mode", id="quiz_tab"):
                    quiz_generate_btn = gr.Button("✨ Generate New Custom Quiz via Gemini", variant="secondary")
                    quiz_output = gr.Markdown("*Click the button above to dynamically render an AI quiz.*")
                    quiz_generate_btn.click(generate_quiz, inputs=[current_subject_id], outputs=[quiz_output])

                # TAB 3: Notes
                with gr.Tab("📝 Summary Study Notes", id="notes_tab"):
                    notes_generate_btn = gr.Button("📖 Compile Revision Notes from Chat History", variant="secondary")
                    notes_output = gr.Markdown("*Click above to transform chat threads into notes.*")
                    notes_generate_btn.click(generate_notes, inputs=[chatbot, current_subject_id], outputs=[notes_output])

                # TAB 4: Flashcards
                with gr.Tab("🃏 Active Flashcards", id="flashcards_tab"):
                    flash_generate_btn = gr.Button("🔄 Formulate Flashcards from Chat History", variant="secondary")
                    flash_output = gr.Markdown("*Click above to extract flashcards.*")
                    flash_generate_btn.click(generate_flashcards, inputs=[chatbot, current_subject_id], outputs=[flash_output])

    gr.Markdown("⚡ *Powered by Gemini 2.5 Flash API.*")

if __name__ == "__main__":
    demo.queue()
    demo.launch(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate"))