-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
630 lines (584 loc) · 27.4 KB
/
app.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from flask import Flask, render_template, redirect, request, jsonify, session
from flask_sqlalchemy import SQLAlchemy
import re
import random
import json
import logging
from datetime import datetime
from gemini import chat_session
app = Flask(__name__)
flask_cors.CORS(app)
# Fallback quiz data for different topics
FALLBACK_QUIZZES = {
"climate": {
"questions": [
{
"question": "What is the main greenhouse gas contributing to climate change?",
"options": ["Carbon dioxide", "Helium", "Neon", "Hydrogen"],
"correct_answer": 0
},
{
"question": "Which human activity releases the most greenhouse gases?",
"options": ["Burning fossil fuels", "Swimming", "Reading", "Playing sports"],
"correct_answer": 0
}
]
},
"default": {
"questions": [
{
"question": "Which environmental issue is most pressing?",
"options": ["Climate change", "Pollution", "Deforestation", "All of the above"],
"correct_answer": 3
}
]
}
}
def extract_json_content(text):
"""
Extract valid JSON content from text using multiple approaches
"""
try:
# First attempt: Try to find JSON between code blocks
json_matches = re.findall(r'```(?:json)?\s*({[\s\S]*?})\s*```', text)
if json_matches:
for match in json_matches:
try:
return json.loads(match)
except:
continue
# Second attempt: Find any JSON-like structure
json_matches = re.findall(r'{[\s\S]*?}', text)
if json_matches:
for match in json_matches:
try:
return json.loads(match)
except:
continue
# Third attempt: Try to construct valid JSON from the text
questions = []
current_question = {}
# Look for question patterns
question_matches = re.finditer(r'["\']question["\']\s*:\s*["\']([^"\']+)["\']', text)
options_matches = re.finditer(r'["\']options["\']\s*:\s*\[(.*?)\]', text)
answer_matches = re.finditer(r'["\']correct_answer["\']\s*:\s*(\d+)', text)
for q, o, a in zip(question_matches, options_matches, answer_matches):
try:
# Clean and parse options
options_str = o.group(1)
options = re.findall(r'["\']([^"\']+)["\']', options_str)
if len(options) == 4: # Only add if we have exactly 4 options
questions.append({
"question": q.group(1),
"options": options,
"correct_answer": int(a.group(1))
})
except:
continue
if questions:
return {"questions": questions}
except Exception as e:
app.logger.error(f"JSON extraction error: {str(e)}")
return None
return None
def get_fallback_quiz(topic):
"""Get appropriate fallback quiz based on topic and randomly select 5 questions"""
topic = topic.lower()
fallback_quizzes = {
"deforestation": {
"questions": [
{
"question": "What is the primary driver of global deforestation?",
"options": ["Agriculture expansion", "Urban development", "Mining", "Logging"],
"correct_answer": 0
},
{
"question": "Which region has the highest rate of deforestation?",
"options": ["Amazon Rainforest", "Congo Basin", "Southeast Asia", "Central America"],
"correct_answer": 0
},
{
"question": "What percentage of Earth's original forests have been lost?",
"options": ["46%", "31%", "25%", "52%"],
"correct_answer": 0
},
{
"question": "What is the main impact of deforestation on biodiversity?",
"options": ["Habitat loss", "Soil erosion", "Climate change", "Water pollution"],
"correct_answer": 0
},
{
"question": "How many acres of forest are lost every minute globally?",
"options": ["30 acres", "20 acres", "40 acres", "50 acres"],
"correct_answer": 0
},
{
"question": "What is REDD+?",
"options": ["Reducing Emissions from Deforestation and Forest Degradation",
"Reforestation Economic Development Deal", "Regional Environmental Defense Division",
"Rainforest Ecosystem Development Database"],
"correct_answer": 0
},
{
"question": "What percentage of greenhouse gas emissions are caused by deforestation?",
"options": ["15%", "5%", "25%", "35%"],
"correct_answer": 0
},
{
"question": "Which industry is most responsible for tropical deforestation?",
"options": ["Cattle ranching", "Palm oil", "Timber", "Mining"],
"correct_answer": 0
},
{
"question": "What is the most effective solution to combat deforestation?",
"options": ["Sustainable forest management", "Complete logging ban",
"Urban development restriction", "Increased mining regulations"],
"correct_answer": 0
},
{
"question": "How does deforestation affect local communities?",
"options": ["Loss of livelihood and resources", "Improved economic opportunities",
"Better access to urban areas", "Increased agricultural land"],
"correct_answer": 0
}
]
},
"climate change": {
"questions": [
{
"question": "What is the main greenhouse gas contributing to climate change?",
"options": ["Carbon dioxide", "Methane", "Water vapor", "Nitrous oxide"],
"correct_answer": 0
},
{
"question": "What is the Paris Agreement's main goal?",
"options": ["Limit global temperature rise to 1.5°C-2°C", "Ban fossil fuels",
"Protect endangered species", "Reduce ocean pollution"],
"correct_answer": 0
},
{
"question": "Which sector produces the most greenhouse gas emissions globally?",
"options": ["Energy production", "Agriculture", "Transportation", "Manufacturing"],
"correct_answer": 0
},
{
"question": "What is the greenhouse effect?",
"options": ["Trapping of heat by atmospheric gases", "Plant growth in greenhouses",
"Ozone depletion", "Arctic ice melting"],
"correct_answer": 0
},
{
"question": "What is a carbon footprint?",
"options": ["Total greenhouse gases produced by human activities",
"Physical impression left by carbon", "Carbon dioxide in the atmosphere",
"Carbon-based fuel usage"],
"correct_answer": 0
},
{
"question": "What is the IPCC?",
"options": ["Intergovernmental Panel on Climate Change",
"International Protocol for Climate Control",
"Institute for Planetary Climate Conservation",
"Industrial Partnership for Climate Cooperation"],
"correct_answer": 0
},
{
"question": "What is carbon sequestration?",
"options": ["Process of capturing and storing carbon dioxide", "Carbon dioxide production",
"Carbon-based fuel extraction", "Carbon molecule splitting"],
"correct_answer": 0
},
{
"question": "What is the urban heat island effect?",
"options": ["Cities being warmer than surrounding areas", "Tropical islands in cities",
"Heat waves in urban areas", "City-based greenhouse effect"],
"correct_answer": 0
},
{
"question": "What is climate mitigation?",
"options": ["Actions to reduce greenhouse gas emissions", "Adapting to climate change",
"Weather prediction", "Temperature measurement"],
"correct_answer": 0
},
{
"question": "What role do oceans play in climate change?",
"options": ["Absorb heat and carbon dioxide", "Produce greenhouse gases",
"Increase global temperature", "Generate wind patterns"],
"correct_answer": 0
}
]
},
"social displacement": {
"questions": [
{
"question": "What is a climate refugee?",
"options": ["Person displaced by climate-related disasters", "Environmental scientist",
"Climate change activist", "Weather forecaster"],
"correct_answer": 0
},
{
"question": "Which region is most affected by climate-induced migration?",
"options": ["Pacific Islands", "Northern Europe", "Central Canada", "Southern Argentina"],
"correct_answer": 0
},
{
"question": "What is environmental justice?",
"options": ["Fair treatment regarding environmental impacts", "Environmental law enforcement",
"Nature conservation", "Wildlife protection"],
"correct_answer": 0
},
{
"question": "How does climate change affect indigenous communities?",
"options": ["Loss of traditional territories and practices", "Increased technological advancement",
"Better access to resources", "Improved living conditions"],
"correct_answer": 0
},
{
"question": "What percentage of global migration is influenced by climate change?",
"options": ["20%", "5%", "50%", "75%"],
"correct_answer": 0
},
{
"question": "Which natural disaster causes the most displacement?",
"options": ["Floods", "Earthquakes", "Volcanic eruptions", "Landslides"],
"correct_answer": 0
},
{
"question": "What is managed retreat?",
"options": ["Planned relocation from high-risk areas", "Military strategy", "Wildlife migration",
"Forest management"],
"correct_answer": 0
},
{
"question": "How does climate displacement affect urban areas?",
"options": ["Increased pressure on infrastructure and services", "Reduced population density",
"Better urban planning", "Decreased housing demand"],
"correct_answer": 0
},
{
"question": "What is climate gentrification?",
"options": ["Property value changes due to climate risks", "Urban renewal", "Population growth",
"Housing development"],
"correct_answer": 0
},
{
"question": "Which vulnerable group is most affected by climate displacement?",
"options": ["Low-income communities", "Wealthy individuals", "Urban professionals",
"Tourist populations"],
"correct_answer": 0
}
]
},
"economic effects": {
"questions": [
{
"question": "What is the estimated annual global cost of climate change?",
"options": ["$500 billion", "$100 billion", "$1 trillion", "$50 billion"],
"correct_answer": 0
},
{
"question": "Which economic sector is most vulnerable to climate change?",
"options": ["Agriculture", "Technology", "Entertainment", "Telecommunications"],
"correct_answer": 0
},
{
"question": "What is a carbon tax?",
"options": ["Fee on greenhouse gas emissions", "Income tax", "Property tax", "Sales tax"],
"correct_answer": 0
},
{
"question": "How does climate change affect insurance costs?",
"options": ["Increases due to higher risk", "Decreases due to competition", "Remains stable",
"No significant impact"],
"correct_answer": 0
},
{
"question": "What is green finance?",
"options": ["Funding for environmental projects", "Agricultural loans", "Government spending",
"Bank investments"],
"correct_answer": 0
},
{
"question": "How do extreme weather events affect GDP?",
"options": ["Reduce economic growth", "Increase productivity", "Improve trade", "Boost employment"],
"correct_answer": 0
},
{
"question": "What is climate risk disclosure?",
"options": ["Reporting climate-related financial risks", "Weather forecasting",
"Environmental regulations", "Carbon emissions data"],
"correct_answer": 0
},
{
"question": "How does climate change affect property values?",
"options": ["Decreases in high-risk areas", "Always increases", "No effect",
"Only affects commercial property"],
"correct_answer": 0
},
{
"question": "What is the economic impact of sea-level rise?",
"options": ["Coastal property damage and loss", "Increased tourism", "Better fishing conditions",
"Improved transportation"],
"correct_answer": 0
},
{
"question": "How does climate change affect labor productivity?",
"options": ["Reduces due to heat stress", "Increases year-round", "No significant impact",
"Only affects indoor work"],
"correct_answer": 0
}
]
},
"extreme weather": {
"questions": [
{
"question": "What is the most common extreme weather event?",
"options": ["Floods", "Tornadoes", "Hurricanes", "Droughts"],
"correct_answer": 0
},
{
"question": "How does climate change affect hurricane intensity?",
"options": ["Increases it", "Decreases it", "No effect", "Only affects duration"],
"correct_answer": 0
},
{
"question": "What is a heat dome?",
"options": ["Trapped high pressure system", "Sports stadium", "Weather station",
"Temperature measurement device"],
"correct_answer": 0
},
{
"question": "How do extreme weather events affect infrastructure?",
"options": ["Damage and disruption", "Improved durability", "No significant impact",
"Enhanced performance"],
"correct_answer": 0
},
{
"question": "What is the El Niño effect?",
"options": ["Pacific Ocean warming pattern", "Arctic storm system", "Desert wind pattern",
"Mountain weather phenomenon"],
"correct_answer": 0
},
{
"question": "How has the frequency of extreme weather events changed?",
"options": ["Increased significantly", "Decreased slightly", "Remained the same",
"Only seasonal changes"],
"correct_answer": 0
},
{
"question": "What is a flash flood?",
"options": ["Rapid flooding from heavy rain", "Coastal flooding", "River overflow",
"Groundwater flooding"],
"correct_answer": 0
},
{
"question": "How do extreme weather events affect agriculture?",
"options": ["Crop damage and failure", "Improved yields", "No significant impact",
"Only affects certain crops"],
"correct_answer": 0
},
{
"question": "What is a polar vortex?",
"options": ["Low pressure arctic air system", "Tropical storm", "Desert wind",
"Mountain weather pattern"],
"correct_answer": 0
},
{
"question": "How do extreme weather events affect public health?",
"options": ["Increased illness and injury", "Improved health conditions", "No significant impact",
"Only affects certain groups"],
"correct_answer": 0
}
]
},
"biodiversity loss": {
"questions": [
{
"question": "What is the current rate of species extinction?",
"options": ["1000 times natural rate", "100 times natural rate", "10 times natural rate",
"Same as natural rate"],
"correct_answer": 0
},
{
"question": "What is the main cause of biodiversity loss?",
"options": ["Habitat destruction", "Climate change", "Pollution", "Overhunting"],
"correct_answer": 0
},
{
"question": "What is a biodiversity hotspot?",
"options": ["Area with many endangered species", "Tourist location", "Wildlife park",
"Research station"],
"correct_answer": 0
},
{
"question": "How does biodiversity loss affect ecosystem services?",
"options": ["Reduces ecosystem stability", "Improves adaptation", "No significant impact",
"Increases resilience"],
"correct_answer": 0
},
{
"question": "What percentage of known species are at risk of extinction?",
"options": ["25%", "10%", "50%", "75%"],
"correct_answer": 0
},
{
"question": "What is ecosystem fragmentation?",
"options": ["Breaking habitats into smaller parts", "Ecosystem growth", "Species migration",
"Habitat improvement"],
"correct_answer": 0
},
{
"question": "How does biodiversity loss affect food security?",
"options": ["Reduces crop resilience", "Improves crop yields", "No significant impact",
"Only affects wild species"],
"correct_answer": 0
},
{
"question": "What is an invasive species?",
"options": ["Non-native harmful species", "Endangered species", "Native species",
"Beneficial species"],
"correct_answer": 0
},
{
"question": "How does ocean acidification affect marine biodiversity?",
"options": ["Damages coral reefs", "Improves fish populations", "No effect",
"Only affects deep ocean"],
"correct_answer": 0
},
{
"question": "What is the Red List?",
"options": ["Endangered species inventory", "Conservation area list", "Wildlife protection law",
"Species recovery plan"],
"correct_answer": 0
}
]
},
"air pollution": {
"questions": [
{
"question": "What is the main source of urban air pollution?",
"options": ["Vehicle emissions", "Industrial plants", "Natural sources", "Construction"],
"correct_answer": 0
},
{
"question": "What is PM2.5?",
"options": ["Fine particulate matter", "Chemical compound", "Air quality index",
"Pollution measurement"],
"correct_answer": 0
},
{
"question": "Which air pollutant is mainly responsible for acid rain?",
"options": ["Sulfur dioxide", "Carbon dioxide", "Methane", "Oxygen"],
"correct_answer": 0
},
{
"question": "What is the main health impact of prolonged exposure to fine particulate matter (PM2.5)?",
"options": ["Skin irritation", "Respiratory issues", "Eye infections", "Hearing problems"],
"correct_answer": 1
},
{
"question": "Which layer of the atmosphere contains ozone that protects life on Earth from harmful ultraviolet (UV) radiation?",
"options": ["Troposphere", "Exosphere", "Mesosphere", "Stratosphere"],
"correct_answer": 3
},
{
"question": "Which of the following is a primary source of indoor air pollution?",
"options": ["Forest fires","Traffic emissions", "Tobacco smoke", "Power plants"],
"correct_answer": 2
}
]
}
}
if topic in fallback_quizzes:
# Get all questions for the topic
all_questions = fallback_quizzes[topic]["questions"]
# Randomly select 5 questions
selected_questions = random.sample(all_questions, 5)
# Return the selected questions in the same format
return {"questions": selected_questions}
def generate_quiz(topic):
try:
# Generate a prompt for the specific topic
prompt = f"""Generate a quiz about {topic} with 5 multiple-choice questions.
Return ONLY a valid JSON object with this structure:
{{
"questions": [
{{
"question": "Question text here?",
"options": ["Option 1", "Option 2", "Option 3", "Option 4"],
"correct_answer": 0
}}
]
}}"""
response = chat_session.send_message(prompt)
if not response or not response.text:
# If the response is empty, use a fallback quiz for the specific topic
app.logger.warning(f"Empty response for {topic}")
return jsonify(get_fallback_quiz(topic))
# Try to extract and parse JSON content
quiz_data = extract_json_content(response.text)
if quiz_data and "questions" in quiz_data:
# Validate questions
valid_questions = []
for q in quiz_data["questions"]:
if (isinstance(q, dict) and
"question" in q and
"options" in q and
"correct_answer" in q and
isinstance(q["options"], list) and
len(q["options"]) == 4):
valid_questions.append(q)
# If we have less than 5 questions, add some from the fallback quiz
if len(valid_questions) < 5:
fallback_quiz = get_fallback_quiz(topic)
for q in fallback_quiz["questions"]:
if len(valid_questions) < 5:
valid_questions.append(q)
else:
break
return jsonify({"questions": valid_questions[:5]})
# If we couldn't get valid questions, use the fallback quiz for the specific topic
return jsonify(get_fallback_quiz(topic))
except Exception as e:
app.logger.error(f"Quiz generation error: {str(e)}")
return jsonify(get_fallback_quiz(topic))
# Route handlers
@app.route('/deforestation', methods=['GET'])
def deforestation_quiz():
return generate_quiz("deforestation")
@app.route('/climate', methods=['GET'])
def climate_quiz():
return generate_quiz("Climate Change")
@app.route('/social', methods=['GET'])
def social_quiz():
return generate_quiz("Social displacement")
@app.route('/ewe', methods=['GET'])
def ewe_quiz():
return generate_quiz("Extreme Weather")
@app.route('/bio_loss', methods=['GET'])
def bio_loss_quiz():
return generate_quiz("Biodiversity loss")
@app.route('/air', methods=['GET'])
def air_quiz():
return generate_quiz("Air Pollution")
@app.route('/ecoeffect', methods=['GET'])
def eco_quiz():
return generate_quiz("Economic effects")
@app.errorhandler(500)
def handle_500_error(e):
return jsonify({
"error": "Internal server error",
"message": "Something went wrong. Please try again later."
}), 500
@app.route('/chatbot', methods=['GET'])
def chatbot_endpoint():
user_input = request.args.get('message')
if not user_input:
return jsonify({"error": "No message provided"}), 400
try:
response = chat_session.send_message(user_input)
return jsonify({"response": response.text})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)