Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
"""
|
2 |
-
BILLION DOLLAR EDUCATION AI -
|
3 |
-
|
|
|
4 |
"""
|
5 |
|
6 |
import gradio as gr
|
@@ -8,363 +9,529 @@ import requests
|
|
8 |
import json
|
9 |
import random
|
10 |
from datasets import load_dataset
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
from datasets import PremiumEducationDatasets
|
16 |
-
|
17 |
-
class EducationAI:
|
18 |
def __init__(self):
|
19 |
-
#
|
20 |
-
self.groq_api_key = "
|
21 |
self.groq_url = "https://api.groq.com/openai/v1/chat/completions"
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
self.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
5. **Key Concept**: Explain the underlying principle
|
39 |
-
|
40 |
-
Be precise like a competition math coach.""",
|
41 |
-
|
42 |
-
"science": """You are a science educator trained on advanced reasoning datasets.
|
43 |
-
ALWAYS use this format:
|
44 |
-
1. **Concept Identification**: What scientific principle applies?
|
45 |
-
2. **Real-World Connection**: How does this relate to everyday life?
|
46 |
-
3. **Detailed Explanation**: Step-by-step reasoning
|
47 |
-
4. **Visual Description**: Describe any diagrams/processes
|
48 |
-
5. **Summary**: Key takeaway in simple terms
|
49 |
-
|
50 |
-
Make complex science accessible but rigorous.""",
|
51 |
-
|
52 |
-
"physics": """You are a physics professor trained on premium problem sets.
|
53 |
-
Structure every response as:
|
54 |
-
1. **Given/Find**: What we know and what we're solving for
|
55 |
-
2. **Physics Principle**: Which law/equation applies
|
56 |
-
3. **Mathematical Setup**: Show the equation setup
|
57 |
-
4. **Calculation**: Step-by-step math
|
58 |
-
5. **Reality Check**: Does the answer make physical sense?
|
59 |
-
|
60 |
-
Think like Richard Feynman - intuition first, then math.""",
|
61 |
-
|
62 |
-
"chemistry": """You are a chemistry tutor with expertise in reaction mechanisms.
|
63 |
-
Follow this pattern:
|
64 |
-
1. **Chemical Analysis**: Identify compounds/reactions involved
|
65 |
-
2. **Mechanism**: Show electron movement and bond changes
|
66 |
-
3. **Step-by-Step Process**: Break down the reaction
|
67 |
-
4. **Balancing**: Ensure conservation of mass/charge
|
68 |
-
5. **Applications**: Where this reaction matters in real life
|
69 |
-
|
70 |
-
Be thorough with chemical reasoning.""",
|
71 |
-
|
72 |
-
"essay": """You are an expert writing coach trained on premium instruction datasets.
|
73 |
-
Structure all writing help as:
|
74 |
-
1. **Thesis Development**: Strong, clear argument
|
75 |
-
2. **Outline Strategy**: Logical flow of ideas
|
76 |
-
3. **Evidence Integration**: How to support claims
|
77 |
-
4. **Style Enhancement**: Improve clarity and flow
|
78 |
-
5. **Final Polish**: Grammar and precision
|
79 |
-
|
80 |
-
Create compelling, well-structured arguments.""",
|
81 |
-
|
82 |
-
"general": """You are an expert tutor trained on diverse premium educational datasets.
|
83 |
-
Always provide:
|
84 |
-
1. **Clear Explanation**: Break down complex concepts
|
85 |
-
2. **Examples**: Concrete illustrations
|
86 |
-
3. **Step-by-Step**: Logical progression
|
87 |
-
4. **Connections**: Link to broader concepts
|
88 |
-
5. **Practice**: Suggest ways to reinforce learning
|
89 |
-
|
90 |
-
Make learning engaging and memorable."""
|
91 |
}
|
|
|
|
|
|
|
92 |
|
93 |
-
def
|
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 |
-
def
|
121 |
-
"""
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
def
|
131 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
-
|
137 |
-
system_prompt = self.subject_prompts.get(subject, self.subject_prompts["general"])
|
138 |
|
139 |
-
#
|
140 |
-
|
141 |
|
142 |
-
#
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
|
148 |
-
- Competition math problems (AMC, AIME, USAMO)
|
149 |
-
- Microsoft Orca mathematical reasoning
|
150 |
-
- Advanced science reasoning from AllenAI
|
151 |
-
- Premium instruction-following examples
|
152 |
|
153 |
-
Use this high-quality training to provide exceptional educational support.
|
154 |
-
Difficulty level requested: {difficulty}
|
155 |
"""
|
156 |
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
headers = {
|
159 |
"Authorization": f"Bearer {self.groq_api_key}",
|
160 |
"Content-Type": "application/json"
|
161 |
}
|
162 |
|
163 |
payload = {
|
164 |
-
"model": "llama3-70b-8192", # Fastest
|
165 |
"messages": [
|
166 |
-
{"role": "system", "content":
|
167 |
{"role": "user", "content": question}
|
168 |
],
|
169 |
-
"temperature": 0.
|
170 |
-
"max_tokens":
|
171 |
-
"top_p": 0.9
|
|
|
172 |
}
|
173 |
|
174 |
try:
|
175 |
-
# Make API call to Groq
|
176 |
response = requests.post(
|
177 |
self.groq_url,
|
178 |
headers=headers,
|
179 |
json=payload,
|
180 |
-
timeout=
|
181 |
)
|
182 |
|
|
|
|
|
|
|
183 |
if response.status_code == 200:
|
184 |
result = response.json()
|
185 |
answer = result["choices"][0]["message"]["content"]
|
186 |
|
187 |
-
# Add
|
188 |
-
|
|
|
|
|
|
|
189 |
|
190 |
-
return
|
191 |
else:
|
192 |
-
return f"
|
193 |
|
194 |
except requests.exceptions.Timeout:
|
195 |
-
return "
|
196 |
-
except requests.exceptions.RequestException as e:
|
197 |
-
return f"🔧 Connection error: {str(e)}"
|
198 |
except Exception as e:
|
199 |
-
return f"
|
200 |
|
201 |
-
def
|
202 |
-
"""
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
• Instruction Following: 50K+ premium examples
|
211 |
-
• **Total Training Examples: 267K+**
|
212 |
|
213 |
-
|
214 |
-
•
|
215 |
-
•
|
216 |
-
•
|
217 |
-
•
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
•
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
|
|
|
|
|
|
|
|
227 |
|
228 |
-
# Initialize
|
229 |
-
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
"""Create the premium education AI interface"""
|
234 |
|
235 |
with gr.Blocks(
|
236 |
-
theme=gr.themes.
|
237 |
-
title="
|
238 |
css="""
|
239 |
-
.gradio-container {
|
240 |
-
max-width: 1200px !important;
|
241 |
-
}
|
242 |
.header {
|
243 |
text-align: center;
|
244 |
-
background: linear-gradient(
|
245 |
-
padding: 2rem;
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
248 |
}
|
249 |
-
.
|
250 |
-
background:
|
251 |
-
border-
|
252 |
-
padding: 1rem;
|
253 |
-
margin: 1rem 0;
|
254 |
}
|
255 |
"""
|
256 |
) as interface:
|
257 |
|
258 |
-
# Header
|
259 |
with gr.Row():
|
260 |
gr.HTML("""
|
261 |
<div class="header">
|
262 |
-
<h1 style="color: white; margin: 0; font-size:
|
263 |
-
<p style="color: #
|
264 |
-
|
265 |
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
</div>
|
267 |
""")
|
268 |
|
269 |
-
# Main
|
270 |
with gr.Row():
|
271 |
-
with gr.Column(scale=
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
with gr.Row():
|
280 |
-
subject_dropdown = gr.Dropdown(
|
281 |
-
choices=["general", "mathematics", "science", "physics", "chemistry", "essay"],
|
282 |
-
label="📚 Subject",
|
283 |
-
value="general",
|
284 |
-
interactive=True
|
285 |
)
|
286 |
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
)
|
293 |
-
|
294 |
-
submit_btn = gr.Button("🚀 Get Expert Help", variant="primary", size="lg")
|
295 |
-
|
296 |
with gr.Column(scale=1):
|
297 |
-
gr.
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
</div>
|
309 |
-
""")
|
310 |
|
311 |
-
#
|
312 |
answer_output = gr.Textbox(
|
313 |
-
label="📖 Expert
|
314 |
-
lines=
|
315 |
max_lines=25,
|
316 |
-
interactive=False
|
|
|
317 |
)
|
318 |
|
319 |
-
#
|
320 |
-
gr.
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
[
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
|
337 |
-
# Event
|
338 |
submit_btn.click(
|
339 |
-
fn=
|
340 |
-
inputs=[question_input, subject_dropdown, difficulty_dropdown],
|
341 |
outputs=answer_output,
|
342 |
-
api_name="predict" #
|
343 |
)
|
344 |
|
345 |
question_input.submit(
|
346 |
-
fn=
|
347 |
-
inputs=[question_input, subject_dropdown, difficulty_dropdown],
|
348 |
outputs=answer_output
|
349 |
)
|
350 |
|
351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
gr.HTML("""
|
353 |
-
<div style="text-align: center; margin-top:
|
354 |
-
<
|
355 |
-
|
356 |
-
|
|
|
|
|
|
|
|
|
|
|
357 |
</div>
|
358 |
""")
|
359 |
|
360 |
return interface
|
361 |
|
362 |
-
# Launch
|
363 |
if __name__ == "__main__":
|
364 |
-
interface =
|
365 |
interface.launch(
|
366 |
server_name="0.0.0.0",
|
367 |
server_port=7860,
|
368 |
share=False,
|
369 |
-
show_error=True
|
|
|
|
|
|
|
370 |
)
|
|
|
1 |
"""
|
2 |
+
BILLION DOLLAR EDUCATION AI - GLOBAL SCALE
|
3 |
+
The ChatGPT for Education - Optimized for Speed, Quality & Scalability
|
4 |
+
Serving millions of students, teachers, and lifelong learners worldwide
|
5 |
"""
|
6 |
|
7 |
import gradio as gr
|
|
|
9 |
import json
|
10 |
import random
|
11 |
from datasets import load_dataset
|
12 |
+
import threading
|
13 |
+
import time
|
14 |
+
import hashlib
|
15 |
+
from typing import Dict, List, Optional
|
16 |
+
import asyncio
|
17 |
+
from concurrent.futures import ThreadPoolExecutor
|
18 |
|
19 |
+
class GlobalEducationAI:
|
|
|
|
|
|
|
20 |
def __init__(self):
|
21 |
+
# Production API configuration
|
22 |
+
self.groq_api_key = "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"
|
23 |
self.groq_url = "https://api.groq.com/openai/v1/chat/completions"
|
24 |
|
25 |
+
# Scalable dataset architecture
|
26 |
+
self.datasets = {}
|
27 |
+
self.example_cache = {} # Speed optimization
|
28 |
+
self.loading_status = "🚀 Initializing Global Education AI..."
|
29 |
+
self.total_examples_loaded = 0
|
30 |
+
|
31 |
+
# Multi-language support for global reach
|
32 |
+
self.supported_languages = ["English", "Spanish", "French", "German", "Chinese", "Japanese"]
|
33 |
+
|
34 |
+
# User analytics for billion-dollar insights
|
35 |
+
self.usage_stats = {
|
36 |
+
"total_queries": 0,
|
37 |
+
"subject_distribution": {},
|
38 |
+
"difficulty_preferences": {},
|
39 |
+
"response_times": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
+
|
42 |
+
# Start optimized dataset loading
|
43 |
+
self.initialize_datasets()
|
44 |
|
45 |
+
def initialize_datasets(self):
|
46 |
+
"""Optimized parallel dataset loading for global scale"""
|
47 |
+
def load_core_datasets():
|
48 |
+
try:
|
49 |
+
self.loading_status = "🔥 Loading Core Math Datasets (Competition + Problem Solving)..."
|
50 |
+
|
51 |
+
# TIER 1: Competition Math (Highest ROI)
|
52 |
+
comp_math = load_dataset("hendrycks/competition_math", split="train[:2000]", trust_remote_code=True)
|
53 |
+
self.datasets['competition_math'] = self.optimize_dataset(comp_math, 'math_competition')
|
54 |
+
|
55 |
+
# TIER 2: Practical Math (Broad Appeal)
|
56 |
+
gsm8k = load_dataset("gsm8k", "main", split="train[:3000]", trust_remote_code=True)
|
57 |
+
self.datasets['gsm8k'] = self.optimize_dataset(gsm8k, 'math_practical')
|
58 |
+
|
59 |
+
self.loading_status = "🧪 Loading Science & Reasoning Datasets..."
|
60 |
+
|
61 |
+
# TIER 3: Science Reasoning (STEM Education)
|
62 |
+
try:
|
63 |
+
science_qa = load_dataset("allenai/openbookqa", split="train[:1000]", trust_remote_code=True)
|
64 |
+
self.datasets['science_qa'] = self.optimize_dataset(science_qa, 'science')
|
65 |
+
except:
|
66 |
+
print("OpenBookQA unavailable, using alternative...")
|
67 |
+
|
68 |
+
# TIER 4: Advanced Reasoning (Microsoft Premium)
|
69 |
+
try:
|
70 |
+
orca_math = load_dataset("microsoft/orca-math-word-problems-200k", split="train[:2000]", trust_remote_code=True)
|
71 |
+
self.datasets['orca_math'] = self.optimize_dataset(orca_math, 'math_advanced')
|
72 |
+
except:
|
73 |
+
print("Orca Math unavailable, continuing with available datasets...")
|
74 |
+
|
75 |
+
# TIER 5: Multi-domain Knowledge (Academic Coverage)
|
76 |
+
try:
|
77 |
+
mmlu = load_dataset("cais/mmlu", "all", split="train[:1000]", trust_remote_code=True)
|
78 |
+
self.datasets['mmlu'] = self.optimize_dataset(mmlu, 'academic')
|
79 |
+
except:
|
80 |
+
print("MMLU unavailable, core datasets sufficient...")
|
81 |
+
|
82 |
+
# Create optimized example cache
|
83 |
+
self.create_example_cache()
|
84 |
+
self.total_examples_loaded = sum(len(cache) for cache in self.example_cache.values())
|
85 |
+
|
86 |
+
self.loading_status = f"✅ GLOBAL EDUCATION AI READY - {len(self.datasets)} datasets, {self.total_examples_loaded:,} examples"
|
87 |
+
print(f"🌍 Global Education AI initialized with {self.total_examples_loaded:,} premium examples")
|
88 |
+
|
89 |
+
except Exception as e:
|
90 |
+
self.loading_status = f"⚠️ Partial initialization - Core functionality available"
|
91 |
+
print(f"Dataset loading info: {e}")
|
92 |
+
|
93 |
+
# Load datasets in background for instant user experience
|
94 |
+
thread = threading.Thread(target=load_core_datasets)
|
95 |
+
thread.daemon = True
|
96 |
+
thread.start()
|
97 |
|
98 |
+
def optimize_dataset(self, dataset, category: str) -> List[Dict]:
|
99 |
+
"""Optimize dataset for speed and quality"""
|
100 |
+
optimized = []
|
101 |
+
|
102 |
+
for item in dataset:
|
103 |
+
try:
|
104 |
+
if category == 'math_competition':
|
105 |
+
if item.get('problem') and item.get('solution') and len(item['problem']) > 20:
|
106 |
+
optimized.append({
|
107 |
+
'question': item['problem'],
|
108 |
+
'solution': item['solution'],
|
109 |
+
'type': item.get('type', 'competition_math'),
|
110 |
+
'difficulty': 'competition',
|
111 |
+
'category': 'mathematics',
|
112 |
+
'quality_score': 10
|
113 |
+
})
|
114 |
+
|
115 |
+
elif category == 'math_practical':
|
116 |
+
if item.get('question') and item.get('answer'):
|
117 |
+
optimized.append({
|
118 |
+
'question': item['question'],
|
119 |
+
'solution': item['answer'],
|
120 |
+
'type': 'word_problem',
|
121 |
+
'difficulty': 'intermediate',
|
122 |
+
'category': 'mathematics',
|
123 |
+
'quality_score': 9
|
124 |
+
})
|
125 |
+
|
126 |
+
elif category == 'science':
|
127 |
+
if item.get('question_stem') and item.get('choices'):
|
128 |
+
choices_text = "\n".join([f"{choice['label']}) {choice['text']}" for choice in item['choices']['text']])
|
129 |
+
optimized.append({
|
130 |
+
'question': f"{item['question_stem']}\n\n{choices_text}",
|
131 |
+
'solution': f"Answer: {item['answerKey']}",
|
132 |
+
'type': 'science_reasoning',
|
133 |
+
'difficulty': 'intermediate',
|
134 |
+
'category': 'science',
|
135 |
+
'quality_score': 8
|
136 |
+
})
|
137 |
+
|
138 |
+
elif category in ['math_advanced', 'academic']:
|
139 |
+
if item.get('question') and item.get('answer'):
|
140 |
+
optimized.append({
|
141 |
+
'question': str(item['question'])[:500], # Truncate for speed
|
142 |
+
'solution': str(item['answer'])[:500],
|
143 |
+
'type': category,
|
144 |
+
'difficulty': 'advanced',
|
145 |
+
'category': 'general',
|
146 |
+
'quality_score': 8
|
147 |
+
})
|
148 |
+
|
149 |
+
except Exception:
|
150 |
+
continue # Skip malformed entries
|
151 |
+
|
152 |
+
return optimized[:500] # Limit for speed while maintaining quality
|
153 |
|
154 |
+
def create_example_cache(self):
|
155 |
+
"""Create subject-specific example cache for instant retrieval"""
|
156 |
+
self.example_cache = {
|
157 |
+
'mathematics': [],
|
158 |
+
'science': [],
|
159 |
+
'physics': [],
|
160 |
+
'chemistry': [],
|
161 |
+
'biology': [],
|
162 |
+
'general': [],
|
163 |
+
'competition': []
|
164 |
+
}
|
165 |
|
166 |
+
for dataset_name, examples in self.datasets.items():
|
167 |
+
for example in examples:
|
168 |
+
category = example.get('category', 'general')
|
169 |
+
if category in self.example_cache:
|
170 |
+
self.example_cache[category].append(example)
|
171 |
+
|
172 |
+
# Add high-quality examples to general cache
|
173 |
+
if example.get('quality_score', 0) >= 9:
|
174 |
+
self.example_cache['general'].append(example)
|
175 |
+
|
176 |
+
# Add competition problems to competition cache
|
177 |
+
if example.get('difficulty') == 'competition':
|
178 |
+
self.example_cache['competition'].append(example)
|
179 |
+
|
180 |
+
def get_optimal_examples(self, question: str, subject: str, difficulty: str, num_examples: int = 2) -> List[Dict]:
|
181 |
+
"""Lightning-fast example retrieval optimized for quality and relevance"""
|
182 |
+
# Map user inputs to cache categories
|
183 |
+
subject_mapping = {
|
184 |
+
'mathematics': 'mathematics',
|
185 |
+
'math': 'mathematics',
|
186 |
+
'science': 'science',
|
187 |
+
'physics': 'science',
|
188 |
+
'chemistry': 'science',
|
189 |
+
'biology': 'science',
|
190 |
+
'general': 'general'
|
191 |
+
}
|
192 |
|
193 |
+
target_subject = subject_mapping.get(subject.lower(), 'general')
|
|
|
194 |
|
195 |
+
# Get examples from cache (instant retrieval)
|
196 |
+
available_examples = self.example_cache.get(target_subject, [])
|
197 |
|
198 |
+
# If requesting competition difficulty, prioritize competition examples
|
199 |
+
if difficulty == 'competition' and self.example_cache.get('competition'):
|
200 |
+
available_examples = self.example_cache['competition'][:50]
|
201 |
+
|
202 |
+
# Filter by difficulty if possible
|
203 |
+
if difficulty in ['advanced', 'competition']:
|
204 |
+
filtered = [ex for ex in available_examples if ex.get('difficulty') in ['advanced', 'competition']]
|
205 |
+
if filtered:
|
206 |
+
available_examples = filtered
|
207 |
+
|
208 |
+
# Return best examples (random selection for variety)
|
209 |
+
if available_examples:
|
210 |
+
return random.sample(available_examples, min(num_examples, len(available_examples)))
|
211 |
+
|
212 |
+
# Fallback to general examples
|
213 |
+
return random.sample(self.example_cache.get('general', []), min(num_examples, len(self.example_cache.get('general', []))))
|
214 |
+
|
215 |
+
def create_premium_prompt(self, question: str, subject: str, difficulty: str) -> str:
|
216 |
+
"""Create optimized prompt with premium dataset examples"""
|
217 |
+
examples = self.get_optimal_examples(question, subject, difficulty)
|
218 |
+
|
219 |
+
if not examples:
|
220 |
+
return f"You are an expert {subject} tutor. Provide detailed, step-by-step solutions."
|
221 |
+
|
222 |
+
# Build efficient few-shot prompt
|
223 |
+
prompt = f"""You are a world-class {subject} educator with access to premium educational datasets.
|
224 |
|
225 |
+
TEACHING METHODOLOGY: Use these high-quality examples from educational datasets as your guide:
|
|
|
|
|
|
|
|
|
226 |
|
|
|
|
|
227 |
"""
|
228 |
|
229 |
+
for i, example in enumerate(examples, 1):
|
230 |
+
prompt += f"EXAMPLE {i} ({example.get('type', 'academic')}):\n"
|
231 |
+
prompt += f"Q: {example['question'][:250]}{'...' if len(example['question']) > 250 else ''}\n"
|
232 |
+
prompt += f"A: {example['solution'][:250]}{'...' if len(example['solution']) > 250 else ''}\n\n"
|
233 |
+
|
234 |
+
prompt += f"""INSTRUCTION: Use the same rigorous, step-by-step approach shown above.
|
235 |
+
- Subject Focus: {subject}
|
236 |
+
- Difficulty Level: {difficulty}
|
237 |
+
- Always show your work and explain reasoning
|
238 |
+
- Make complex concepts accessible
|
239 |
+
- Provide practical applications when relevant
|
240 |
+
|
241 |
+
Now solve the student's question:"""
|
242 |
+
|
243 |
+
return prompt
|
244 |
+
|
245 |
+
def educate(self, question: str, subject: str = "general", difficulty: str = "intermediate", language: str = "English") -> str:
|
246 |
+
"""Main education function optimized for global scale and speed"""
|
247 |
+
|
248 |
+
# Analytics tracking for billion-dollar insights
|
249 |
+
self.usage_stats["total_queries"] += 1
|
250 |
+
self.usage_stats["subject_distribution"][subject] = self.usage_stats["subject_distribution"].get(subject, 0) + 1
|
251 |
+
self.usage_stats["difficulty_preferences"][difficulty] = self.usage_stats["difficulty_preferences"].get(difficulty, 0) + 1
|
252 |
+
|
253 |
+
start_time = time.time()
|
254 |
+
|
255 |
+
if not question.strip():
|
256 |
+
return "🎓 Hello! I'm your AI education assistant. Ask me any academic question and I'll provide detailed, step-by-step explanations using premium educational datasets!"
|
257 |
+
|
258 |
+
# Check system status
|
259 |
+
if "Initializing" in self.loading_status or "Loading" in self.loading_status:
|
260 |
+
return f"🔄 {self.loading_status}\n\nI'm still loading educational datasets. You can ask questions now, but responses will get even better in a moment!"
|
261 |
+
|
262 |
+
# Create optimized prompt with dataset examples
|
263 |
+
system_prompt = self.create_premium_prompt(question, subject, difficulty)
|
264 |
+
|
265 |
+
# Add language instruction for global users
|
266 |
+
if language != "English":
|
267 |
+
system_prompt += f"\n\nIMPORTANT: Respond in {language}."
|
268 |
+
|
269 |
+
# Optimized API request for speed
|
270 |
headers = {
|
271 |
"Authorization": f"Bearer {self.groq_api_key}",
|
272 |
"Content-Type": "application/json"
|
273 |
}
|
274 |
|
275 |
payload = {
|
276 |
+
"model": "llama3-70b-8192", # Fastest model for real-time responses
|
277 |
"messages": [
|
278 |
+
{"role": "system", "content": system_prompt},
|
279 |
{"role": "user", "content": question}
|
280 |
],
|
281 |
+
"temperature": 0.2, # Lower for consistency in education
|
282 |
+
"max_tokens": 1500, # Optimized length
|
283 |
+
"top_p": 0.9,
|
284 |
+
"stream": False # Disable streaming for faster processing
|
285 |
}
|
286 |
|
287 |
try:
|
|
|
288 |
response = requests.post(
|
289 |
self.groq_url,
|
290 |
headers=headers,
|
291 |
json=payload,
|
292 |
+
timeout=15 # Aggressive timeout for speed
|
293 |
)
|
294 |
|
295 |
+
response_time = time.time() - start_time
|
296 |
+
self.usage_stats["response_times"].append(response_time)
|
297 |
+
|
298 |
if response.status_code == 200:
|
299 |
result = response.json()
|
300 |
answer = result["choices"][0]["message"]["content"]
|
301 |
|
302 |
+
# Add premium branding with real stats
|
303 |
+
dataset_count = len(self.datasets)
|
304 |
+
example_count = self.total_examples_loaded
|
305 |
+
|
306 |
+
footer = f"\n\n---\n*🌍 **Global Education AI** powered by {dataset_count} premium datasets ({example_count:,} examples) | Response time: {response_time:.2f}s | Query #{self.usage_stats['total_queries']:,}*"
|
307 |
|
308 |
+
return answer + footer
|
309 |
else:
|
310 |
+
return f"⚠️ Service temporarily unavailable. Please try again in a moment. (Error: {response.status_code})"
|
311 |
|
312 |
except requests.exceptions.Timeout:
|
313 |
+
return "⚡ Response took too long. Please try a more specific question or try again."
|
|
|
|
|
314 |
except Exception as e:
|
315 |
+
return f"🔧 Technical issue occurred. Our team has been notified. Please try again shortly."
|
316 |
|
317 |
+
def get_global_analytics(self) -> str:
|
318 |
+
"""Get analytics for billion-dollar insights"""
|
319 |
+
total_queries = self.usage_stats["total_queries"]
|
320 |
+
avg_response_time = sum(self.usage_stats["response_times"][-100:]) / len(self.usage_stats["response_times"][-100:]) if self.usage_stats["response_times"] else 0
|
321 |
+
|
322 |
+
top_subjects = sorted(self.usage_stats["subject_distribution"].items(), key=lambda x: x[1], reverse=True)[:3]
|
323 |
+
top_difficulties = sorted(self.usage_stats["difficulty_preferences"].items(), key=lambda x: x[1], reverse=True)[:3]
|
324 |
+
|
325 |
+
analytics = f"""📊 **GLOBAL EDUCATION AI ANALYTICS**
|
|
|
|
|
326 |
|
327 |
+
🌍 **Scale Metrics:**
|
328 |
+
• Total Queries Served: {total_queries:,}
|
329 |
+
• Average Response Time: {avg_response_time:.2f}s
|
330 |
+
• Datasets Loaded: {len(self.datasets)}
|
331 |
+
• Examples Available: {self.total_examples_loaded:,}
|
332 |
|
333 |
+
📚 **Popular Subjects:**"""
|
334 |
+
|
335 |
+
for subject, count in top_subjects:
|
336 |
+
analytics += f"\n• {subject.title()}: {count:,} queries"
|
337 |
+
|
338 |
+
analytics += f"\n\n⚡ **Difficulty Distribution:**"
|
339 |
+
for difficulty, count in top_difficulties:
|
340 |
+
analytics += f"\n• {difficulty.title()}: {count:,} requests"
|
341 |
+
|
342 |
+
analytics += f"\n\n🚀 **Status**: {self.loading_status}"
|
343 |
+
|
344 |
+
return analytics
|
345 |
|
346 |
+
# Initialize Global Education AI
|
347 |
+
global_ai = GlobalEducationAI()
|
348 |
|
349 |
+
def create_global_interface():
|
350 |
+
"""Create world-class education interface for global scale"""
|
|
|
351 |
|
352 |
with gr.Blocks(
|
353 |
+
theme=gr.themes.Origin(), # Professional theme
|
354 |
+
title="🌍 Global Education AI - The ChatGPT for Education",
|
355 |
css="""
|
356 |
+
.gradio-container { max-width: 1400px !important; }
|
|
|
|
|
357 |
.header {
|
358 |
text-align: center;
|
359 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
|
360 |
+
padding: 3rem; border-radius: 20px; margin-bottom: 2rem;
|
361 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
|
362 |
+
}
|
363 |
+
.stats-panel {
|
364 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
365 |
+
border-radius: 15px; padding: 1.5rem; margin: 1rem 0;
|
366 |
+
border: 1px solid #e1e8ed;
|
367 |
}
|
368 |
+
.feature-box {
|
369 |
+
background: white; border-radius: 10px; padding: 1rem;
|
370 |
+
margin: 0.5rem 0; border-left: 4px solid #667eea;
|
|
|
|
|
371 |
}
|
372 |
"""
|
373 |
) as interface:
|
374 |
|
375 |
+
# Global Header
|
376 |
with gr.Row():
|
377 |
gr.HTML("""
|
378 |
<div class="header">
|
379 |
+
<h1 style="color: white; margin: 0; font-size: 3em; font-weight: 700;">🌍 Global Education AI</h1>
|
380 |
+
<p style="color: #f0f0f0; margin: 1rem 0 0 0; font-size: 1.3em; font-weight: 300;">
|
381 |
+
The ChatGPT for Education • Powered by Premium Datasets • Serving Millions Worldwide
|
382 |
</p>
|
383 |
+
<div style="margin-top: 1rem;">
|
384 |
+
<span style="background: rgba(255,255,255,0.2); padding: 0.5rem 1rem; border-radius: 20px; margin: 0.25rem; display: inline-block; color: white;">Competition Math</span>
|
385 |
+
<span style="background: rgba(255,255,255,0.2); padding: 0.5rem 1rem; border-radius: 20px; margin: 0.25rem; display: inline-block; color: white;">Advanced Science</span>
|
386 |
+
<span style="background: rgba(255,255,255,0.2); padding: 0.5rem 1rem; border-radius: 20px; margin: 0.25rem; display: inline-block; color: white;">Microsoft Orca</span>
|
387 |
+
<span style="background: rgba(255,255,255,0.2); padding: 0.5rem 1rem; border-radius: 20px; margin: 0.25rem; display: inline-block; color: white;">Multi-Language</span>
|
388 |
+
</div>
|
389 |
</div>
|
390 |
""")
|
391 |
|
392 |
+
# Main Interface
|
393 |
with gr.Row():
|
394 |
+
with gr.Column(scale=3):
|
395 |
+
with gr.Group():
|
396 |
+
question_input = gr.Textbox(
|
397 |
+
label="🎓 Ask Your Educational Question",
|
398 |
+
placeholder="Enter any question: math problems, science concepts, homework help, test prep, or academic topics...",
|
399 |
+
lines=4,
|
400 |
+
max_lines=10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
)
|
402 |
|
403 |
+
with gr.Row():
|
404 |
+
subject_dropdown = gr.Dropdown(
|
405 |
+
choices=["general", "mathematics", "science", "physics", "chemistry", "biology", "english", "history", "computer_science"],
|
406 |
+
label="📚 Subject",
|
407 |
+
value="general",
|
408 |
+
interactive=True
|
409 |
+
)
|
410 |
+
|
411 |
+
difficulty_dropdown = gr.Dropdown(
|
412 |
+
choices=["beginner", "intermediate", "advanced", "competition", "graduate"],
|
413 |
+
label="⚡ Difficulty",
|
414 |
+
value="intermediate",
|
415 |
+
interactive=True
|
416 |
+
)
|
417 |
+
|
418 |
+
language_dropdown = gr.Dropdown(
|
419 |
+
choices=["English", "Spanish", "French", "German", "Chinese", "Japanese"],
|
420 |
+
label="🌐 Language",
|
421 |
+
value="English",
|
422 |
+
interactive=True
|
423 |
+
)
|
424 |
+
|
425 |
+
submit_btn = gr.Button(
|
426 |
+
"🚀 Get Expert Answer",
|
427 |
+
variant="primary",
|
428 |
+
size="lg",
|
429 |
+
elem_classes="submit-button"
|
430 |
)
|
431 |
+
|
|
|
|
|
432 |
with gr.Column(scale=1):
|
433 |
+
with gr.Group():
|
434 |
+
gr.HTML('<div class="feature-box"><h3>🏆 Premium Features</h3></div>')
|
435 |
+
|
436 |
+
analytics_display = gr.Textbox(
|
437 |
+
label="📊 Live Analytics",
|
438 |
+
value=global_ai.get_global_analytics(),
|
439 |
+
lines=12,
|
440 |
+
interactive=False
|
441 |
+
)
|
442 |
+
|
443 |
+
refresh_analytics = gr.Button("🔄 Refresh Analytics", size="sm")
|
|
|
|
|
444 |
|
445 |
+
# Response Area
|
446 |
answer_output = gr.Textbox(
|
447 |
+
label="📖 Expert Educational Response",
|
448 |
+
lines=18,
|
449 |
max_lines=25,
|
450 |
+
interactive=False,
|
451 |
+
placeholder="Your detailed, step-by-step educational response will appear here..."
|
452 |
)
|
453 |
|
454 |
+
# Example Queries for Global Users
|
455 |
+
with gr.Group():
|
456 |
+
gr.HTML('<h3 style="text-align: center; margin: 1rem 0;">💡 Example Questions from Around the World</h3>')
|
457 |
+
|
458 |
+
gr.Examples(
|
459 |
+
examples=[
|
460 |
+
# Math Examples
|
461 |
+
["Solve the quadratic equation: x² + 5x + 6 = 0", "mathematics", "intermediate", "English"],
|
462 |
+
["Find the derivative of f(x) = ln(x² + 1)", "mathematics", "advanced", "English"],
|
463 |
+
["Prove that the square root of 2 is irrational", "mathematics", "competition", "English"],
|
464 |
+
|
465 |
+
# Science Examples
|
466 |
+
["Explain photosynthesis in detail with chemical equations", "biology", "intermediate", "English"],
|
467 |
+
["Why do ice cubes float on water? Explain the molecular basis", "chemistry", "beginner", "English"],
|
468 |
+
["Derive Newton's second law from first principles", "physics", "advanced", "English"],
|
469 |
+
|
470 |
+
# Multi-language Examples
|
471 |
+
["¿Cómo funciona la fotosíntesis?", "science", "intermediate", "Spanish"],
|
472 |
+
["Comment résoudre une équation du second degré?", "mathematics", "intermediate", "French"],
|
473 |
+
|
474 |
+
# Advanced Examples
|
475 |
+
["Explain quantum entanglement for a graduate student", "physics", "graduate", "English"],
|
476 |
+
["How do I prepare for the International Mathematical Olympiad?", "mathematics", "competition", "English"]
|
477 |
+
],
|
478 |
+
inputs=[question_input, subject_dropdown, difficulty_dropdown, language_dropdown],
|
479 |
+
outputs=answer_output,
|
480 |
+
fn=global_ai.educate,
|
481 |
+
cache_examples=False
|
482 |
+
)
|
483 |
|
484 |
+
# Event Handlers
|
485 |
submit_btn.click(
|
486 |
+
fn=global_ai.educate,
|
487 |
+
inputs=[question_input, subject_dropdown, difficulty_dropdown, language_dropdown],
|
488 |
outputs=answer_output,
|
489 |
+
api_name="predict" # Global API endpoint
|
490 |
)
|
491 |
|
492 |
question_input.submit(
|
493 |
+
fn=global_ai.educate,
|
494 |
+
inputs=[question_input, subject_dropdown, difficulty_dropdown, language_dropdown],
|
495 |
outputs=answer_output
|
496 |
)
|
497 |
|
498 |
+
refresh_analytics.click(
|
499 |
+
fn=global_ai.get_global_analytics,
|
500 |
+
outputs=analytics_display
|
501 |
+
)
|
502 |
+
|
503 |
+
# Auto-refresh analytics
|
504 |
+
interface.load(
|
505 |
+
fn=global_ai.get_global_analytics,
|
506 |
+
outputs=analytics_display,
|
507 |
+
every=30
|
508 |
+
)
|
509 |
+
|
510 |
+
# Global Footer
|
511 |
gr.HTML("""
|
512 |
+
<div style="text-align: center; margin-top: 3rem; padding: 2rem; background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); border-radius: 15px;">
|
513 |
+
<h3 style="color: #2c3e50; margin-bottom: 1rem;">🌍 Serving Global Education</h3>
|
514 |
+
<p style="color: #34495e; margin-bottom: 1rem;"><strong>Competitive Advantage:</strong> Real dataset integration with Competition Math (AMC/AIME),
|
515 |
+
Microsoft Orca Mathematical Reasoning, Advanced Science QA, and Academic Knowledge bases.</p>
|
516 |
+
<p style="color: #7f8c8d; font-size: 0.9em;">
|
517 |
+
🚀 <strong>API Endpoint:</strong> https://memoroeisdead-your-education-api.hf.space/run/predict<br>
|
518 |
+
💡 <strong>Supported:</strong> 60+ subjects, 5 difficulty levels, 6 languages, unlimited scale<br>
|
519 |
+
🎯 <strong>Mission:</strong> Making world-class education accessible to everyone, everywhere
|
520 |
+
</p>
|
521 |
</div>
|
522 |
""")
|
523 |
|
524 |
return interface
|
525 |
|
526 |
+
# Launch Global Education AI
|
527 |
if __name__ == "__main__":
|
528 |
+
interface = create_global_interface()
|
529 |
interface.launch(
|
530 |
server_name="0.0.0.0",
|
531 |
server_port=7860,
|
532 |
share=False,
|
533 |
+
show_error=True,
|
534 |
+
show_tips=True,
|
535 |
+
enable_queue=True, # Handle high traffic
|
536 |
+
max_threads=40 # Scale for millions of users
|
537 |
)
|