jacksonwambali commited on
Commit
6d08347
·
verified ·
1 Parent(s): 3f3bb36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +524 -8
app.py CHANGED
@@ -1,14 +1,530 @@
 
 
 
 
1
  from fastai.vision.all import *
2
- #from fastai.vision.widgets import *
3
- import timm
 
 
4
  learn = load_learner('export.pkl')
 
5
 
 
 
 
 
6
 
7
- labels = learn.dls.vocab
8
- def predict(img):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  img = PILImage.create(img)
10
- pred,pred_idx,probs = learn.predict(img)
11
- return {labels[i]: float(probs[i]) for i in range(len(labels))}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- import gradio as gr
14
- gr.Interface(fn=predict, inputs=gr.Image(), outputs=gr.Label(num_top_classes=3)).launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import re
4
+ import folium
5
  from fastai.vision.all import *
6
+ from groq import Groq
7
+ from PIL import Image
8
+
9
+ # Load the trained model
10
  learn = load_learner('export.pkl')
11
+ labels = learn.dls.vocab
12
 
13
+ # Initialize Groq client
14
+ client = Groq(
15
+ api_key=os.environ.get("GROQ_API_KEY"),
16
+ )
17
 
18
+ # Language translations
19
+ translations = {
20
+ "en": {
21
+ "app_title": "Bird Species Identification for Researchers",
22
+ "app_description": "Upload an image to identify bird species and get detailed information relevant to research in Tanzania and climate change studies.",
23
+ "upload_label": "Upload Bird Image",
24
+ "identify_button": "Identify Bird",
25
+ "predictions_label": "Top 5 Predictions",
26
+ "bird_info_label": "Bird Information",
27
+ "research_questions": "Research Questions",
28
+ "question_placeholder": "Example: How has climate change affected this bird's migration pattern?",
29
+ "question_label": "Ask a question about this bird",
30
+ "submit_question": "Submit Question",
31
+ "clear_conversation": "Clear Conversation",
32
+ "upload_prompt": "Please upload an image",
33
+ "question_title": "Question:",
34
+ "answer_title": "Answer:",
35
+ "habitat_map_title": "Natural Habitat Map for",
36
+ "detailed_info_title": "Detailed Information"
37
+ },
38
+ "sw": {
39
+ "app_title": "Utambuzi wa Spishi za Ndege kwa Watafiti",
40
+ "app_description": "Pakia picha ili kutambua spishi za ndege na kupata taarifa muhimu zinazohusiana na utafiti nchini Tanzania na masomo ya mabadiliko ya tabianchi.",
41
+ "upload_label": "Pakia Picha ya Ndege",
42
+ "identify_button": "Tambua Ndege",
43
+ "predictions_label": "Utabiri Bora 5",
44
+ "bird_info_label": "Taarifa za Ndege",
45
+ "research_questions": "Maswali ya Utafiti",
46
+ "question_placeholder": "Mfano: Je, mabadiliko ya tabianchi yameathiri vipi mfumo wa uhamiaji wa ndege huyu?",
47
+ "question_label": "Uliza swali kuhusu ndege huyu",
48
+ "submit_question": "Wasilisha Swali",
49
+ "clear_conversation": "Futa Mazungumzo",
50
+ "upload_prompt": "Tafadhali pakia picha",
51
+ "question_title": "Swali:",
52
+ "answer_title": "Jibu:",
53
+ "habitat_map_title": "Ramani ya Makazi Asilia ya",
54
+ "detailed_info_title": "Taarifa za Kina"
55
+ }
56
+ }
57
+
58
+ def clean_bird_name(name):
59
+ """Clean bird name by removing numbers and special characters, and fix formatting"""
60
+ # Remove numbers and dots at the beginning
61
+ cleaned = re.sub(r'^\d+\.', '', name)
62
+ # Replace underscores with spaces
63
+ cleaned = cleaned.replace('_', ' ')
64
+ # Remove any remaining special characters
65
+ cleaned = re.sub(r'[^\w\s]', '', cleaned)
66
+ # Fix spacing
67
+ cleaned = ' '.join(cleaned.split())
68
+ return cleaned
69
+
70
+ def get_bird_habitat_map(bird_name, check_tanzania=True):
71
+ """Get habitat map locations for the bird using Groq API"""
72
+ clean_name = clean_bird_name(bird_name)
73
+
74
+ # First check if the bird is endemic to Tanzania
75
+ if check_tanzania:
76
+ tanzania_check_prompt = f"""
77
+ Is the {clean_name} bird native to or commonly found in Tanzania?
78
+ Answer with ONLY "yes" or "no".
79
+ """
80
+
81
+ try:
82
+ tanzania_check = client.chat.completions.create(
83
+ messages=[{"role": "user", "content": tanzania_check_prompt}],
84
+ model="llama-3.3-70b-versatile",
85
+ )
86
+ is_in_tanzania = "yes" in tanzania_check.choices[0].message.content.lower()
87
+ except:
88
+ # Default to showing Tanzania if we can't determine
89
+ is_in_tanzania = True
90
+ else:
91
+ is_in_tanzania = True
92
+
93
+ # Now get the habitat locations
94
+ prompt = f"""
95
+ Provide a JSON array of the main habitat locations for the {clean_name} bird in the world.
96
+ Return ONLY a JSON array with 3-5 entries, each containing:
97
+ 1. "name": Location name
98
+ 2. "lat": Latitude (numeric value)
99
+ 3. "lon": Longitude (numeric value)
100
+ 4. "description": Brief description of why this is a key habitat (2-3 sentences)
101
+
102
+ Example format:
103
+ [
104
+ {{"name": "Example Location", "lat": 12.34, "lon": 56.78, "description": "Brief description"}},
105
+ ...
106
+ ]
107
+
108
+ {'' if is_in_tanzania else 'DO NOT include any locations in Tanzania as this bird is not native to or commonly found there.'}
109
+ """
110
+
111
+ try:
112
+ chat_completion = client.chat.completions.create(
113
+ messages=[
114
+ {
115
+ "role": "user",
116
+ "content": prompt,
117
+ }
118
+ ],
119
+ model="llama-3.3-70b-versatile",
120
+ )
121
+ response = chat_completion.choices[0].message.content
122
+
123
+ # Extract JSON from response (in case there's additional text)
124
+ import json
125
+ import re
126
+
127
+ # Find JSON pattern in response
128
+ json_match = re.search(r'\[.*\]', response, re.DOTALL)
129
+ if json_match:
130
+ locations = json.loads(json_match.group())
131
+ else:
132
+ # Fallback if JSON extraction fails
133
+ locations = [
134
+ {"name": "Primary habitat region", "lat": 0, "lon": 0,
135
+ "description": "Could not retrieve specific habitat information for this bird."}
136
+ ]
137
+
138
+ return locations, is_in_tanzania
139
+
140
+ except Exception as e:
141
+ return [{"name": "Error retrieving data", "lat": 0, "lon": 0,
142
+ "description": "Please try again or check your connection."}], False
143
+
144
+ def create_habitat_map(habitat_locations):
145
+ """Create a folium map with the habitat locations"""
146
+ # Find center point based on valid coordinates
147
+ valid_coords = [(loc.get("lat", 0), loc.get("lon", 0))
148
+ for loc in habitat_locations
149
+ if loc.get("lat", 0) != 0 or loc.get("lon", 0) != 0]
150
+
151
+ if valid_coords:
152
+ # Calculate the average of the coordinates
153
+ avg_lat = sum(lat for lat, _ in valid_coords) / len(valid_coords)
154
+ avg_lon = sum(lon for _, lon in valid_coords) / len(valid_coords)
155
+ # Create map centered on the average coordinates
156
+ m = folium.Map(location=[avg_lat, avg_lon], zoom_start=3)
157
+ else:
158
+ # Default world map if no valid coordinates
159
+ m = folium.Map(location=[20, 0], zoom_start=2)
160
+
161
+ # Add markers for each habitat location
162
+ for location in habitat_locations:
163
+ name = location.get("name", "Unknown")
164
+ lat = location.get("lat", 0)
165
+ lon = location.get("lon", 0)
166
+ description = location.get("description", "No description available")
167
+
168
+ # Skip invalid coordinates
169
+ if lat == 0 and lon == 0:
170
+ continue
171
+
172
+ # Add marker
173
+ folium.Marker(
174
+ location=[lat, lon],
175
+ popup=folium.Popup(f"<b>{name}</b><br>{description}", max_width=300),
176
+ tooltip=name
177
+ ).add_to(m)
178
+
179
+ # Save map to HTML
180
+ map_html = m._repr_html_()
181
+ return map_html
182
+
183
+ def format_bird_info(raw_info, language="en"):
184
+ """Improve the formatting of bird information"""
185
+ # Add proper line breaks between sections and ensure consistent heading levels
186
+ formatted = raw_info
187
+
188
+ # Get translation of warning text based on language
189
+ warning_text = "NOT TYPICALLY FOUND IN TANZANIA"
190
+ warning_translation = "HAPATIKANI SANA TANZANIA" if language == "sw" else warning_text
191
+
192
+ # Fix heading levels (make all main sections h3)
193
+ formatted = re.sub(r'#+\s+' + warning_text,
194
+ f'<div class="alert alert-warning"><strong>⚠️ {warning_translation}</strong></div>',
195
+ formatted)
196
+
197
+ # Replace markdown headings with HTML headings for better control
198
+ formatted = re.sub(r'#+\s+(.*)', r'<h3>\1</h3>', formatted)
199
+
200
+ # Add paragraph tags for better spacing
201
+ formatted = re.sub(r'\n\*\s+(.*)', r'<p>• \1</p>', formatted)
202
+ formatted = re.sub(r'\n([^<\n].*)', r'<p>\1</p>', formatted)
203
+
204
+ # Remove any duplicate paragraph tags
205
+ formatted = formatted.replace('<p><p>', '<p>')
206
+ formatted = formatted.replace('</p></p>', '</p>')
207
+
208
+ return formatted
209
+
210
+ def get_bird_info(bird_name, language="en"):
211
+ """Get detailed information about a bird using Groq API"""
212
+ clean_name = clean_bird_name(bird_name)
213
+
214
+ # Adjust language for the prompt
215
+ lang_instruction = ""
216
+ if language == "sw":
217
+ lang_instruction = " Provide your response in Swahili language."
218
+
219
+ prompt = f"""
220
+ Provide detailed information about the {clean_name} bird, including:
221
+ 1. Physical characteristics and appearance
222
+ 2. Habitat and distribution
223
+ 3. Diet and behavior
224
+ 4. Migration patterns (emphasize if this pattern has changed in recent years due to climate change)
225
+ 5. Conservation status
226
+
227
+ If this bird is not commonly found in Tanzania, explicitly flag that this bird is "NOT TYPICALLY FOUND IN TANZANIA" at the beginning of your response and explain why its presence might be unusual.
228
+
229
+ Format your response in markdown for better readability.{lang_instruction}
230
+ """
231
+
232
+ try:
233
+ chat_completion = client.chat.completions.create(
234
+ messages=[
235
+ {
236
+ "role": "user",
237
+ "content": prompt,
238
+ }
239
+ ],
240
+ model="llama-3.3-70b-versatile",
241
+ )
242
+ return chat_completion.choices[0].message.content
243
+ except Exception as e:
244
+ error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
245
+ return f"{error_msg}: {str(e)}"
246
+
247
+ def predict_and_get_info(img, language="en"):
248
+ """Predict bird species and get detailed information"""
249
+ # Get translations
250
+ t = translations[language]
251
+
252
+ # Process the image
253
  img = PILImage.create(img)
254
+
255
+ # Get prediction
256
+ pred, pred_idx, probs = learn.predict(img)
257
+
258
+ # Get top 5 predictions (or all if less than 5)
259
+ num_classes = min(5, len(labels))
260
+ top_indices = probs.argsort(descending=True)[:num_classes]
261
+ top_probs = probs[top_indices]
262
+ top_labels = [labels[i] for i in top_indices]
263
+
264
+ # Format as dictionary with cleaned names for display
265
+ prediction_results = {clean_bird_name(top_labels[i]): float(top_probs[i]) for i in range(num_classes)}
266
+
267
+ # Get top prediction (original format for info retrieval)
268
+ top_bird = str(pred)
269
+ # Also keep a clean version for display
270
+ clean_top_bird = clean_bird_name(top_bird)
271
+
272
+ # Get habitat locations and create map
273
+ habitat_locations, is_in_tanzania = get_bird_habitat_map(top_bird)
274
+ habitat_map_html = create_habitat_map(habitat_locations)
275
+
276
+ # Get detailed information about the top predicted bird
277
+ bird_info = get_bird_info(top_bird, language)
278
+ formatted_info = format_bird_info(bird_info, language)
279
+
280
+ # Create combined info with map at the top and properly formatted information
281
+ custom_css = """
282
+ <style>
283
+ .bird-container {
284
+ font-family: Arial, sans-serif;
285
+ padding: 10px;
286
+ }
287
+ .map-container {
288
+ height: 400px;
289
+ width: 100%;
290
+ border: 1px solid #ddd;
291
+ border-radius: 8px;
292
+ overflow: hidden;
293
+ margin-bottom: 20px;
294
+ }
295
+ .info-container {
296
+ line-height: 1.6;
297
+ }
298
+ .info-container h3 {
299
+ margin-top: 20px;
300
+ margin-bottom: 10px;
301
+ color: #2c3e50;
302
+ border-bottom: 1px solid #eee;
303
+ padding-bottom: 5px;
304
+ }
305
+ .info-container p {
306
+ margin-bottom: 10px;
307
+ }
308
+ .alert {
309
+ padding: 10px;
310
+ margin-bottom: 15px;
311
+ border-radius: 4px;
312
+ }
313
+ .alert-warning {
314
+ background-color: #fcf8e3;
315
+ border: 1px solid #faebcc;
316
+ color: #8a6d3b;
317
+ }
318
+ </style>
319
+ """
320
+
321
+ combined_info = f"""
322
+ {custom_css}
323
+ <div class="bird-container">
324
+ <h2>{t['habitat_map_title']} {clean_top_bird}</h2>
325
+ <div class="map-container">
326
+ {habitat_map_html}
327
+ </div>
328
+
329
+ <div class="info-container">
330
+ <h2>{t['detailed_info_title']}</h2>
331
+ {formatted_info}
332
+ </div>
333
+ </div>
334
+ """
335
+
336
+ return prediction_results, combined_info, clean_top_bird
337
 
338
+ def follow_up_question(question, bird_name, language="en"):
339
+ """Allow researchers to ask follow-up questions about the identified bird"""
340
+ t = translations[language]
341
+
342
+ if not question.strip() or not bird_name:
343
+ return "Please identify a bird first and ask a specific question about it." if language == "en" else "Tafadhali tambua ndege kwanza na uulize swali maalum kuhusu ndege huyo."
344
+
345
+ # Adjust language for the prompt
346
+ lang_instruction = ""
347
+ if language == "sw":
348
+ lang_instruction = " Provide your response in Swahili language."
349
+
350
+ prompt = f"""
351
+ The researcher is asking about the {bird_name} bird: "{question}"
352
+
353
+ Provide a detailed, scientific answer focusing on accurate ornithological information.
354
+ If the question relates to Tanzania or climate change impacts, emphasize those aspects in your response.
355
+
356
+ IMPORTANT: Do not repeat basic introductory information about the bird that would have already been provided in a general description.
357
+ Do not start your answer with phrases like "Introduction to the {bird_name}" or similar repetitive headers.
358
+ Directly answer the specific question asked.
359
+
360
+ Format your response in markdown for better readability.{lang_instruction}
361
+ """
362
+
363
+ try:
364
+ chat_completion = client.chat.completions.create(
365
+ messages=[
366
+ {
367
+ "role": "user",
368
+ "content": prompt,
369
+ }
370
+ ],
371
+ model="llama-3.3-70b-versatile",
372
+ )
373
+ return chat_completion.choices[0].message.content
374
+ except Exception as e:
375
+ error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
376
+ return f"{error_msg}: {str(e)}"
377
+
378
+ def update_conversation(question, bird_name, history, language="en"):
379
+ """Update the conversation history with new Q&A"""
380
+ t = translations[language]
381
+
382
+ if not question.strip():
383
+ return history
384
+
385
+ answer = follow_up_question(question, bird_name, language)
386
+
387
+ # Format the conversation with clear separation
388
+ new_exchange = f"""
389
+ ### {t['question_title']}
390
+ {question}
391
+ ### {t['answer_title']}
392
+ {answer}
393
+ ---
394
+ """
395
+ updated_history = new_exchange + history
396
+ return updated_history
397
+
398
+ # Create the Gradio interface
399
+ def create_interface(language="en"):
400
+ t = translations[language]
401
+
402
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
403
+ # Language selector
404
+ with gr.Row():
405
+ with gr.Column(scale=3):
406
+ gr.Markdown(f"# {t['app_title']}")
407
+ with gr.Column(scale=1):
408
+ language_selector = gr.Radio(
409
+ choices=["English", "Kiswahili"],
410
+ label="Language / Lugha",
411
+ value="English" if language == "en" else "Kiswahili"
412
+ )
413
+
414
+ gr.Markdown(f"{t['app_description']}")
415
+
416
+ # Store the current bird and language for context
417
+ current_bird = gr.State("")
418
+ current_lang = gr.State(language)
419
+
420
+ # Main identification section
421
+ with gr.Row():
422
+ with gr.Column(scale=1):
423
+ input_image = gr.Image(type="pil", label=t['upload_label'])
424
+ submit_btn = gr.Button(t['identify_button'], variant="primary")
425
+
426
+ with gr.Column(scale=2):
427
+ prediction_output = gr.Label(label=t['predictions_label'], num_top_classes=5)
428
+ bird_info_output = gr.HTML(label=t['bird_info_label'])
429
+
430
+ # Clear divider
431
+ gr.Markdown("---")
432
+
433
+ # Follow-up question section with improved UI
434
+ gr.Markdown(f"## {t['research_questions']}")
435
+
436
+ conversation_history = gr.Markdown("")
437
+
438
+ with gr.Row():
439
+ follow_up_input = gr.Textbox(
440
+ label=t['question_label'],
441
+ placeholder=t['question_placeholder'],
442
+ lines=2
443
+ )
444
+
445
+ with gr.Row():
446
+ follow_up_btn = gr.Button(t['submit_question'], variant="primary")
447
+ clear_btn = gr.Button(t['clear_conversation'])
448
+
449
+ # Set up event handlers
450
+ def process_image(img, lang):
451
+ if img is None:
452
+ return None, translations[lang]['upload_prompt'], "", ""
453
+
454
+ try:
455
+ pred_results, info, clean_bird_name = predict_and_get_info(img, lang)
456
+ return pred_results, info, clean_bird_name, ""
457
+ except Exception as e:
458
+ error_msg = "Hitilafu katika kuchakata picha" if lang == "sw" else "Error processing image"
459
+ return None, f"{error_msg}: {str(e)}", "", ""
460
+
461
+ def clear_conversation_history():
462
+ return ""
463
+
464
+ def switch_language(choice):
465
+ new_lang = "sw" if choice == "Kiswahili" else "en"
466
+ return new_lang
467
+
468
+ # Connect language selector to recreate the interface
469
+ language_selector.change(
470
+ switch_language,
471
+ inputs=[language_selector],
472
+ outputs=[current_lang]
473
+ ).then(
474
+ lambda lang: gr.Blocks.update(visible=False),
475
+ inputs=[current_lang],
476
+ outputs=[app]
477
+ ).then(
478
+ lambda: None,
479
+ None,
480
+ None,
481
+ _js=f"""() => {{
482
+ // Reload the page with the new language parameter
483
+ const url = new URL(window.location.href);
484
+ const lang = document.querySelector('input[name="language-selector"]:checked').value;
485
+ url.searchParams.set('language', lang === 'Kiswahili' ? 'sw' : 'en');
486
+ window.location.href = url.toString();
487
+ }}"""
488
+ )
489
+
490
+ submit_btn.click(
491
+ process_image,
492
+ inputs=[input_image, current_lang],
493
+ outputs=[prediction_output, bird_info_output, current_bird, conversation_history]
494
+ )
495
+
496
+ follow_up_btn.click(
497
+ update_conversation,
498
+ inputs=[follow_up_input, current_bird, conversation_history, current_lang],
499
+ outputs=[conversation_history]
500
+ ).then(
501
+ lambda: "",
502
+ outputs=follow_up_input
503
+ )
504
+
505
+ clear_btn.click(
506
+ clear_conversation_history,
507
+ outputs=[conversation_history]
508
+ )
509
+
510
+ return app
511
+
512
+ # Get language from URL parameter or default to English
513
+ def launch_app():
514
+ import sys
515
+
516
+ # Check if running in Gradio's development environment
517
+ if len(sys.argv) > 1 and sys.argv[1] == "run":
518
+ # Default to English when running locally
519
+ language = "en"
520
+ else:
521
+ # Get language from URL parameter
522
+ import os
523
+ language_param = os.environ.get("GRADIO_LANGUAGE", "en")
524
+ language = language_param if language_param in ["en", "sw"] else "en"
525
+
526
+ app = create_interface(language)
527
+ app.launch(share=True)
528
+
529
+ if __name__ == "__main__":
530
+ launch_app()