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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -83
app.py CHANGED
@@ -1,100 +1,82 @@
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)
 
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)