File size: 11,882 Bytes
7d3e983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""
GenAI Specialization - Main Gradio Application
Coursera-style interactive learning platform with live demos
"""

import gradio as gr
import os
import sys
import subprocess
from pathlib import Path
import json
import time
import random
from datetime import datetime

# Try importing optional dependencies
try:
    import torch
    import transformers
    TORCH_AVAILABLE = True
except ImportError:
    TORCH_AVAILABLE = False
    print("⚠️ PyTorch not available - some features disabled")

class GenAICourseApp:
    def __init__(self):
        self.course_root = Path(__file__).parent
        self.course_structure = self.load_course_structure()
        
    def load_course_structure(self):
        """Load course structure from JSON or use default"""
        return {
            "1": {
                "title": "Module 1: Foundations of AI & ML",
                "emoji": "πŸ“š",
                "description": "Master the fundamentals of Machine Learning, Neural Networks, and Deep Learning",
                "lessons": [
                    "Introduction to AI/ML",
                    "Neural Networks Architecture",
                    "Backpropagation Deep Dive",
                    "Gradient Descent Variants",
                    "Bias-Variance Tradeoff",
                    "Model Optimization"
                ],
                "notebooks": [
                    "01_Foundations/01_Introduction_to_AI_ML/notebooks/ml_basics_intro.ipynb",
                    "01_Foundations/02_Neural_Networks_Deep_Dive/notebooks/build_nn_scratch.ipynb",
                    "01_Foundations/03_Model_Optimization/notebooks/bias_variance_tradeoff.ipynb"
                ],
                "color": "#FF6B6B"
            },
            "2": {
                "title": "Module 2: Advanced ML Techniques",
                "emoji": "πŸš€",
                "description": "Explore Reinforcement Learning and Computer Vision",
                "lessons": [
                    "RL Fundamentals",
                    "Q-Learning & Policy Gradients",
                    "CNN Architectures",
                    "Transfer Learning",
                    "Object Detection",
                    "Image Generation"
                ],
                "notebooks": [
                    "02_Advanced_ML_Techniques/01_Reinforcement_Learning/notebooks/q_learning_intro.ipynb",
                    "02_Advanced_ML_Techniques/02_Computer_Vision/notebooks/cnn_from_scratch.ipynb"
                ],
                "color": "#4ECDC4"
            },
            "3": {
                "title": "Module 3: NLP Fundamentals",
                "emoji": "πŸ“",
                "description": "Master Natural Language Processing and Attention Mechanisms",
                "lessons": [
                    "Text Preprocessing",
                    "Word Embeddings",
                    "RNNs & LSTMs",
                    "Attention Mechanism",
                    "Seq2Seq Models",
                    "Transformer Basics"
                ],
                "notebooks": [
                    "03_NLP_Fundamentals/01_NLP_Basics/notebooks/nlp_preprocessing.ipynb",
                    "03_NLP_Fundamentals/02_Sequence_Models/notebooks/attention_visualization.ipynb"
                ],
                "color": "#FFE66D"
            },
            "4": {
                "title": "Module 4: Generative AI Core",
                "emoji": "🎨",
                "description": "Deep dive into Transformers, LLMs, and Generative AI",
                "lessons": [
                    "Introduction to GenAI",
                    "Transformer Architecture",
                    "Self-Attention & Multi-Head",
                    "Positional Encoding",
                    "LLM Fundamentals",
                    "Arguments of LLM"
                ],
                "notebooks": [
                    "04_Generative_AI_Core/02_Transformer_Architecture/notebooks/transformer_from_scratch.ipynb",
                    "04_Generative_AI_Core/03_LLM_Fundamentals/notebooks/llm_parameters_explained.ipynb"
                ],
                "color": "#6B5B95"
            },
            "5": {
                "title": "Module 5: Advanced LLM Techniques",
                "emoji": "⚑",
                "description": "Fine-tuning, RAG, and LLM Optimization",
                "lessons": [
                    "Fine-tuning Strategies",
                    "LoRA & QLoRA",
                    "RAG Architecture",
                    "Retrieval Strategies",
                    "Model Compression",
                    "Faster Inference"
                ],
                "notebooks": [
                    "05_Advanced_LLM_Techniques/01_Fine_Tuning_LLMs/notebooks/lora_finetuning.ipynb",
                    "05_Advanced_LLM_Techniques/02_RAG_Systems/notebooks/rag_pipeline_basic.ipynb",
                    "05_Advanced_LLM_Techniques/03_LLM_Optimization/notebooks/quantization_basics.ipynb"
                ],
                "color": "#F08A5D"
            },
            "6": {
                "title": "Module 6: Practical GenAI",
                "emoji": "🌐",
                "description": "Hands-on projects and deployment strategies",
                "lessons": [
                    "GenAI Applications",
                    "Prompt Engineering",
                    "LangChain & Agents",
                    "Model Deployment",
                    "HF Hub Integration",
                    "Capstone Projects"
                ],
                "notebooks": [
                    "06_Practical_GenAI/01_Leveraging_GenAI/notebooks/langchain_intro.ipynb",
                    "06_Practical_GenAI/02_Model_Training_Deployment/notebooks/deployment_strategies.ipynb"
                ],
                "color": "#88B04B"
            }
        }
    
    def launch_notebook(self, notebook_path):
        """Launch Jupyter notebook"""
        try:
            full_path = self.course_root / notebook_path
            if full_path.exists():
                subprocess.Popen([sys.executable, "-m", "jupyter", "notebook", str(full_path)])
                return f"βœ… Launched: {full_path.name}"
            else:
                return f"❌ Notebook not found: {notebook_path}"
        except Exception as e:
            return f"❌ Error: {str(e)}"
    
    def get_module_overview(self, module_key):
        """Get formatted module overview"""
        module = self.course_structure[module_key]
        
        overview = f"""
        # {module['emoji']} {module['title']}
        
        {module['description']}
        
        ## πŸ“– Lessons
        """
        
        for i, lesson in enumerate(module['lessons'], 1):
            overview += f"\n{i}. {lesson}"
        
        overview += f"""
        
        ## πŸ““ Notebooks
        """
        
        for i, nb in enumerate(module['notebooks'], 1):
            nb_name = Path(nb).name
            overview += f"\n{i}. `{nb_name}`"
        
        return overview
    
    def rag_demo(self, query):
        """Simple RAG demo for HF Space"""
        knowledge_base = {
            "gen ai": "**Generative AI** refers to deep learning models that can generate text, images, code, and more. Popular models include GPT-4, Llama 2, Claude, and Gemini.",
            "llm": "**Large Language Models (LLMs)** are foundation models trained on massive text data (often trillions of tokens). They use transformer architecture and can perform various tasks without task-specific training.",
            "rag": "**Retrieval-Augmented Generation (RAG)** combines retrieval systems with LLMs. It searches a knowledge base for relevant documents and provides them as context to the LLM, enabling accurate, up-to-date responses.",
            "fine tuning": "**Fine-tuning** adapts pre-trained models to specific tasks by continuing training on domain-specific datasets. PEFT methods like LoRA make this efficient by updating only 0.1% of parameters.",
            "transformer": "**Transformers** use self-attention to process sequences in parallel. Key components: multi-head attention, positional encoding, feed-forward networks, and layer normalization.",
            "attention": "**Attention mechanisms** allow models to focus on relevant parts of input. Self-attention computes relationships between all tokens, while cross-attention attends to encoder outputs.",
            "backpropagation": "**Backpropagation** computes gradients using the chain rule, enabling efficient training of neural networks through gradient descent.",
            "gradient descent": "**Gradient descent** optimizes model parameters by moving in the direction of steepest descent. Variants: SGD, Adam, RMSprop, AdaGrad.",
            "lora": "**LoRA (Low-Rank Adaptation)** freezes base weights and injects trainable rank decomposition matrices, reducing trainable parameters by 1000x while maintaining performance."
        }
        
        query_lower = query.lower()
        relevant_info = []
        
        for key, value in knowledge_base.items():
            if key in query_lower:
                relevant_info.append(value)
        
        if relevant_info:
            response = "## πŸ“š Retrieved Information\n\n"
            response += "\n\n".join(relevant_info)
            
            if len(relevant_info) > 1:
                response += "\n\n---\nπŸ’‘ *Multiple relevant documents found*"
        else:
            response = """❓ **I don't have specific information on that topic.**

Try asking about:
- **Gen AI** - Generative AI fundamentals
- **LLM** - Large Language Models  
- **RAG** - Retrieval-Augmented Generation
- **Fine-tuning** - LoRA, PEFT methods
- **Transformer** - Architecture & attention
- **Backpropagation** - Gradient computation
"""
        
        return response

# Create the Gradio interface
def create_interface():
    app = GenAICourseApp()
    
    # Custom CSS
    custom_css = """
    <style>
        .gradio-container {
            font-family: 'Inter', sans-serif;
        }
        h1 {
            background: linear-gradient(90deg, #6B5B95, #F08A5D);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-size: 2.5rem !important;
        }
        .module-card {
            border-radius: 10px;
            padding: 20px;
            margin: 10px 0;
            transition: transform 0.3s;
        }
        .module-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .stat-card {
            text-align: center;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 10px;
        }
    </style>
    """
    
    with gr.Blocks(title="GenAI Specialization", theme=gr.themes.Soft()) as demo:
        gr.HTML(custom_css)
        
        gr.Markdown("""
        # 🧠 Generative AI Specialization
        
        ### *Your Complete Learning Path from Fundamentals to Advanced LLMs*
        
        ---
        """)
        
        # Stats row
        with gr.Row():
            with gr.Column():
                gr.Markdown("""
                <div class="stat-card">
                    <h2>6</h2>
                    <p>Modules</p>
                </div>
                """)
            with gr.Column():
                gr.Markdown("""
                <div class="stat-card">
                    <h2>30+</h2>
                    <p>Lessons</p>
                </div>
                """)
            with gr.Column():
                gr.Markdown("""
                <div class="stat-card">
                    <h2>3</h2>
                    <p>Capstone Projects</p>
                </div>
                """)
            with gr.Column():