zainulabedin949 commited on
Commit
2202c06
·
verified ·
1 Parent(s): ba6cc63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -138
app.py CHANGED
@@ -261,82 +261,12 @@ EMERGENCY_SCENARIOS = {
261
  }
262
  }
263
 
264
- def analyze_medical_image(image):
265
- """Analyze medical image using multiple classifiers"""
266
-
267
- detected_conditions = []
268
- confidence_scores = {}
269
-
270
- try:
271
- # Try wound classifier first
272
- wound_results = wound_classifier(image, top_k=5)
273
-
274
- for result in wound_results:
275
- label = result['label'].lower()
276
- score = result['score']
277
-
278
- # Store all results with scores
279
- confidence_scores[label] = score
280
-
281
- # Map wound types to Urdu emergencies
282
- if score > 0.2:
283
- if any(word in label for word in ['burn', 'scald', 'thermal', 'fire']):
284
- detected_conditions.append(("جل جانا", score))
285
- elif any(word in label for word in ['cut', 'laceration', 'wound', 'bleeding', 'incision']):
286
- detected_conditions.append(("خون بہنا", score))
287
- elif any(word in label for word in ['abrasion', 'scrape', 'graze', 'scratch']):
288
- detected_conditions.append(("خون بہنا", score))
289
- elif any(word in label for word in ['bruise', 'contusion', 'hematoma']):
290
- detected_conditions.append(("چوٹ یا نیلا نشان", score))
291
- elif any(word in label for word in ['fracture', 'broken', 'bone']):
292
- detected_conditions.append(("ہڈی ٹوٹنا", score))
293
- elif any(word in label for word in ['bite', 'animal', 'insect']):
294
- detected_conditions.append(("سانپ کا کاٹنا", score))
295
- elif any(word in label for word in ['rash', 'skin', 'allergy', 'dermatitis']):
296
- detected_conditions.append(("جلد پر خارش یا دانے", score))
297
- elif any(word in label for word in ['swelling', 'inflammation', 'edema']):
298
- detected_conditions.append(("چوٹ یا نیلا نشان", score))
299
-
300
- # If no specific wound detected, try general classifier
301
- if not detected_conditions:
302
- general_results = medical_classifier(image, top_k=5)
303
-
304
- for result in general_results:
305
- label = result['label'].lower()
306
- score = result['score']
307
-
308
- if score > 0.15:
309
- if any(word in label for word in ['wound', 'injury', 'medical', 'blood', 'skin', 'burn', 'cut']):
310
- detected_conditions.append(("خون بہنا", score))
311
- break
312
-
313
- except Exception as e:
314
- print(f"Image analysis error: {e}")
315
- return None, None
316
-
317
- # Return the most confident detection
318
- if detected_conditions:
319
- detected_conditions.sort(key=lambda x: x[1], reverse=True)
320
- best_match = detected_conditions[0]
321
-
322
- # Create detailed analysis message
323
- analysis_msg = f"\n\n📊 تصویر کی تجزیہ:\n"
324
- analysis_msg += f"• پتہ چلی حالت: {best_match[0]}\n"
325
- analysis_msg += f"• اعتماد کی سطح: {best_match[1]:.1%}\n"
326
-
327
- if len(detected_conditions) > 1:
328
- analysis_msg += f"\n🔍 دیگر ممکنہ حالات:\n"
329
- for condition, score in detected_conditions[1:3]:
330
- analysis_msg += f"• {condition} ({score:.1%})\n"
331
-
332
- return best_match[0], analysis_msg
333
-
334
- return None, None
335
-
336
  def text_to_speech_urdu(text):
337
  """Convert Urdu text to speech"""
338
  try:
339
- tts = gTTS(text=text, lang='ur', slow=False)
 
 
340
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
341
  tts.save(temp_file.name)
342
  return temp_file.name
@@ -344,74 +274,19 @@ def text_to_speech_urdu(text):
344
  print(f"TTS Error: {e}")
345
  return None
346
 
347
- def get_emergency_help(query_text, image=None):
348
  """Get emergency first aid instructions"""
349
 
350
- matched_emergency = None
351
- image_analysis_msg = ""
352
-
353
- # First try image analysis if image is provided
354
- if image is not None:
355
- detected_emergency, analysis_msg = analyze_medical_image(image)
356
- if detected_emergency:
357
- matched_emergency = detected_emergency
358
- image_analysis_msg = analysis_msg
359
-
360
- # If no match from image, try text query
361
- if not matched_emergency and query_text:
362
- query_lower = query_text.lower()
363
-
364
- keywords_map = {
365
- "دل کا دورہ": ["دل", "heart", "chest", "سینہ", "سینے"],
366
- "دم گھٹنا": ["گھٹ", "chok", "گلا", "throat", "سانس"],
367
- "خون بہنا": ["خون", "blood", "کٹ", "cut", "زخم", "bleeding"],
368
- "بے ہوشی": ["بے ہوش", "unconscious", "faint", "گر"],
369
- "جل جانا": ["جل", "burn", "آگ", "fire", "جلنا"],
370
- "ہڈی ٹوٹنا": ["ہڈی", "bone", "fracture", "توڑ", "ٹوٹ"],
371
- "زہر کھا لینا": ["زہر", "poison", "کھا لیا"],
372
- "دورہ پڑنا": ["دورہ", "seizure", "جھٹکے"],
373
- "لو لگنا": ["لو", "heat", "گرمی"],
374
- "سانپ کا کاٹنا": ["سانپ", "snake", "کاٹ", "bite"],
375
- "دمہ کا دورہ": ["دمہ", "asthma", "سانس", "breath"],
376
- "الرجی کا شدید ردعمل": ["الرجی", "allergy", "سوجن", "swell"],
377
- "فالج": ["فالج", "stroke", "چہرہ", "face"],
378
- "شوگر کم ہونا": ["شوگر", "sugar", "low", "کانپنا"],
379
- "بجلی کا جھٹکا": ["بجلی", "electric", "shock", "جھٹکا"],
380
- "ڈوبنا": ["ڈوب", "drown", "پانی", "water"],
381
- "ناک سے خون بہنا": ["ناک", "nose", "bleed"],
382
- "موچ": ["موچ", "sprain", "ٹخنہ", "ankle"],
383
- "جلد پر خارش یا دانے": ["خارش", "rash", "دانے", "جلد", "skin"],
384
- "چوٹ یا نیلا نشان": ["چوٹ", "bruise", "نیلا", "چوٹ"]
385
- }
386
-
387
- for emergency, keywords in keywords_map.items():
388
- if any(keyword in query_lower for keyword in keywords):
389
- matched_emergency = emergency
390
- break
391
-
392
- if not matched_emergency:
393
- response = "میں نے آپ کی بات یا تصویر کو پہچان نہیں سکا۔ "
394
-
395
- if image is not None:
396
- response += "\n\n💡 تجویز: براہ کرم:\n"
397
- response += "• تصویر کو واضح اور روشنی میں لیں\n"
398
- response += "• زخم یا متاثرہ حصہ قریب سے دکھائیں\n"
399
- response += "• یا نیچے سے اپنی ایمرجنسی منتخب کریں\n\n"
400
-
401
- response += "براہ کرم مندرجہ ذیل میں سے منتخب کریں:\n\n"
402
- response += "\n".join([f"• {key}" for key in EMERGENCY_SCENARIOS.keys()])
403
  audio_file = text_to_speech_urdu(response)
404
  return response, audio_file
405
 
406
  # Get emergency info
407
- emergency_info = EMERGENCY_SCENARIOS[matched_emergency]
408
 
409
  # Format response without asterisks
410
- response = f"🚨 {matched_emergency}\n\n"
411
-
412
- if image_analysis_msg:
413
- response += image_analysis_msg + "\n"
414
-
415
  response += f"علامات: {emergency_info['symptoms']}\n\n"
416
  response += "فوری اقدامات:\n\n"
417
 
@@ -438,8 +313,82 @@ with gr.Blocks(title="ایمرجنسی فرسٹ ایڈ AI", theme=gr.themes.Soft
438
  # 🚑 ایمرجنسی فرسٹ ایڈ AI
439
  ## آپ کا زندگی بچانے والا ساتھی - اردو میں
440
 
441
- کیسے استعمال کریں:
442
- 1. نیچے سے اپنی ایمرجنسی منتخب کریں
443
- 2. فوری اقدامات اردو میں ملیں گے
444
- 3. آواز سنیں یا پڑھیں - آپ کی مرضی!
445
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  }
262
  }
263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  def text_to_speech_urdu(text):
265
  """Convert Urdu text to speech"""
266
  try:
267
+ # Remove asterisks and other markdown formatting before TTS
268
+ clean_text = text.replace('*', '').replace('#', '').replace('•', '')
269
+ tts = gTTS(text=clean_text, lang='ur', slow=False)
270
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
271
  tts.save(temp_file.name)
272
  return temp_file.name
 
274
  print(f"TTS Error: {e}")
275
  return None
276
 
277
+ def get_emergency_help(scenario_name):
278
  """Get emergency first aid instructions"""
279
 
280
+ if scenario_name not in EMERGENCY_SCENARIOS:
281
+ response = "براہ کرم صحیح ایمرجنسی منتخب کریں"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  audio_file = text_to_speech_urdu(response)
283
  return response, audio_file
284
 
285
  # Get emergency info
286
+ emergency_info = EMERGENCY_SCENARIOS[scenario_name]
287
 
288
  # Format response without asterisks
289
+ response = f"🚨 {scenario_name}\n\n"
 
 
 
 
290
  response += f"علامات: {emergency_info['symptoms']}\n\n"
291
  response += "فوری اقدامات:\n\n"
292
 
 
313
  # 🚑 ایمرجنسی فرسٹ ایڈ AI
314
  ## آپ کا زندگی بچانے والا ساتھی - اردو میں
315
 
316
+ نیچے سے اپنی ایمرجنسی منتخب کریں اور فوری ہدایات حاصل کریں
317
+ """)
318
+
319
+ gr.Markdown("### ⚡ عام ایمرجنسیز - منتخب کریں")
320
+
321
+ with gr.Row():
322
+ with gr.Column():
323
+ btn1 = gr.Button("💔 دل کا دورہ", size="lg")
324
+ btn2 = gr.Button("🫁 دم گھٹنا", size="lg")
325
+ btn3 = gr.Button("🩸 خون بہنا", size="lg")
326
+ btn10 = gr.Button("🫀 دمہ کا دورہ", size="lg")
327
+ btn11 = gr.Button("🤧 الرجی کا ردعمل", size="lg")
328
+ btn16 = gr.Button("🔴 جلد پر خارش یا دانے", size="lg")
329
+
330
+ with gr.Column():
331
+ btn4 = gr.Button("😵 بے ہوشی", size="lg")
332
+ btn5 = gr.Button("🔥 جل جانا", size="lg")
333
+ btn6 = gr.Button("🦴 ہڈی ٹوٹنا", size="lg")
334
+ btn12 = gr.Button("🧠 فالج", size="lg")
335
+ btn13 = gr.Button("🍬 شوگر کم ہونا", size="lg")
336
+ btn17 = gr.Button("🟣 چوٹ یا نیلا نشان", size="lg")
337
+
338
+ with gr.Column():
339
+ btn7 = gr.Button("☠️ زہر کھا لینا", size="lg")
340
+ btn8 = gr.Button("⚡ دورہ پڑنا", size="lg")
341
+ btn9 = gr.Button("🌡️ لو لگنا", size="lg")
342
+ btn14 = gr.Button("⚡ بجلی کا جھٹکا", size="lg")
343
+ btn15 = gr.Button("🌊 ڈوبنا", size="lg")
344
+ btn18 = gr.Button("🩹 موچ", size="lg")
345
+
346
+ with gr.Row():
347
+ with gr.Column():
348
+ btn19 = gr.Button("🐍 سانپ کا کاٹنا", size="lg")
349
+ with gr.Column():
350
+ btn20 = gr.Button("👃 ناک سے خون بہنا", size="lg")
351
+
352
+ quick_output = gr.Textbox(label="فرسٹ ایڈ ہدایات", lines=20, rtl=True)
353
+ quick_audio = gr.Audio(label="آواز میں سنیں", type="filepath")
354
+
355
+ # Connect all buttons with proper lambda functions
356
+ btn1.click(fn=lambda: quick_scenario("دل کا دورہ"), inputs=[], outputs=[quick_output, quick_audio])
357
+ btn2.click(fn=lambda: quick_scenario("دم گھٹنا"), inputs=[], outputs=[quick_output, quick_audio])
358
+ btn3.click(fn=lambda: quick_scenario("خون بہنا"), inputs=[], outputs=[quick_output, quick_audio])
359
+ btn4.click(fn=lambda: quick_scenario("بے ہوشی"), inputs=[], outputs=[quick_output, quick_audio])
360
+ btn5.click(fn=lambda: quick_scenario("جل جانا"), inputs=[], outputs=[quick_output, quick_audio])
361
+ btn6.click(fn=lambda: quick_scenario("ہڈی ٹوٹنا"), inputs=[], outputs=[quick_output, quick_audio])
362
+ btn7.click(fn=lambda: quick_scenario("زہر کھا لینا"), inputs=[], outputs=[quick_output, quick_audio])
363
+ btn8.click(fn=lambda: quick_scenario("دورہ پڑنا"), inputs=[], outputs=[quick_output, quick_audio])
364
+ btn9.click(fn=lambda: quick_scenario("لو لگنا"), inputs=[], outputs=[quick_output, quick_audio])
365
+ btn10.click(fn=lambda: quick_scenario("دمہ کا دورہ"), inputs=[], outputs=[quick_output, quick_audio])
366
+ btn11.click(fn=lambda: quick_scenario("الرجی کا شدید ردعمل"), inputs=[], outputs=[quick_output, quick_audio])
367
+ btn12.click(fn=lambda: quick_scenario("فالج"), inputs=[], outputs=[quick_output, quick_audio])
368
+ btn13.click(fn=lambda: quick_scenario("شوگر کم ہونا"), inputs=[], outputs=[quick_output, quick_audio])
369
+ btn14.click(fn=lambda: quick_scenario("بجلی کا جھٹکا"), inputs=[], outputs=[quick_output, quick_audio])
370
+ btn15.click(fn=lambda: quick_scenario("ڈوبنا"), inputs=[], outputs=[quick_output, quick_audio])
371
+ btn16.click(fn=lambda: quick_scenario("جلد پر خارش یا دانے"), inputs=[], outputs=[quick_output, quick_audio])
372
+ btn17.click(fn=lambda: quick_scenario("چوٹ یا نیلا نشان"), inputs=[], outputs=[quick_output, quick_audio])
373
+ btn18.click(fn=lambda: quick_scenario("موچ"), inputs=[], outputs=[quick_output, quick_audio])
374
+ btn19.click(fn=lambda: quick_scenario("سانپ کا کاٹنا"), inputs=[], outputs=[quick_output, quick_audio])
375
+ btn20.click(fn=lambda: quick_scenario("ناک سے خون بہنا"), inputs=[], outputs=[quick_output, quick_audio])
376
+
377
+ gr.Markdown("""
378
+ ---
379
+ ### ⚠️ اہم یاد دہانی:
380
+ - یہ AI صرف ابتدائی مدد کے لیے ہے
381
+ - شدید ایمرجنسی میں فوری 1122 کال کریں
382
+ - پیشہ ور طبی مشورے کا متبادل نہیں
383
+
384
+ 🏥 ایمرجنسی نمبرز:
385
+ - Rescue 1122: 1122
386
+ - Edhi Ambulance: 115
387
+ - Chippa Ambulance: 1020
388
+
389
+ Made with ❤️ for Pakistan | Competition Project by Zain
390
+ """)
391
+
392
+ # Launch the app
393
+ if __name__ == "__main__":
394
+ app.launch()