Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 9 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your_groq_api_key_here")
|
| 10 |
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
|
| 12 |
-
# Stickers
|
| 13 |
stickers = [
|
| 14 |
-
"https://
|
| 15 |
-
"https://
|
| 16 |
-
"https://
|
| 17 |
-
"https://
|
| 18 |
-
"https://i.imgur.com/y0ubkNe.png"
|
| 19 |
]
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
st.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|