CoolFace
Apppublic

Kyan14/First_Working_Version

sourceHugging Faceccupdated 3y agoView on Hugging Face
0likes
app.py102 linesDownload Raw Back to root
1import requests2from PIL import Image3from io import BytesIO4import base645import gradio as gr6from transformers import CLIPProcessor, CLIPModel7import numpy as np8import time9 10# Replace with your own API key11STABLE_DIFFUSION_API_KEY = "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"12 13# Load the CLIP model and processor14model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")15processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")16 17def get_mood_from_image(image: Image.Image):18    moods = ["scared", "angry", "happy", "sad", "disgusted", "surprised"]19    20    # Create unique prompts for each mood21    prompts = [22        "The emotion conveyed by this image is fear. The person looks scared and tense.",23        "The emotion conveyed by this image is anger. The person looks furious and irritated.",24        "The emotion conveyed by this image is happy. The person looks happy and cheerful.",25        "The emotion conveyed by this image is sadness. The person looks unhappy and gloomy.",26        "The emotion conveyed by this image is disgust. The person looks repulsed and sickened.",27        "The emotion conveyed by this image is surprise. The person looks astonished and amazed.",28    ]29    30    # Prepare the inputs for the model31    inputs = processor(text=prompts, images=image, return_tensors="pt", padding=True)32    33    # Run the model34    logits = model(**inputs).logits_per_image35    probs = logits.softmax(dim=-1).tolist()36 37    # Calculate the scores for each mood38    mood_scores = {}39    for mood, score in zip(moods, probs[0]):40        mood_scores[mood] = score41    print("Mood Scores:", mood_scores)42    # Select the mood with the highest score43    selected_mood = max(mood_scores, key=mood_scores.get)44 45    return selected_mood46 47def generate_art(mood):48    # Implement art generation logic using the Stable Diffusion API49    prompt = f"{mood} generative art with vibrant colors and intricate patterns ({str(np.random.randint(1, 10000))})"50    51    headers = {52        "Authorization": f"Bearer {STABLE_DIFFUSION_API_KEY}",53        "Accept": "image/jpeg",  # Set the Accept header to receive an image directly54    }55 56    json_data = {57        "inputs": prompt58    }59 60    while True:61        response = requests.post('https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5', headers=headers, json=json_data)62 63        if response.status_code == 503:64            print("Model is loading, waiting for 30 seconds before retrying...")65            time.sleep(30)66            continue67 68        if response.status_code != 200:69            print(f"Error: API response status code {response.status_code}")70            print("Response content:")71            print(response.content)72            return None73 74        break75 76    image = Image.open(BytesIO(response.content))77 78    return image79 80 81def mood_art_generator(image):82    mood = get_mood_from_image(image)83    print("Mood:", mood)84    if mood:85        art = generate_art(mood)86        output_text = f"You seem to be {mood}. Here's an artwork representing it!"87        return art, output_text88    else:89        return None, "Failed to generate artwork."90 91iface = gr.Interface(92    fn=mood_art_generator,93    inputs=gr.inputs.Image(shape=(224, 224), image_mode="RGB", source="upload"),94    outputs=[gr.outputs.Image(type="pil"), gr.outputs.Textbox()],95    title="Mood-based Art Generator",96    description="Upload an image of yourself and let the AI generate artwork based on your mood.",97    allow_flagging=False,98    analytics_enabled=False,99    share=True100)101 102iface.launch()