Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines567891011121314151617181920212223242526272829303132334 unmodified linesimport gradio as grfrom transformers import CLIPProcessor, CLIPModelimport numpy as np# Replace with your own API keySTABLE_DIFFUSION_API_KEY = "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"# Load the CLIP model and processormodel = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")def get_mood_from_image(image: Image.Image):moods = ["fear", "anger", "joy", "sadness", "disgust", "surprise"]# Create unique prompts for each moodprompts = ["The emotion conveyed by this image is fear. The person looks scared and tense.","The emotion conveyed by this image is anger. The person looks furious and irritated.","The emotion conveyed by this image is joy. The person looks happy and cheerful.","The emotion conveyed by this image is sadness. The person looks unhappy and gloomy.","The emotion conveyed by this image is disgust. The person looks repulsed and sickened.","The emotion conveyed by this image is surprise. The person looks astonished and amazed.",]# Prepare the inputs for the modelinputs = processor(text=prompts, images=image, return_tensors="pt", padding=True)# Run the modellogits = model(**inputs).log4 unmodified lines56789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596974 unmodified linesimport gradio as grfrom transformers import CLIPProcessor, CLIPModelimport numpy as npimport time# Replace with your own API keySTABLE_DIFFUSION_API_KEY = "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"processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")def get_mood_from_image(image: Image.Image):moods = ["happy", "sad", "angry", "neutral"]prompt = "The mood of the person in this image is: "# Create text prompts for each moodtext_inputs = [f"{prompt}{mood}" for mood in moods]# Prepare the inputs for the modelinputs = processor(text=text_inputs, images=image, return_tensors="pt", padding=True)# Run the modellogits = model(**inputs).logits_per_imageprompt = f"{mood} generative art with vibrant colors and intricate patterns"...request_headers = {"Authorization": f"Bearer {STABLE_DIFFUSION_API_KEY}","Accept": "image/jpeg", # Set the Accept header to receive an image directly}"inputs": prompt}while True:# Use the correct variable name (request_headers) in the post requestresponse = requests.post('https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5', headers=request_headers, json=json_data)# Check if the response status is not 200 (OK)if response.status_code == 503: # Model is loadingprint("Model is loading, waiting for 30 seconds before retrying...")time.sleep(30)continueif response.status_code != 200:print(f"Error: API response status code {response.status_code}")print("Response content:")print(response.content)return Nonebreak# Load the image directly from the response contentimage = Image.open(BytesIO(response.content))return imagedef mood_art_generator(image):mood = get_mood_from_image(image)print("Mood:", mood)if mood:art = generate_art(mood)return artelse:return Noneiface = gr.Interface(fn=mood_art_generator,inputs=gr.inputs.Image(shape=(224, 224), image_mode="RGB", source="upload"),outputs=gr.outputs.Image(type="pil"),title="Mood-based Art Generator",description="Upload an image of yourself and let the AI generate artwork based on your mood.",allow_flagging=False,analytics_enabled=False,share=True)iface.launch()
app.py+79-15