Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -11,20 +11,79 @@ import base64
|
|
11 |
# Set up Replicate API key from environment variable
|
12 |
os.environ['REPLICATE_API_TOKEN'] = os.getenv('REPLICATE_API_TOKEN')
|
13 |
|
14 |
-
def
|
15 |
"""
|
16 |
-
Upload image to
|
17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
-
# Convert PIL image to base64
|
20 |
buffered = BytesIO()
|
21 |
image.save(buffered, format="PNG")
|
22 |
buffered.seek(0)
|
23 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
24 |
|
25 |
-
# Imgur API (anonymous upload)
|
26 |
headers = {
|
27 |
-
'Authorization': 'Client-ID 0d90e8a3e7d8b4e'
|
28 |
}
|
29 |
|
30 |
response = requests.post(
|
@@ -35,9 +94,11 @@ def upload_to_imgur(image):
|
|
35 |
|
36 |
if response.status_code == 200:
|
37 |
data = response.json()
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
return None
|
42 |
|
43 |
def save_temp_image(image):
|
@@ -63,45 +124,63 @@ def process_images(prompt, image1, image2=None):
|
|
63 |
# Prepare image URLs list
|
64 |
image_urls = []
|
65 |
|
66 |
-
#
|
67 |
-
|
|
|
68 |
if url1:
|
69 |
image_urls.append(url1)
|
|
|
70 |
else:
|
71 |
-
# If
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
|
|
75 |
if image2:
|
76 |
-
|
|
|
77 |
if url2:
|
78 |
image_urls.append(url2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
# Prepare input exactly as shown in
|
81 |
input_data = {
|
82 |
"prompt": prompt,
|
83 |
"image_input": image_urls
|
84 |
}
|
85 |
|
86 |
-
print(f"Sending to Replicate: {
|
87 |
|
88 |
-
# Run the model
|
89 |
output = replicate.run(
|
90 |
"google/nano-banana",
|
91 |
input=input_data
|
92 |
)
|
93 |
|
94 |
-
# Process output
|
95 |
if output is None:
|
96 |
return None, "โ No output received from model"
|
97 |
|
98 |
# Try different methods to get the image
|
99 |
try:
|
100 |
-
# Method 1: Using read() method
|
101 |
if hasattr(output, 'read'):
|
102 |
img_data = output.read()
|
103 |
img = Image.open(BytesIO(img_data))
|
104 |
-
return img, "โ
|
105 |
except:
|
106 |
pass
|
107 |
|
@@ -112,7 +191,7 @@ def process_images(prompt, image1, image2=None):
|
|
112 |
response = requests.get(output_url, timeout=30)
|
113 |
if response.status_code == 200:
|
114 |
img = Image.open(BytesIO(response.content))
|
115 |
-
return img, "โ
|
116 |
except:
|
117 |
pass
|
118 |
|
@@ -127,14 +206,14 @@ def process_images(prompt, image1, image2=None):
|
|
127 |
response = requests.get(output_url, timeout=30)
|
128 |
if response.status_code == 200:
|
129 |
img = Image.open(BytesIO(response.content))
|
130 |
-
return img, "โ
|
131 |
|
132 |
return None, f"โ Could not process output. Output type: {type(output)}"
|
133 |
|
134 |
except replicate.exceptions.ReplicateError as e:
|
135 |
error_msg = str(e)
|
136 |
if "502" in error_msg:
|
137 |
-
return None, "โ Server error (502). The model might be unavailable.
|
138 |
elif "404" in error_msg:
|
139 |
return None, "โ Model 'google/nano-banana' not found. Please verify the model exists."
|
140 |
elif "401" in error_msg or "403" in error_msg:
|
@@ -190,7 +269,7 @@ css = """
|
|
190 |
# Build the Gradio interface
|
191 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
192 |
gr.HTML("""
|
193 |
-
<div class="header-text"
|
194 |
<div class="description-text">
|
195 |
Upload 1-2 images and describe how you want them styled.
|
196 |
The AI will create a beautiful transformation using nano-banana model!
|
@@ -230,7 +309,8 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
230 |
#### ๐ก Tips:
|
231 |
- Upload high-quality images for best results
|
232 |
- Be specific in your style description
|
233 |
-
- Model: google/nano-banana
|
|
|
234 |
""")
|
235 |
|
236 |
with gr.Column(scale=1):
|
|
|
11 |
# Set up Replicate API key from environment variable
|
12 |
os.environ['REPLICATE_API_TOKEN'] = os.getenv('REPLICATE_API_TOKEN')
|
13 |
|
14 |
+
def upload_image_to_hosting(image):
|
15 |
"""
|
16 |
+
Upload image to multiple hosting services with fallback
|
17 |
"""
|
18 |
+
# Method 1: Try imgbb.com (more reliable than Imgur)
|
19 |
+
try:
|
20 |
+
buffered = BytesIO()
|
21 |
+
image.save(buffered, format="PNG")
|
22 |
+
buffered.seek(0)
|
23 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
24 |
+
|
25 |
+
# imgbb API (free tier)
|
26 |
+
response = requests.post(
|
27 |
+
"https://api.imgbb.com/1/upload",
|
28 |
+
data={
|
29 |
+
'key': '6d207e02198a847aa98d0a2a901485a5', # Free API key
|
30 |
+
'image': img_base64,
|
31 |
+
}
|
32 |
+
)
|
33 |
+
|
34 |
+
if response.status_code == 200:
|
35 |
+
data = response.json()
|
36 |
+
if data.get('success'):
|
37 |
+
return data['data']['url']
|
38 |
+
except Exception as e:
|
39 |
+
print(f"imgbb upload failed: {e}")
|
40 |
+
|
41 |
+
# Method 2: Try freeimage.host
|
42 |
+
try:
|
43 |
+
buffered = BytesIO()
|
44 |
+
image.save(buffered, format="PNG")
|
45 |
+
buffered.seek(0)
|
46 |
+
|
47 |
+
files = {'source': buffered}
|
48 |
+
response = requests.post(
|
49 |
+
"https://freeimage.host/api/1/upload",
|
50 |
+
files=files,
|
51 |
+
data={'key': '6d207e02198a847aa98d0a2a901485a5'}
|
52 |
+
)
|
53 |
+
|
54 |
+
if response.status_code == 200:
|
55 |
+
data = response.json()
|
56 |
+
if 'image' in data and 'url' in data['image']:
|
57 |
+
return data['image']['url']
|
58 |
+
except Exception as e:
|
59 |
+
print(f"freeimage.host upload failed: {e}")
|
60 |
+
|
61 |
+
# Method 3: Try 0x0.st (simple and reliable)
|
62 |
+
try:
|
63 |
+
buffered = BytesIO()
|
64 |
+
image.save(buffered, format="PNG")
|
65 |
+
buffered.seek(0)
|
66 |
+
|
67 |
+
files = {'file': ('image.png', buffered, 'image/png')}
|
68 |
+
response = requests.post(
|
69 |
+
"https://0x0.st",
|
70 |
+
files=files
|
71 |
+
)
|
72 |
+
|
73 |
+
if response.status_code == 200:
|
74 |
+
return response.text.strip()
|
75 |
+
except Exception as e:
|
76 |
+
print(f"0x0.st upload failed: {e}")
|
77 |
+
|
78 |
+
# Method 4: Original Imgur as last resort
|
79 |
try:
|
|
|
80 |
buffered = BytesIO()
|
81 |
image.save(buffered, format="PNG")
|
82 |
buffered.seek(0)
|
83 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
84 |
|
|
|
85 |
headers = {
|
86 |
+
'Authorization': 'Client-ID 0d90e8a3e7d8b4e'
|
87 |
}
|
88 |
|
89 |
response = requests.post(
|
|
|
94 |
|
95 |
if response.status_code == 200:
|
96 |
data = response.json()
|
97 |
+
if data.get('success'):
|
98 |
+
return data['data']['link']
|
99 |
+
except Exception as e:
|
100 |
+
print(f"Imgur upload failed: {e}")
|
101 |
+
|
102 |
return None
|
103 |
|
104 |
def save_temp_image(image):
|
|
|
124 |
# Prepare image URLs list
|
125 |
image_urls = []
|
126 |
|
127 |
+
# Upload first image
|
128 |
+
print("Uploading first image...")
|
129 |
+
url1 = upload_image_to_hosting(image1)
|
130 |
if url1:
|
131 |
image_urls.append(url1)
|
132 |
+
print(f"Image 1 uploaded: {url1}")
|
133 |
else:
|
134 |
+
# If all hosting services fail, use base64 data URI as fallback
|
135 |
+
buffered = BytesIO()
|
136 |
+
image1.save(buffered, format="PNG")
|
137 |
+
buffered.seek(0)
|
138 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
139 |
+
data_uri = f"data:image/png;base64,{img_base64}"
|
140 |
+
image_urls.append(data_uri)
|
141 |
+
print("Using base64 data URI for image 1")
|
142 |
|
143 |
+
# Upload second image if provided
|
144 |
if image2:
|
145 |
+
print("Uploading second image...")
|
146 |
+
url2 = upload_image_to_hosting(image2)
|
147 |
if url2:
|
148 |
image_urls.append(url2)
|
149 |
+
print(f"Image 2 uploaded: {url2}")
|
150 |
+
else:
|
151 |
+
buffered = BytesIO()
|
152 |
+
image2.save(buffered, format="PNG")
|
153 |
+
buffered.seek(0)
|
154 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
155 |
+
data_uri = f"data:image/png;base64,{img_base64}"
|
156 |
+
image_urls.append(data_uri)
|
157 |
+
print("Using base64 data URI for image 2")
|
158 |
|
159 |
+
# Prepare input exactly as shown in the reference
|
160 |
input_data = {
|
161 |
"prompt": prompt,
|
162 |
"image_input": image_urls
|
163 |
}
|
164 |
|
165 |
+
print(f"Sending to Replicate: prompt='{prompt}', images={len(image_urls)}")
|
166 |
|
167 |
+
# Run the model
|
168 |
output = replicate.run(
|
169 |
"google/nano-banana",
|
170 |
input=input_data
|
171 |
)
|
172 |
|
173 |
+
# Process output
|
174 |
if output is None:
|
175 |
return None, "โ No output received from model"
|
176 |
|
177 |
# Try different methods to get the image
|
178 |
try:
|
179 |
+
# Method 1: Using read() method
|
180 |
if hasattr(output, 'read'):
|
181 |
img_data = output.read()
|
182 |
img = Image.open(BytesIO(img_data))
|
183 |
+
return img, "โ
Free Nano Banana generated your image successfully! ๐"
|
184 |
except:
|
185 |
pass
|
186 |
|
|
|
191 |
response = requests.get(output_url, timeout=30)
|
192 |
if response.status_code == 200:
|
193 |
img = Image.open(BytesIO(response.content))
|
194 |
+
return img, "โ
Free Nano Banana generated your image successfully! ๐"
|
195 |
except:
|
196 |
pass
|
197 |
|
|
|
206 |
response = requests.get(output_url, timeout=30)
|
207 |
if response.status_code == 200:
|
208 |
img = Image.open(BytesIO(response.content))
|
209 |
+
return img, "โ
Free Nano Banana generated your image successfully! ๐"
|
210 |
|
211 |
return None, f"โ Could not process output. Output type: {type(output)}"
|
212 |
|
213 |
except replicate.exceptions.ReplicateError as e:
|
214 |
error_msg = str(e)
|
215 |
if "502" in error_msg:
|
216 |
+
return None, "โ Server error (502). The nano-banana model might be temporarily unavailable."
|
217 |
elif "404" in error_msg:
|
218 |
return None, "โ Model 'google/nano-banana' not found. Please verify the model exists."
|
219 |
elif "401" in error_msg or "403" in error_msg:
|
|
|
269 |
# Build the Gradio interface
|
270 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
271 |
gr.HTML("""
|
272 |
+
<div class="header-text">๐ Free Nano Banana</div>
|
273 |
<div class="description-text">
|
274 |
Upload 1-2 images and describe how you want them styled.
|
275 |
The AI will create a beautiful transformation using nano-banana model!
|
|
|
309 |
#### ๐ก Tips:
|
310 |
- Upload high-quality images for best results
|
311 |
- Be specific in your style description
|
312 |
+
- Model: google/nano-banana ๐
|
313 |
+
- Free image hosting included!
|
314 |
""")
|
315 |
|
316 |
with gr.Column(scale=1):
|