Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,82 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your_groq_api_key_here")
|
| 7 |
client = Groq(api_key=GROQ_API_KEY)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
st.title("
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 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 |
-
#
|
| 36 |
-
def skincare_advice(name, gender, age,
|
| 37 |
prompt = f"""
|
| 38 |
-
You are a skincare
|
| 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 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
for item in section_buffer:
|
| 94 |
-
html_output += f"<p style='margin:4px 0;'>{item}</p>\n"
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
from groq import Groq
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
+
# Set your Groq API key
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your_groq_api_key_here")
|
| 8 |
client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
|
| 10 |
+
# Emoji-based section colors
|
| 11 |
+
section_colors = {
|
| 12 |
+
"π": "#3B82F6", # Greeting
|
| 13 |
+
"π©Ί": "#10B981", # Skin Issue
|
| 14 |
+
"π§΄": "#F59E0B", # Routine
|
| 15 |
+
"π·": "#8B5CF6", # Image
|
| 16 |
+
"π‘": "#EF4444", # Advice
|
| 17 |
+
}
|
| 18 |
|
| 19 |
+
# Title
|
| 20 |
+
st.title("π SkinSense AI β Your Skincare Helper")
|
| 21 |
|
| 22 |
+
# Input fields
|
| 23 |
+
name = st.text_input("Enter your name")
|
| 24 |
+
gender = st.radio("Select your gender", ["Female", "Male", "Other"])
|
| 25 |
+
age = st.slider("Select your age", 10, 60, 25)
|
| 26 |
+
skin_concern = st.selectbox("Primary Skin Concern", ["Acne", "Dark Spots", "Dryness", "Oiliness", "Wrinkles"])
|
| 27 |
+
description = st.text_area("Describe your skin concern")
|
| 28 |
+
duration = st.selectbox("How long have you had this concern?", ["Less than 1 month", "1-3 months", "More than 3 months"])
|
| 29 |
+
sensitivity = st.radio("Is your skin sensitive?", ["Yes", "No"])
|
| 30 |
+
routine = st.text_area("Describe your current skincare routine (optional)")
|
| 31 |
+
image = st.file_uploader("Upload a picture (optional)", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Send query to Groq
|
| 34 |
+
def skincare_advice(name, gender, age, concern, description, duration, sensitivity, routine, image):
|
| 35 |
prompt = f"""
|
| 36 |
+
You are a professional skincare assistant. Based on the following details, provide a personalized skincare advice in a friendly tone:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
- Name: {name}
|
| 39 |
+
- Gender: {gender}
|
| 40 |
+
- Age: {age}
|
| 41 |
+
- Concern: {concern}
|
| 42 |
+
- Description: {description}
|
| 43 |
+
- Duration: {duration}
|
| 44 |
+
- Sensitive Skin: {sensitivity}
|
| 45 |
+
- Current Routine: {routine if routine else "Not provided"}
|
| 46 |
+
- Image Provided: {"Yes" if image else "No"}
|
| 47 |
|
| 48 |
+
Start with a short greeting, then address the concern, suggest a simple skincare routine, and provide helpful tips. Use emoji headers for each section like π Greeting, π©Ί Skin Issue, π§΄ Routine, π‘ Advice.
|
| 49 |
+
"""
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model="llama3-70b-8192",
|
| 52 |
+
messages=[{"role": "user", "content": prompt}],
|
| 53 |
+
temperature=0.7,
|
| 54 |
+
)
|
| 55 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Submit
|
| 58 |
+
if st.button("Get Advice"):
|
| 59 |
+
if not name or not description:
|
| 60 |
+
st.warning("Please fill in your name and skin concern description.")
|
| 61 |
+
else:
|
| 62 |
+
with st.spinner("Analyzing your skin concern..."):
|
| 63 |
+
raw_response = skincare_advice(name, gender, age, skin_concern, description, duration, sensitivity, routine, image)
|
| 64 |
|
| 65 |
+
# Clean HTML output
|
| 66 |
+
html_output = ""
|
| 67 |
+
lines = raw_response.split("\n")
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
for line in lines:
|
| 70 |
+
# Remove all existing HTML tags from model's response
|
| 71 |
+
clean_line = re.sub(r"<.*?>", "", line.strip())
|
| 72 |
+
if not clean_line:
|
| 73 |
+
continue
|
| 74 |
+
emoji = clean_line[:2]
|
| 75 |
+
if emoji in section_colors:
|
| 76 |
+
color = section_colors.get(emoji, "#000")
|
| 77 |
+
html_output += f"<h4 style='color:{color}; margin-top:20px;'>{clean_line}</h4>\n"
|
| 78 |
+
else:
|
| 79 |
+
html_output += f"<p style='color:#111; margin:5px 0;'>{clean_line}</p>\n"
|
| 80 |
|
| 81 |
+
# Display HTML in Streamlit
|
| 82 |
+
st.markdown(html_output, unsafe_allow_html=True)
|