Friday / app.py
ans123's picture
Update app.py
2dcd8c5 verified
raw
history blame
14 kB
import gradio as gr
import pandas as pd
from groq import Groq
# Initialize the Groq client with your API key
client = Groq(api_key="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")
# Define the system message for the model
system_message = {
"role": "system",
"content": "You are an experienced Fashion designer who starts conversation with proper greet, giving valuable and catchy fashion advices and suggestions, stays to the point and precise, asks questions only if the user have any concern over your provided suggestions, taking inputs like name, age, gender, location, ethnicity, height, weight"
}
# Function to reset the chat
def reset_chat():
return [], "New Chat"
# Function to handle the questionnaire submission
def submit_questionnaire(name, age, location, gender, ethnicity, height, weight,
style_preference, color_palette, everyday_style,
preferred_prints, season_preference, outfit_priority,
experiment_with_trends, accessories, fit_preference,
material_preference, top_preference, bottom_preference,
outerwear_preference, footwear_preference, dress_frequency,
layering_preference, jeans_fit, formal_wear_frequency,
sportswear_preference, party_outfit, confidence_in_style,
follow_fashion_trends, look_for_inspiration,
wardrobe_satisfaction, unique_style, outfit_struggle,
fashion_preference, gender_neutral_clothing,
special_occasion_attire, trendsetter,
ai_usefulness, trust_in_ai, ai_preference,
ai_usage_frequency, ai_match_preferences,
ai_recommendation, ai_understanding_style,
more_personalized_recommendations, event_suggestions,
ai_improvements):
# Store questionnaire responses in a DataFrame
questionnaire_data = {
"Name": name,
"Age": age,
"Location": location,
"Gender": gender,
"Ethnicity": ethnicity,
"Height": height,
"Weight": weight,
"Style Preference": style_preference,
"Color Palette": color_palette,
"Everyday Style": everyday_style,
"Preferred Prints": preferred_prints,
"Season Preference": season_preference,
"Outfit Priority": outfit_priority,
"Experiment with Trends": experiment_with_trends,
"Accessories": accessories,
"Fit Preference": fit_preference,
"Material Preference": material_preference,
"Top Preference": top_preference,
"Bottom Preference": bottom_preference,
"Outerwear Preference": outerwear_preference,
"Footwear Preference": footwear_preference,
"Dress Frequency": dress_frequency,
"Layering Preference": layering_preference,
"Jeans Fit": jeans_fit,
"Formal Wear Frequency": formal_wear_frequency,
"Sportswear Preference": sportswear_preference,
"Party Outfit": party_outfit,
"Confidence in Style": confidence_in_style,
"Follow Fashion Trends": follow_fashion_trends,
"Look for Inspiration": look_for_inspiration,
"Wardrobe Satisfaction": wardrobe_satisfaction,
"Unique Style": unique_style,
"Outfit Struggle": outfit_struggle,
"Fashion Preference": fashion_preference,
"Gender Neutral Clothing": gender_neutral_clothing,
"Special Occasion Attire": special_occasion_attire,
"Trendsetter": trendsetter,
"AI Usefulness": ai_usefulness,
"Trust in AI": trust_in_ai,
"AI Preference": ai_preference,
"AI Usage Frequency": ai_usage_frequency,
"AI Match Preferences": ai_match_preferences,
"AI Recommendation": ai_recommendation,
"AI Understanding Style": ai_understanding_style,
"More Personalized Recommendations": more_personalized_recommendations,
"Event Suggestions": event_suggestions,
"AI Improvements": ai_improvements
}
df = pd.DataFrame([questionnaire_data]) # Create DataFrame from dictionary
# Append to CSV file
df.to_csv("questionnaire_responses.csv", mode='a', header=not pd.io.common.file_exists("questionnaire_responses.csv"), index=False)
return "Thank you for completing the questionnaire!"
# Function to handle chat
def chat(user_input, messages, name, age, location, gender, ethnicity, height, weight):
if user_input:
# Create a user profile string
user_profile_string = (
f"User profile: Name: {name}, Age: {age}, Location: {location}, "
f"Gender: {gender}, Ethnicity: {ethnicity}, Height: {height}, Weight: {weight}"
)
# Prepare messages for the API call, including the profile and the conversation history
messages.append({"role": "user", "content": user_input})
messages.append(system_message)
messages.append({"role": "user", "content": user_profile_string})
try:
# Generate a response from the Groq API
completion = client.chat.completions.create(
model="llama3-8b-8192",
messages=messages,
temperature=1,
max_tokens=1024,
top_p=1,
stream=False,
)
# Ensure response is valid
if completion.choices and len(completion.choices) > 0:
response_content = completion.choices[0].message.content
else:
response_content = "Sorry, I couldn't generate a response."
except Exception as e:
response_content = f"Error: {str(e)}"
# Store assistant response in the chat history
messages.append({"role": "assistant", "content": response_content})
return messages, response_content
return messages, ""
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## Fashion Assistant Chatbot")
# Sidebar for user inputs
with gr.Row():
with gr.Column():
name = gr.Textbox(label="Name")
age = gr.Number(label="Age", value=25, minimum=1, maximum=100)
location = gr.Textbox(label="Location")
gender = gr.Radio(label="Gender", choices=["Male", "Female", "Other"])
ethnicity = gr.Radio(label="Ethnicity", choices=["Asian", "Black", "Hispanic", "White", "Other"])
height = gr.Number(label="Height (cm)", value=170, minimum=50, maximum=250)
weight = gr.Number(label="Weight (kg)", value=70, minimum=20, maximum=200)
with gr.Column():
submit_btn = gr.Button("Submit Questionnaire")
reset_btn = gr.Button("Reset Chat")
# Questionnaire responses
style_preference = gr.Radio(label="Which style do you prefer the most?", choices=["Casual", "Formal", "Streetwear", "Athleisure", "Baggy"])
color_palette = gr.Radio(label="What color palette do you wear often?", choices=["Neutrals", "Bright Colors", "Pastels", "Dark Shades"])
everyday_style = gr.Radio(label="How would you describe your everyday style?", choices=["Relaxed", "Trendy", "Elegant", "Bold"])
preferred_prints = gr.Radio(label="What type of prints do you like?", choices=["Solid", "Stripes", "Floral", "Geometric", "Animal Print"])
season_preference = gr.Radio(label="Which season influences your wardrobe the most?", choices=["Spring", "Summer", "Fall", "Winter"])
outfit_priority = gr.Radio(label="What do you prioritize when choosing an outfit?", choices=["Comfort", "Style", "Affordability", "Brand"])
experiment_with_trends = gr.Radio(label="How often do you experiment with new trends?", choices=["Always", "Sometimes", "Rarely", "Never"])
accessories = gr.Radio(label="What kind of accessories do you usually wear?", choices=["Watches", "Rings", "Necklaces", "Bracelets", "Earrings"])
fit_preference = gr.Radio(label="What fit do you prefer in clothes?", choices=["Loose", "Tailored", "Fitted", "Oversized"])
material_preference = gr.Radio(label="Which material do you prefer?", choices=["Cotton", "Linen", "Silk", "Denim", "Wool"])
# More preferences
top_preference = gr.Radio(label="Preferred Top Styles", choices=["T-shirts", "Shirts", "Sweaters", "Blouses"])
bottom_preference = gr.Radio(label="Preferred Bottom Styles", choices=["Jeans", "Shorts", "Skirts", "Trousers"])
outerwear_preference = gr.Radio(label="Preferred Outerwear", choices=["Jackets", "Coats", "Hoodies"])
footwear_preference = gr.Radio(label="Preferred Footwear", choices=["Sneakers", "Boots", "Sandals"])
dress_frequency = gr.Radio(label="How often do you wear dresses?", choices=["Daily", "Weekly", "Occasionally", "Rarely"])
layering_preference = gr.Radio(label="Do you like layering?", choices=["Yes", "No"])
jeans_fit = gr.Radio(label="Preferred Jeans Fit", choices=["Skinny", "Straight", "Baggy"])
formal_wear_frequency = gr.Radio(label="How often do you wear formal attire?", choices=["Daily", "Weekly", "Occasionally", "Rarely"])
sportswear_preference = gr.Radio(label="Preferred Sportswear Style", choices=["Athletic", "Casual", "Fashionable"])
party_outfit = gr.Radio(label="Preferred Outfit for Parties", choices=["Dresses", "Suits", "Casual Wear"])
confidence_in_style = gr.Radio(label="How confident do you feel in your style?", choices=["Very Confident", "Somewhat Confident", "Not Confident"])
follow_fashion_trends = gr.Radio(label="Do you follow fashion trends?", choices=["Always", "Sometimes", "Never"])
look_for_inspiration = gr.Radio(label="Where do you look for fashion inspiration?", choices=["Social Media", "Fashion Magazines", "Friends", "Other"])
wardrobe_satisfaction = gr.Radio(label="Are you satisfied with your wardrobe?", choices=["Yes", "No"])
unique_style = gr.Radio(label="Do you consider your style unique?", choices=["Yes", "No"])
outfit_struggle = gr.Radio(label="Do you struggle to put outfits together?", choices=["Yes", "No"])
fashion_preference = gr.Radio(label="What is your fashion preference?", choices=["Trendy", "Classic", "Bohemian", "Minimalist"])
gender_neutral_clothing = gr.Radio(label="Do you wear gender-neutral clothing?", choices=["Yes", "No"])
special_occasion_attire = gr.Radio(label="Do you have specific attire for special occasions?", choices=["Yes", "No"])
trendsetter = gr.Radio(label="Do you consider yourself a trendsetter?", choices=["Yes", "No"])
ai_usefulness = gr.Radio(label="How useful do you find AI in fashion?", choices=["Very Useful", "Somewhat Useful", "Not Useful"])
trust_in_ai = gr.Radio(label="Do you trust AI fashion recommendations?", choices=["Yes", "No"])
ai_preference = gr.Radio(label="Do you prefer AI recommendations?", choices=["Yes", "No"])
ai_usage_frequency = gr.Radio(label="How often do you use AI for fashion?", choices=["Daily", "Weekly", "Rarely", "Never"])
ai_match_preferences = gr.Radio(label="Does AI match your preferences?", choices=["Always", "Sometimes", "Rarely", "Never"])
ai_recommendation = gr.Radio(label="How satisfied are you with AI recommendations?", choices=["Very Satisfied", "Satisfied", "Dissatisfied"])
ai_understanding_style = gr.Radio(label="Does AI understand your style?", choices=["Very Well", "Somewhat", "Not Well"])
more_personalized_recommendations = gr.Radio(label="Do you want more personalized recommendations?", choices=["Yes", "No"])
event_suggestions = gr.Radio(label="Would you like event-specific suggestions?", choices=["Yes", "No"])
ai_improvements = gr.Radio(label="How can AI improve?", choices=["More Options", "Better Quality", "Faster Suggestions"])
# Chat functionality
chatbox = gr.Chatbot(type='messages')
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
# Connect the buttons to their respective functions
submit_btn.click(submit_questionnaire, inputs=[name, age, location, gender, ethnicity, height, weight,
style_preference, color_palette, everyday_style,
preferred_prints, season_preference, outfit_priority,
experiment_with_trends, accessories, fit_preference,
material_preference, top_preference, bottom_preference,
outerwear_preference, footwear_preference, dress_frequency,
layering_preference, jeans_fit, formal_wear_frequency,
sportswear_preference, party_outfit, confidence_in_style,
follow_fashion_trends, look_for_inspiration,
wardrobe_satisfaction, unique_style, outfit_struggle,
fashion_preference, gender_neutral_clothing,
special_occasion_attire, trendsetter,
ai_usefulness, trust_in_ai, ai_preference,
ai_usage_frequency, ai_match_preferences,
ai_recommendation, ai_understanding_style,
more_personalized_recommendations, event_suggestions,
ai_improvements], outputs="text")
reset_btn.click(reset_chat, outputs=[chatbox, "title"])
user_input.submit(chat, inputs=[user_input, chatbox, name, age, location, gender, ethnicity, height, weight], outputs=[chatbox, user_input])
# Run the app
demo.launch()