Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,87 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time # For progress bar simulation
|
| 3 |
|
| 4 |
+
# Custom CSS for styling (linked automatically in Gradio)
|
| 5 |
+
css = """
|
| 6 |
+
.chat-message.user {
|
| 7 |
+
background: #E3F2FD !important;
|
| 8 |
+
border-radius: 15px 15px 0 15px !important;
|
| 9 |
+
}
|
| 10 |
+
.chat-message.bot {
|
| 11 |
+
border: 1px solid #e0e0e0 !important;
|
| 12 |
+
border-radius: 15px 15px 15px 0 !important;
|
| 13 |
+
}
|
| 14 |
+
.warning {color: #FF5252; font-weight: bold;}
|
| 15 |
+
"""
|
| 16 |
|
| 17 |
+
def analyze_skin(text_input, image_input=None):
|
| 18 |
+
"""Generate AI response based on text/image input"""
|
| 19 |
+
# Simulate AI processing delay
|
| 20 |
+
time.sleep(1.5)
|
| 21 |
+
|
| 22 |
+
if image_input:
|
| 23 |
+
return """
|
| 24 |
+
## 🔍 Analysis Results (Image)
|
| 25 |
+
- **Most likely:** Mild acne (70% confidence)
|
| 26 |
+
- **Recommendations:**
|
| 27 |
+
- Apply benzoyl peroxide 2.5% twice daily
|
| 28 |
+
- Use non-comedogenic moisturizer
|
| 29 |
+
⚠️ <span class='warning'>Consult a doctor if nodules appear.</span>
|
| 30 |
+
"""
|
| 31 |
+
elif "acne" in text_input.lower():
|
| 32 |
+
return acne_response()
|
| 33 |
+
else:
|
| 34 |
+
return general_guidance()
|
| 35 |
+
|
| 36 |
+
def acne_response():
|
| 37 |
+
return """
|
| 38 |
+
## 🧴 Acne Treatment Plan
|
| 39 |
+
1. **Cleanser:** Salicylic acid wash (AM/PM)
|
| 40 |
+
2. **Treatment:** Benzoyl peroxide 2.5% (PM only)
|
| 41 |
+
3. **Moisturizer:** Oil-free, non-comedogenic
|
| 42 |
+
📅 *Expected improvement:* 4-6 weeks
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def general_guidance():
|
| 46 |
+
return """
|
| 47 |
+
## 🌟 General Skincare Advice
|
| 48 |
+
- **AM Routine:** Cleanser → Vitamin C → SPF 50+
|
| 49 |
+
- **PM Routine:** Double cleanse → Retinol → Moisturizer
|
| 50 |
+
💡 *Pro tip:* Patch test new products!
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
# --- UI Components ---
|
| 54 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 55 |
+
gr.Markdown("# 🩺 **DermaGenius AI**")
|
| 56 |
+
gr.Markdown("Describe your skin concern or upload an image for analysis")
|
| 57 |
+
|
| 58 |
+
with gr.Tab("Text Chat"):
|
| 59 |
+
chatbot = gr.ChatInterface(
|
| 60 |
+
analyze_skin,
|
| 61 |
+
examples=["How to treat dark spots?", "Is this mole dangerous?"],
|
| 62 |
+
submit_btn="Analyze 🧐"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
with gr.Tab("Image Analysis"):
|
| 66 |
+
with gr.Row():
|
| 67 |
+
img_input = gr.Image(type="filepath", label="Upload Skin Image")
|
| 68 |
+
img_output = gr.Image(label="Analysis Highlight", shape=(300, 300))
|
| 69 |
+
with gr.Row():
|
| 70 |
+
progress = gr.ProgressBar(label="Processing...", visible=False)
|
| 71 |
+
analyze_btn = gr.Button("Analyze Image", variant="primary")
|
| 72 |
+
|
| 73 |
+
# Image analysis logic
|
| 74 |
+
def analyze_image(img):
|
| 75 |
+
progress.visible = True
|
| 76 |
+
yield {"value": 0, "visible": True}
|
| 77 |
+
time.sleep(2) # Simulate processing
|
| 78 |
+
yield {"value": 100, "visible": False}
|
| 79 |
+
return img, analyze_skin("", img)
|
| 80 |
+
|
| 81 |
+
analyze_btn.click(
|
| 82 |
+
analyze_image,
|
| 83 |
+
inputs=img_input,
|
| 84 |
+
outputs=[img_output, chatbot]
|
| 85 |
+
)
|
| 86 |
|
| 87 |
demo.launch()
|