JaveriaZia commited on
Commit
1beb09d
Β·
verified Β·
1 Parent(s): 754aa3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -164
app.py CHANGED
@@ -1,171 +1,59 @@
1
  import gradio as gr
2
- import os, random
3
  from groq import Groq
 
4
 
5
- # βœ… Initialize Groq client
6
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
7
 
8
- # βœ… Improved stickers (cute, health/skincare-related)
9
- stickers = [
10
- "https://media.giphy.com/media/3o6Zt481isNVuQI1l6/giphy.gif", # Cream applying
11
- "https://media.giphy.com/media/l3q2XB76CaWPggiNW/giphy.gif", # Aloe vera
12
- "https://media.giphy.com/media/26tPplGWjN0xLybiU/giphy.gif", # Water drinking
13
- "https://media.giphy.com/media/3o7TKP9XDb9IzUrJXa/giphy.gif", # Cucumber eyes
14
- "https://media.giphy.com/media/26Fxy3Iz1ari8oytO/giphy.gif", # Face mask cute
15
- ]
16
-
17
- def skincare_advice(name, gender, age, skin_concern, description, image, duration, sensitivity, routine):
18
- if not description.strip() and (skin_concern == "Other" or not skin_concern.strip()):
19
- return "<b style='color:red;'>⚠ Please select or describe your skin concern.</b>"
20
- if not name.strip():
21
- return "<b style='color:red;'>⚠ Please enter your name for personalized advice.</b>"
22
-
23
- user_issue = description if description.strip() else skin_concern
24
- random_factor = random.randint(1, 10000)
25
-
26
- # βœ… Gender-based differentiation
27
- if gender == "Female":
28
- gender_pref = "Focus on hydration, mild natural ingredients, and avoid harsh scrubs."
29
- elif gender == "Male":
30
- gender_pref = "Men often need oil-control and deep cleansing due to thicker skin."
31
- else:
32
- gender_pref = "Use a balanced routine suitable for all skin types."
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
- # βœ… Remove settings & gradio bottom bar
171
- demo.launch(show_api=False, share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()