JaveriaZia commited on
Commit
edc8559
Β·
verified Β·
1 Parent(s): a0569ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -130
app.py CHANGED
@@ -1,141 +1,100 @@
1
  import streamlit as st
2
- import random
3
  import os
4
- import re
5
  from groq import Groq
6
- import base64
7
 
8
- # Set your GROQ API Key
9
- GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your_groq_api_key_here") # Replace with actual key
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
12
- # Stickers for response
13
  stickers = [
14
- "https://i.imgur.com/6KZyFxh.png",
15
- "https://i.imgur.com/f1Qdqpv.png",
16
- "https://i.imgur.com/UuUJqfL.png",
17
- "https://i.imgur.com/bGMH89Y.png",
18
- "https://i.imgur.com/y0ubkNe.png"
19
  ]
20
 
21
- # Streamlit UI setup
22
- st.set_page_config(page_title="SkinSense AI", layout="centered")
23
- st.markdown("<h1 style='text-align:center; color:#5E35B1;'>🌸 SkinSense AI – Skincare Buddy</h1>", unsafe_allow_html=True)
24
-
25
- # Input form
26
- name = st.text_input("Your Name")
27
- gender = st.radio("Gender", ["Female", "Male", "Other"])
28
- age = st.slider("Age", 12, 100, 25)
29
- skin_concern = st.selectbox("Main Skin Concern", ["Acne", "Dryness", "Oiliness", "Pores", "Dullness", "Pigmentation", "Other"])
30
- description = st.text_area("Describe Your Skin Problem", placeholder="e.g. Small white pimples under skin for 2 weeks...")
31
- image = st.file_uploader("Or upload a picture of your skin", type=["jpg", "jpeg", "png"])
32
- duration = st.selectbox("How long has this been a concern?", ["Less than 1 week", "1-2 weeks", "More than 2 weeks"])
33
- sensitivity = st.radio("Is your skin sensitive?", ["Yes", "No", "Not Sure"])
34
- routine = st.text_input("Do you follow a routine?", placeholder="e.g. Facewash, moisturizer, sunscreen")
35
- submit = st.button("✨ Generate Skincare Advice")
36
-
37
- # Core function
38
- def skincare_advice(name, gender, age, skin_concern, description, duration, sensitivity, routine, image):
39
- if not name.strip():
40
- return "<span style='color:red;'>⚠ Please enter your name.</span>"
41
-
42
- sticker = random.choice(stickers)
43
-
44
- # Determine concern text
45
- if description.strip():
46
- concern_text = description.strip()
47
- elif image is not None:
48
- concern_text = "The user uploaded an image showing their skin issue. Please base your suggestions on typical visible skin concerns."
49
- else:
50
- concern_text = skin_concern
51
-
52
- # Prompt for the model
53
  prompt = f"""
54
- A user named {name} (Gender: {gender}, Age: {age}) is experiencing a skin issue: "{concern_text}".
55
- Duration: {duration}, Sensitive skin: {sensitivity}, Routine followed: {routine}.
56
-
57
- Write a response in sections using emojis and headings only for:
58
- 🌟 Greeting
59
- 🩺 Skin Issue Summary
60
- πŸ“Š Global Insight
61
- 🧴 Daily Skincare Routine
62
- 🌿 Natural Remedy
63
- πŸ’‘ Lifestyle Tips
64
- πŸ’¬ Motivational Quote
65
-
66
- Each section should start with a title (e.g., 🌿 Natural Remedy) on one line, then 2–4 lines of helpful content below it in plain text or bullet points.
67
- Do not use bold, underline, or any extra styling.
68
- """
69
-
70
- try:
71
- response = client.chat.completions.create(
72
- model="llama3-8b-8192",
73
- messages=[{"role": "user", "content": prompt}]
74
- )
75
- raw = response.choices[0].message.content
76
- except Exception as e:
77
- return f"<span style='color:red;'>❌ Error: {str(e)}</span>"
78
-
79
- # Format response into HTML
80
- lines = raw.strip().split("\n")
81
- html_output = ""
82
- section_colors = {
83
- "🌟": "#E53935", "🩺": "#D81B60", "πŸ“Š": "#5E35B1",
84
- "🧴": "#F57C00", "🌿": "#43A047", "πŸ’‘": "#039BE5", "πŸ’¬": "#6D4C41"
85
- }
86
-
87
- current_section = None
88
- section_buffer = []
89
-
90
- for line in lines:
91
- line = line.strip()
92
- if not line:
93
- continue
94
-
95
- emoji = line[:2]
96
- if emoji in section_colors:
97
- if current_section:
98
- html_output += f"<h4 style='color:{section_colors[current_section]}; margin-top:20px;'>{section_title}</h4>\n"
99
- for l in section_buffer:
100
- html_output += f"<p style='color:#111; margin:5px 0;'>{l}</p>\n"
101
- section_buffer = []
102
-
103
- current_section = emoji
104
- section_title = line
105
- else:
106
- section_buffer.append(line)
107
-
108
- # Flush the final section
109
- if current_section:
110
- html_output += f"<h4 style='color:{section_colors[current_section]}; margin-top:20px;'>{section_title}</h4>\n"
111
- for l in section_buffer:
112
- html_output += f"<p style='color:#111; margin:5px 0;'>{l}</p>\n"
113
-
114
- # Embed uploaded image
115
- image_html = ""
116
- if image is not None:
117
- mime = image.type
118
- img_bytes = image.read()
119
- img_b64 = base64.b64encode(img_bytes).decode()
120
- image_html = f"<img src='data:{mime};base64,{img_b64}' style='max-width:100%; border-radius:12px; margin:15px 0;'>"
121
-
122
- # Final HTML layout
123
- final_html = f"""
124
- <div style='font-family:Arial, sans-serif; color:#111; background:#fff; padding:20px;
125
- border-radius:12px; border:1px solid #ccc; box-shadow:0 2px 6px rgba(0,0,0,0.1);'>
126
- <div style='display:flex; align-items:center; justify-content:space-between;'>
127
- <h2 style='color:#000; margin:0;'>πŸ‘€ <b>{name}</b> | {gender}, {age} yrs</h2>
128
- <img src="{sticker}" style="width:70px; border-radius:10px;">
129
- </div>
130
- {image_html}
131
- {html_output}
132
- </div>
133
  """
134
-
135
- return final_html
136
-
137
- # Handle button press
138
- if submit:
139
- with st.spinner("Generating advice..."):
140
- result = skincare_advice(name, gender, age, skin_concern, description, duration, sensitivity, routine, image)
141
- st.markdown(result, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
  import os
 
3
  from groq import Groq
 
4
 
5
+ # --- Load Groq API Key ---
6
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your_groq_api_key_here")
7
  client = Groq(api_key=GROQ_API_KEY)
8
 
9
+ # --- Cute Stickers ---
10
  stickers = [
11
+ "https://media.giphy.com/media/3o6Zt481isNVuQI1l6/giphy.gif",
12
+ "https://media.giphy.com/media/l3q2XB76CaWPcSspC/giphy.gif",
13
+ "https://media.giphy.com/media/ICOgUNjpvO0PC/giphy.gif",
14
+ "https://media.giphy.com/media/26BRuo6sLetdllPAQ/giphy.gif"
 
15
  ]
16
 
17
+ # --- Title ---
18
+ st.title("πŸ’†β€β™€οΈ SkinSense AI - Your Skincare Assistant")
19
+
20
+ # --- Sidebar Input ---
21
+ with st.sidebar:
22
+ st.header("πŸ“ User Info")
23
+ name = st.text_input("Your Name")
24
+ gender = st.selectbox("Gender", ["Female", "Male", "Other"])
25
+ age = st.slider("Age", 10, 80, 25)
26
+ skin_concern = st.selectbox("Main Skin Concern", [
27
+ "Acne", "Dryness", "Oiliness", "Dark Spots", "Redness", "Wrinkles", "Dullness", "Other"
28
+ ])
29
+ description = st.text_area("Describe your skin condition (symptoms, triggers, etc.)")
30
+ duration = st.text_input("How long has this been happening?")
31
+ sensitivity = st.radio("Skin Sensitivity?", ["Yes", "No"])
32
+ routine = st.text_area("Current Skincare Routine (if any)")
33
+ image = st.file_uploader("Optional: Upload a photo of your skin (jpg/png)", type=["jpg", "png"])
34
+
35
+ # --- Function to get response from Groq ---
36
+ def skincare_advice(name, gender, age, skin_concern, description, duration, sensitivity, routine, image=None):
 
 
 
 
 
 
 
 
 
 
 
 
37
  prompt = f"""
38
+ You are a skincare expert chatbot. A user named {name}, a {gender}, aged {age}, has the following skin concern: {skin_concern}.
39
+ They described the issue as: {description}. The issue has been happening for: {duration}.
40
+ Sensitive skin: {sensitivity}. Current routine: {routine}.
41
+ Provide a friendly and professional skin analysis, possible causes, and a step-by-step personalized skincare routine.
42
+ Include emojis to make it friendly. Use headings like "🌟 Greeting", "🩺 Skin Issue Summary", "πŸ“Š Possible Causes", "🧴 Skincare Routine", "🌿 Natural Remedies", "πŸ’‘ Tips", "πŸ’¬ Final Note".
43
+ Don't recommend medicines.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  """
45
+ completion = client.chat.completions.create(
46
+ model="llama3-8b-8192",
47
+ messages=[
48
+ {"role": "system", "content": "You are a friendly and expert skincare assistant."},
49
+ {"role": "user", "content": prompt}
50
+ ]
51
+ )
52
+ return completion.choices[0].message.content
53
+
54
+ # --- Generate Button ---
55
+ if st.button("✨ Get My Skincare Plan"):
56
+ with st.spinner("Analyzing your skin profile..."):
57
+ raw = skincare_advice(name, gender, age, skin_concern, description, duration, sensitivity, routine, image)
58
+
59
+ # --- Parse and Beautify Output ---
60
+ lines = raw.strip().split("\n")
61
+ html_output = ""
62
+ section_colors = {
63
+ "🌟": "#E53935", "🩺": "#D81B60", "πŸ“Š": "#5E35B1",
64
+ "🧴": "#F57C00", "🌿": "#43A047", "πŸ’‘": "#039BE5", "πŸ’¬": "#6D4C41"
65
+ }
66
+
67
+ current_section = None
68
+ section_title = ""
69
+ section_buffer = []
70
+
71
+ for line in lines:
72
+ line = line.strip()
73
+ if not line:
74
+ continue
75
+
76
+ emoji = line[:2]
77
+ if emoji in section_colors:
78
+ # Flush previous section
79
+ if current_section:
80
+ html_output += f"<h4 style='color:{section_colors[current_section]}; margin-top:20px;'>{section_title}</h4>\n"
81
+ for item in section_buffer:
82
+ html_output += f"<p style='margin:4px 0;'>{item}</p>\n"
83
+ section_buffer = []
84
+
85
+ current_section = emoji
86
+ section_title = line
87
+ else:
88
+ section_buffer.append(line)
89
+
90
+ # Flush final section
91
+ if current_section:
92
+ html_output += f"<h4 style='color:{section_colors[current_section]}; margin-top:20px;'>{section_title}</h4>\n"
93
+ for item in section_buffer:
94
+ html_output += f"<p style='margin:4px 0;'>{item}</p>\n"
95
+
96
+ # --- Display Final HTML Output ---
97
+ st.markdown(html_output, unsafe_allow_html=True)
98
+
99
+ # --- Show Sticker ---
100
+ st.image(random.choice(stickers), use_column_width=True)