Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,100 +5,93 @@ import base64
|
|
5 |
import gradio as gr
|
6 |
from transformers import CLIPProcessor, CLIPModel
|
7 |
import numpy as np
|
|
|
8 |
|
9 |
# Replace with your own API key
|
10 |
STABLE_DIFFUSION_API_KEY = "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"
|
11 |
-
|
12 |
-
# Load the CLIP model and processor
|
13 |
-
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
14 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
15 |
|
16 |
def get_mood_from_image(image: Image.Image):
|
17 |
-
moods = ["
|
|
|
18 |
|
19 |
-
# Create
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
|
29 |
# Prepare the inputs for the model
|
30 |
-
inputs = processor(text=
|
31 |
|
32 |
# Run the model
|
33 |
logits = model(**inputs).logits_per_image
|
34 |
-
probs = logits.softmax(dim=-1).tolist()
|
35 |
-
|
36 |
-
# Calculate the scores for each mood
|
37 |
-
mood_scores = {}
|
38 |
-
for mood, score in zip(moods, probs[0]):
|
39 |
-
mood_scores[mood] = score
|
40 |
-
print("Mood Scores:", mood_scores)
|
41 |
-
# Select the mood with the highest score
|
42 |
-
selected_mood = max(mood_scores, key=mood_scores.get)
|
43 |
-
|
44 |
-
return selected_mood
|
45 |
-
|
46 |
-
def generate_art(mood):
|
47 |
-
# Implement art generation logic using the Stable Diffusion API
|
48 |
prompt = f"{mood} generative art with vibrant colors and intricate patterns"
|
49 |
...
|
50 |
|
51 |
-
|
52 |
"Authorization": f"Bearer {STABLE_DIFFUSION_API_KEY}",
|
53 |
"Accept": "image/jpeg", # Set the Accept header to receive an image directly
|
54 |
}
|
55 |
-
|
56 |
-
json_data = {
|
57 |
"inputs": prompt
|
58 |
}
|
59 |
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
# Load the image directly from the response content
|
70 |
image = Image.open(BytesIO(response.content))
|
71 |
|
72 |
return image
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
def mood_art_generator(image):
|
84 |
mood = get_mood_from_image(image)
|
85 |
print("Mood:", mood)
|
86 |
if mood:
|
87 |
art = generate_art(mood)
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
else:
|
92 |
-
return None
|
93 |
|
94 |
iface = gr.Interface(
|
95 |
fn=mood_art_generator,
|
96 |
inputs=gr.inputs.Image(shape=(224, 224), image_mode="RGB", source="upload"),
|
97 |
-
outputs=
|
98 |
title="Mood-based Art Generator",
|
99 |
description="Upload an image of yourself and let the AI generate artwork based on your mood.",
|
100 |
allow_flagging=False,
|
101 |
-
analytics_enabled=False
|
|
|
102 |
)
|
103 |
|
104 |
-
iface.launch(
|
|
|
5 |
import gradio as gr
|
6 |
from transformers import CLIPProcessor, CLIPModel
|
7 |
import numpy as np
|
8 |
+
import time
|
9 |
|
10 |
# Replace with your own API key
|
11 |
STABLE_DIFFUSION_API_KEY = "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"
|
|
|
|
|
|
|
12 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
13 |
|
14 |
def get_mood_from_image(image: Image.Image):
|
15 |
+
moods = ["happy", "sad", "angry", "neutral"]
|
16 |
+
prompt = "The mood of the person in this image is: "
|
17 |
|
18 |
+
# Create text prompts for each mood
|
19 |
+
text_inputs = [f"{prompt}{mood}" for mood in moods]
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
|
28 |
# Prepare the inputs for the model
|
29 |
+
inputs = processor(text=text_inputs, images=image, return_tensors="pt", padding=True)
|
30 |
|
31 |
# Run the model
|
32 |
logits = model(**inputs).logits_per_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
prompt = f"{mood} generative art with vibrant colors and intricate patterns"
|
34 |
...
|
35 |
|
36 |
+
request_headers = {
|
37 |
"Authorization": f"Bearer {STABLE_DIFFUSION_API_KEY}",
|
38 |
"Accept": "image/jpeg", # Set the Accept header to receive an image directly
|
39 |
}
|
|
|
|
|
40 |
"inputs": prompt
|
41 |
}
|
42 |
|
43 |
+
while True:
|
44 |
+
# Use the correct variable name (request_headers) in the post request
|
45 |
+
response = requests.post('https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5', headers=request_headers, json=json_data)
|
46 |
|
47 |
+
# Check if the response status is not 200 (OK)
|
48 |
+
if response.status_code == 503: # Model is loading
|
49 |
+
print("Model is loading, waiting for 30 seconds before retrying...")
|
50 |
+
time.sleep(30)
|
51 |
+
continue
|
52 |
+
|
53 |
+
if response.status_code != 200:
|
54 |
+
print(f"Error: API response status code {response.status_code}")
|
55 |
+
print("Response content:")
|
56 |
+
print(response.content)
|
57 |
+
return None
|
58 |
+
|
59 |
+
break
|
60 |
|
61 |
# Load the image directly from the response content
|
62 |
image = Image.open(BytesIO(response.content))
|
63 |
|
64 |
return image
|
65 |
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
|
75 |
def mood_art_generator(image):
|
76 |
mood = get_mood_from_image(image)
|
77 |
print("Mood:", mood)
|
78 |
if mood:
|
79 |
art = generate_art(mood)
|
80 |
+
return art
|
81 |
+
|
82 |
+
|
83 |
else:
|
84 |
+
return None
|
85 |
|
86 |
iface = gr.Interface(
|
87 |
fn=mood_art_generator,
|
88 |
inputs=gr.inputs.Image(shape=(224, 224), image_mode="RGB", source="upload"),
|
89 |
+
outputs=gr.outputs.Image(type="pil"),
|
90 |
title="Mood-based Art Generator",
|
91 |
description="Upload an image of yourself and let the AI generate artwork based on your mood.",
|
92 |
allow_flagging=False,
|
93 |
+
analytics_enabled=False,
|
94 |
+
share=True
|
95 |
)
|
96 |
|
97 |
+
iface.launch()
|