import gradio as gr import requests from PIL import Image import base64 from io import BytesIO import logging # Set up logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) # Define the API endpoints skin_metrics_url = 'http://127.0.1:5000/upload' recommendation_url = 'http://127.0.1:5000/recommend' def capture_face_image(file): try: if file is None: return "Please upload an image", None # Open the uploaded image file (directly using the file object passed from Gradio) image = Image.open(file) # Convert image to base64 buffered = BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') # Send to backend for skin analysis try: response = requests.put( skin_metrics_url, json={'file': img_str}, timeout=10 # Add timeout ) response.raise_for_status() # Raise exception for bad status codes skin_data = response.json() # Skin analysis data from backend return skin_data, image # Return both the analysis data and image except requests.exceptions.RequestException as e: logger.error(f"API request failed: {str(e)}") return f"Error in skin analysis: {str(e)}", image except Exception as e: logger.error(f"Image processing error: {str(e)}") return f"Error processing image: {str(e)}", None def get_recommendations(tone, skin_type, concerns): try: # Define the full list of features that the model expects (18 in total) all_features = [ 'normal', 'dry', 'oily', 'combination', 'acne', 'sensitive', 'fine lines', 'wrinkles', 'redness', 'dull', 'pore', 'pigmentation', 'blackheads', 'whiteheads', 'blemishes', 'dark circles', 'eye bags', 'dark spots' ] # Prepare the features dict with all the values set to 0 initially features_dict = {feature: 0 for feature in all_features} # Update the dictionary with features selected by the user for concern in concerns: if concern in features_dict: features_dict[concern] = 1 # Prepare the data to send to the backend data = { 'tone': tone, 'type': skin_type, 'features': features_dict # Send the full features dictionary } # Log the data being sent to the backend logger.info(f"Sending recommendation request with data: {data}") # Make a request to the backend for recommendations try: response = requests.put( recommendation_url, json=data, timeout=10 # Add timeout ) response.raise_for_status() # Log the response status code and content logger.info(f"Recommendation API response status: {response.status_code}") recommendations = response.json() # Receive recommendations from backend # Log the recommendations received from the backend logger.info(f"Received recommendations: {recommendations}") # Generate HTML content for cards product_cards = "" if recommendations.get("general"): for category, products in recommendations["general"].items(): for product in products: card_html = f"""
""" product_cards += card_html # Wrap the product cards in a responsive grid grid_html = f"""