Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,171 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os, random
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 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 |
-
prompt = f"""
|
| 35 |
-
User Profile:
|
| 36 |
-
- Name: {name}
|
| 37 |
-
- Gender: {gender}
|
| 38 |
-
- Age: {age}
|
| 39 |
-
- Skin Concern: {skin_concern}
|
| 40 |
-
- Description: {user_issue}
|
| 41 |
-
- Duration: {duration}
|
| 42 |
-
- Sensitive Skin: {sensitivity}
|
| 43 |
-
- Skincare Routine: {routine}
|
| 44 |
-
|
| 45 |
-
Additional Preference based on Gender:
|
| 46 |
-
{gender_pref}
|
| 47 |
-
|
| 48 |
-
Creativity factor: {random_factor}
|
| 49 |
-
|
| 50 |
-
FORMAT STRICTLY:
|
| 51 |
-
π Personalized Greeting (use Name & friendly tone)
|
| 52 |
-
π©Ί Skin Issue Summary (1 concise line)
|
| 53 |
-
π Global Insight (but vary percentage, not same every time)
|
| 54 |
-
π§΄ Daily Care Routine (3 bullet points)
|
| 55 |
-
πΏ One Natural Remedy (unique each time)
|
| 56 |
-
π‘ 3 Lifestyle Tips (not generic, vary)
|
| 57 |
-
π¬ Motivational Quote (1 line, italic)
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
try:
|
| 61 |
-
response = client.chat.completions.create(
|
| 62 |
-
model="llama3-8b-8192",
|
| 63 |
-
messages=[{"role": "user", "content": prompt}]
|
| 64 |
-
)
|
| 65 |
-
result = response.choices[0].message.content.strip()
|
| 66 |
-
except:
|
| 67 |
-
# β
Fallback response if API fails
|
| 68 |
-
result = f"""
|
| 69 |
-
π Hi {name}, great to see you here!
|
| 70 |
-
π©Ί Your skin concern: {skin_concern}
|
| 71 |
-
π Around {random.randint(10,80)}% of people face this issue globally.
|
| 72 |
-
π§΄ Daily Care Routine:
|
| 73 |
-
β’ Cleanse twice daily with a mild face wash
|
| 74 |
-
β’ Hydrate with light moisturizer
|
| 75 |
-
β’ Apply SPF 30 sunscreen before going out
|
| 76 |
-
πΏ Natural Remedy: Honey + turmeric paste for 10 mins once a week
|
| 77 |
-
π‘ Lifestyle Tips:
|
| 78 |
-
β’ Sleep at least 7 hrs daily
|
| 79 |
-
β’ Eat omega-rich foods (fish, nuts)
|
| 80 |
-
β’ Avoid touching your face frequently
|
| 81 |
-
π¬ βYour skin reflects how kindly you treat yourself.β
|
| 82 |
-
"""
|
| 83 |
-
|
| 84 |
-
styled_result = (
|
| 85 |
-
result.replace("π", "<h3 style='color:#4CAF50;'>π")
|
| 86 |
-
.replace("π©Ί", "</h3><h3 style='color:#4CAF50;'>π©Ί")
|
| 87 |
-
.replace("π", "</h3><h3 style='color:#4CAF50;'>π")
|
| 88 |
-
.replace("π§΄", "</h3><h3 style='color:#4CAF50;'>π§΄")
|
| 89 |
-
.replace("πΏ", "</h3><h3 style='color:#4CAF50;'>πΏ")
|
| 90 |
-
.replace("π‘", "</h3><h3 style='color:#4CAF50;'>π‘")
|
| 91 |
-
.replace("π¬", "</h3><h3 style='color:#4CAF50;'>π¬") + "</h3>"
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
sticker = random.choice(stickers)
|
| 95 |
-
|
| 96 |
-
return f"""
|
| 97 |
-
<div style='font-family:Arial, sans-serif; color:#111; background:#fff; padding:20px;
|
| 98 |
-
border-radius:15px; border:2px solid #000; box-shadow:0px 3px 10px rgba(0,0,0,0.3);'>
|
| 99 |
-
<div style='display:flex; align-items:center; justify-content:space-between;'>
|
| 100 |
-
<h2 style='color:#000; margin:0;'>π€ <b>{name}</b> | {gender}, {age} yrs</h2>
|
| 101 |
-
<img src="{sticker}" style="width:70px; border-radius:10px;">
|
| 102 |
-
</div>
|
| 103 |
-
<div style='margin-top:15px; font-size:16px; line-height:1.7;'>
|
| 104 |
-
{styled_result.replace('*', '<b>').replace('', '</b>')}
|
| 105 |
-
</div>
|
| 106 |
-
</div>
|
| 107 |
-
"""
|
| 108 |
-
|
| 109 |
-
with gr.Blocks() as demo:
|
| 110 |
-
gr.HTML("""
|
| 111 |
-
<style>
|
| 112 |
-
label, .gradio-container, .gr-box {
|
| 113 |
-
color: black !important;
|
| 114 |
-
font-weight: bold;
|
| 115 |
-
font-family: Arial, sans-serif;
|
| 116 |
-
}
|
| 117 |
-
textarea, input, select {
|
| 118 |
-
background-color: white !important;
|
| 119 |
-
border: 2px solid black !important;
|
| 120 |
-
color: #000000 !important;
|
| 121 |
-
font-size: 15px !important;
|
| 122 |
-
font-weight: bold;
|
| 123 |
-
}
|
| 124 |
-
input[type="range"] {
|
| 125 |
-
accent-color: black !important;
|
| 126 |
-
}
|
| 127 |
-
select {
|
| 128 |
-
background-color: white !important;
|
| 129 |
-
color: black !important;
|
| 130 |
-
border: 2px solid #4CAF50 !important;
|
| 131 |
-
font-weight: bold;
|
| 132 |
-
}
|
| 133 |
-
</style>
|
| 134 |
-
""")
|
| 135 |
-
|
| 136 |
-
gr.HTML("""
|
| 137 |
-
<h1 style='text-align:center; font-family:Arial; font-size:30px;
|
| 138 |
-
background: linear-gradient(to right, black, #4CAF50);
|
| 139 |
-
-webkit-background-clip: text; color: transparent;'>
|
| 140 |
-
πΏ <b>Welcome to SkinSense AI β Your Smart Skincare Buddy</b>
|
| 141 |
-
</h1>
|
| 142 |
-
""")
|
| 143 |
-
|
| 144 |
-
with gr.Row():
|
| 145 |
-
with gr.Column(scale=1):
|
| 146 |
-
name = gr.Textbox(label="π Name")
|
| 147 |
-
gender = gr.Dropdown(["Female", "Male", "Other"], label="π» Gender", value="Female")
|
| 148 |
-
age = gr.Slider(5, 100, value=25, step=1, label="π Age")
|
| 149 |
-
skin_concern = gr.Dropdown(
|
| 150 |
-
["Dark Spots", "Acne", "Tanned Skin", "Oily Skin", "Dryness", "Other"],
|
| 151 |
-
label="π Select a Common Skin Concern", value="Other"
|
| 152 |
-
)
|
| 153 |
-
description = gr.Textbox(label="π§Ύ Or Describe Your Skin Concern", lines=3)
|
| 154 |
-
image = gr.Image(type="filepath", label="π· Upload Image (optional)")
|
| 155 |
-
duration = gr.Dropdown(["π Less than a week", "π 1β4 weeks", "π
Over a month"],
|
| 156 |
-
label="β³ How long?")
|
| 157 |
-
sensitivity = gr.Dropdown(["Yes", "No", "Not Sure"], label="π§ͺ Sensitive Skin?")
|
| 158 |
-
routine = gr.Dropdown(["Yes", "No"], label="π§΄ Skincare Routine?")
|
| 159 |
-
submit_btn = gr.Button("β¨ Get My Personalized Advice")
|
| 160 |
-
|
| 161 |
-
with gr.Column(scale=1):
|
| 162 |
-
output = gr.HTML("<p style='color:gray;'>Your personalized skincare advice will appear here...</p>")
|
| 163 |
-
|
| 164 |
-
submit_btn.click(
|
| 165 |
-
fn=skincare_advice,
|
| 166 |
-
inputs=[name, gender, age, skin_concern, description, image, duration, sensitivity, routine],
|
| 167 |
-
outputs=output
|
| 168 |
)
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Initialize Groq client with your API key
|
| 6 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
+
# Prompt Template
|
| 9 |
+
prompt_template = """
|
| 10 |
+
You are SkinSense AI, a gentle and friendly AI-powered skincare assistant. Your task is to analyze the following user-described symptoms and skin concerns. Based on this input, kindly return the following in a friendly tone with emojis and HTML formatting:
|
| 11 |
+
1. Greet the user using their name (if given).
|
| 12 |
+
2. Briefly summarize their skin issue in 1 line.
|
| 13 |
+
3. Give a global skin insight (a short stat or fact).
|
| 14 |
+
4. Suggest a daily care routine with 3 bullet points.
|
| 15 |
+
5. Recommend 1 natural home remedy.
|
| 16 |
+
6. Share 3 lifestyle tips.
|
| 17 |
+
7. End with a motivational quote and a short note of encouragement.
|
| 18 |
+
|
| 19 |
+
User Symptoms: {input}
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Main function to generate output
|
| 23 |
+
def analyze_skin(input, name):
|
| 24 |
+
prompt = prompt_template.format(input=input)
|
| 25 |
+
|
| 26 |
+
# Get response from Groq (Mixtral model)
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model="mixtral-8x7b-32768",
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "system", "content": "You are a helpful and friendly AI assistant."},
|
| 31 |
+
{"role": "user", "content": prompt}
|
| 32 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
+
raw_output = response.choices[0].message.content
|
| 36 |
+
|
| 37 |
+
# Fix <b> tags (auto-close them)
|
| 38 |
+
fixed_output = raw_output.replace("<b>", "<b>").replace("</b>", "</b>")
|
| 39 |
+
|
| 40 |
+
# Add user name in greeting if provided
|
| 41 |
+
if name:
|
| 42 |
+
fixed_output = fixed_output.replace("Hi!", f"π Hi {name}! π")
|
| 43 |
+
|
| 44 |
+
return fixed_output
|
| 45 |
+
|
| 46 |
+
# Gradio UI setup
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=analyze_skin,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Textbox(label="Describe your skin concern (e.g., dry, acne, oily, sensitive, etc.)"),
|
| 51 |
+
gr.Textbox(label="Your name (optional)", placeholder="e.g. Javeria")
|
| 52 |
+
],
|
| 53 |
+
outputs=gr.HTML(label="SkinSense AI Response"),
|
| 54 |
+
title="SkinSense AI π§΄πΈ",
|
| 55 |
+
description="A GenAI-powered assistant for personalized skincare advice. Just describe your skin concern and get a full care plan instantly!"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
iface.launch()
|