Update app.py
Browse files15 unmodified lines161718192021222324252627287 unmodified lines36373839404142434445464748495051525354555657585960616263646566676869707172737475764 unmodified lines818283848586878889907 unmodified lines98991001011021031041051061071081091101111121131141151161171181191201211221231241251261271285 unmodified lines13413513613713813914014114214314414514614714815 unmodified lines# Islamic advice mapping based on emotionislamic_advice = {"sadness": "\"Do not grieve; indeed Allah is with us.\"\n\n**Arabic:** ููุง ุชูุญูุฒููู ุฅูููู ุงูููููู ู ูุนูููุง\n(Qur'an, At-Tawbah 9:40)","joy": "\"If you are grateful, I will surely increase your favor.\"\n\n**Arabic:** ููุฆูู ุดูููุฑูุชูู ู ููุฃูุฒููุฏููููููู ู\n(Qur'an, Ibrahim 14:7)","fear": "\"Sufficient for us is Allah, and [He is] the best Disposer of affairs.\"\n\n**Arabic:** ุญูุณูุจูููุง ุงูููููู ููููุนูู ู ุงููููููููู\n(Qur'an, Al Imran 3:173)","anger": "\"The strong is not the one who overcomes people by his strength, but the strong is the one who controls himself while in anger.\"\n\n(Hadith, Sahih al-Bukhari 6114)","confusion": "\"Seek help through patience and prayer.\"\n\n**Arabic:** ููุงุณูุชูุนูููููุง ุจูุงูุตููุจูุฑู ููุงูุตููููุงุฉู\n(Qur'an, Al-Baqarah 2:45)","love": "\"Indeed, those who have believed and done righteous deeds - the Most Merciful will appoint for them affection.\"\n\n**Arabic:** ุณูููุฌูุนููู ููููู ู ุงูุฑููุญูู ููฐูู ููุฏููุง\n(Qur'an, Maryam 19:96)","neutral": "May Allah always guide your heart in every situation."}# Analyze image emotion using processor and model7 unmodified linespred_idx = torch.argmax(probs).item()label = image_model.config.id2label[pred_idx].lower()score = probs[pred_idx].item()islamic = islamic_advice.get(label, "May Allah always guide your heart.")return f"{label.capitalize()} ({score:.2f})", islamic# Emotion label to icon mapping (subset)emotion_icons = {"admiration": "๐","amusement": "๐ ","anger": "๐ก","annoyance": "๐","approval": "๐","caring": "๐","confusion": "๐ค","curiosity": "๐ฎ","desire": "๐คค","disappointment": "๐","disapproval": "๐","disgust": "๐คฎ","embarrassment": "๐ณ","excitement": "๐","fear": "๐ฑ","gratitude": "๐","grief": "๐ญ","joy": "๐","love": "โค๏ธ","nervousness": "๐คง","optimism": "๐","pride": "๐","realization": "๐คฏ","relief": "๐","remorse": "๐","sadness": "๐ข","surprise": "๐ฒ","neutral": "๐"}# Analyze text emotiondef get_emotions(text, threshold):inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)4 unmodified linestop_idx = torch.argmax(probs).item()top_label = model.config.id2label[top_idx].lower()top_score = probs[top_idx].item()islamic = islamic_advice.get(top_label, "May Allah always guide your heart.")labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]icons = [emotion_icons.get(label, '') + ' ' + label.capitalize() + f" ({probs[i]:.2f})" for i, label in enumerate(labels)]result = ", ".join(icons) if icons else "No strong emotion detected."return result, islamic7 unmodified lines# Gradio UIcustom_css = """body {background: linear-gradient(to right, #f9f9f9, #d4ecff);font-family: 'Segoe UI', sans-serif;}.gr-button {background-color: #007BFF !important;color: white !important;border-radius: 8px !important;font-weight: bold;}.gr-button:hover {background-color: #0056b3 !important;}.gr-textbox {border-radius: 8px !important;border: 1px solid #ccc !important;padding: 10px !important;}.output-textbox {font-size: 1.5rem;font-weight: bold;color: #333;background-color: #f1f9ff;border-radius: 8px;padding: 10px;border: 1px solid #007BFF;}"""5 unmodified linesgr.Image(type="pil", label="Upload Face Photo")],outputs=[gr.Textbox(label="Detected Text Emotions", elem_classes=["output-textbox"]),gr.Textbox(label="Quranic/Hadith Advice (Text)", elem_classes=["output-textbox"]),gr.Textbox(label="Detected Photo Emotion", elem_classes=["output-textbox"]),gr.Textbox(label="Quranic/Hadith Advice (Image)", elem_classes=["output-textbox"])],title="๐ฅฐ Multi-Modal Emotion Detector with Islamic Insight",description="Analyze emotion from both text and a facial photo. Then receive inspirational Islamic advice based on your mood.",theme="default",css=custom_css)demo.launch()15 unmodified lines161718192021222324252627287 unmodified lines3637383940414243444 unmodified lines495051525354555657587 unmodified lines666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131145 unmodified lines12012112212312412512612712812913013113213313415 unmodified lines# Islamic advice mapping based on emotionislamic_advice = {"sadness": "<div class='notion-card'><h3>\u201cDo not grieve; indeed Allah is with us.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ููุง ุชูุญูุฒููู ุฅูููู ุงูููููู ู ูุนูููุง</span><br><em>Qur'an, At-Tawbah 9:40</em></p></div>","joy": "<div class='notion-card'><h3>\u201cIf you are grateful, I will surely increase your favor.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ููุฆูู ุดูููุฑูุชูู ู ููุฃูุฒููุฏููููููู ู</span><br><em>Qur'an, Ibrahim 14:7</em></p></div>","fear": "<div class='notion-card'><h3>\u201cSufficient for us is Allah, and [He is] the best Disposer of affairs.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ุญูุณูุจูููุง ุงูููููู ููููุนูู ู ุงููููููููู</span><br><em>Qur'an, Al Imran 3:173</em></p></div>","anger": "<div class='notion-card'><h3>\u201cThe strong is not the one who overcomes people by his strength, but the strong is the one who controls himself while in anger.\u201d</h3><p><em>Hadith, Sahih al-Bukhari 6114</em></p></div>","confusion": "<div class='notion-card'><h3>\u201cSeek help through patience and prayer.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ููุงุณูุชูุนูููููุง ุจูุงูุตููุจูุฑู ููุงูุตููููุงุฉู</span><br><em>Qur'an, Al-Baqarah 2:45</em></p></div>","love": "<div class='notion-card'><h3>\u201cIndeed, those who have believed and done righteous deeds - the Most Merciful will appoint for them affection.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ุณูููุฌูุนููู ููููู ู ุงูุฑููุญูู ููฐูู ููุฏููุง</span><br><em>Qur'an, Maryam 19:96</em></p></div>","neutral": "<div class='notion-card'><h3>May Allah always guide your heart in every situation.</h3></div>"}# Analyze image emotion using processor and model7 unmodified linespred_idx = torch.argmax(probs).item()label = image_model.config.id2label[pred_idx].lower()score = probs[pred_idx].item()islamic = islamic_advice.get(label, islamic_advice["neutral"])return f"{label.capitalize()} ({score:.2f})", islamic# Analyze text emotiondef get_emotions(text, threshold):inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)4 unmodified linestop_idx = torch.argmax(probs).item()top_label = model.config.id2label[top_idx].lower()top_score = probs[top_idx].item()islamic = islamic_advice.get(top_label, islamic_advice["neutral"])labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]icons = [f"{label.capitalize()} ({probs[i]:.2f})" for i, label in enumerate(labels)]result = ", ".join(icons) if icons else "No strong emotion detected."return result, islamic7 unmodified lines# Gradio UIcustom_css = """body {background: #f9fafb;font-family: 'Inter', sans-serif;color: #1f2937;padding: 20px;}.gr-button {background-color: #3b82f6 !important;color: white !important;border-radius: 8px !important;font-weight: 600;padding: 10px 20px;}.gr-button:hover {background-color: #2563eb !important;}.gr-textbox, .output-textbox {border-radius: 12px !important;border: 1px solid #d1d5db !important;padding: 12px !important;font-size: 1rem;}.output-textbox {background-color: #ffffff;border: 1px solid #60a5fa;}.notion-card {background: #ffffff;border-radius: 12px;border: 1px solid #e5e7eb;padding: 16px;margin-top: 12px;box-shadow: 0 2px 6px rgba(0,0,0,0.04);}.notion-card h3 {margin-top: 0;color: #111827;font-size: 1.1rem;margin-bottom: 6px;}.notion-card p {font-size: 0.95rem;color: #374151;margin: 0;}"""5 unmodified linesgr.Image(type="pil", label="Upload Face Photo")],outputs=[gr.Textbox(label="Detected Text Emotions", elem_classes=["output-textbox"], lines=1),gr.HTML(label="Quranic/Hadith Advice (Text)"),gr.Textbox(label="Detected Photo Emotion", elem_classes=["output-textbox"], lines=1),gr.HTML(label="Quranic/Hadith Advice (Image)")],title="๐ง EmotionLens: Understand & Reflect Emotionally",description="An AI-powered emotional intelligence assistant with Islamic wisdom. Analyze how you feel from text and facial expressions, and receive gentle spiritual insight.",theme="default",css=custom_css)demo.launch()
app.py+51-65