Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -24,87 +24,84 @@ def process_images(prompt, image1, image2=None):
|
|
24 |
|
25 |
try:
|
26 |
import tempfile
|
27 |
-
import
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
|
32 |
-
#
|
|
|
|
|
|
|
|
|
33 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp1:
|
34 |
-
image1.save(tmp1.name)
|
|
|
35 |
with open(tmp1.name, 'rb') as f:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
os.unlink(tmp1.name) # Clean up temp file
|
40 |
|
41 |
-
# Process second image if provided
|
42 |
if image2:
|
43 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp2:
|
44 |
-
image2.save(tmp2.name)
|
45 |
with open(tmp2.name, 'rb') as f:
|
46 |
-
|
47 |
-
|
48 |
os.unlink(tmp2.name)
|
49 |
|
50 |
-
status_message = "π¨ Processing your images..."
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
#
|
56 |
-
|
57 |
-
"stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
|
58 |
-
input={
|
59 |
-
"prompt": prompt,
|
60 |
-
"image": image_inputs[0] if image_inputs else None,
|
61 |
-
"num_outputs": 1,
|
62 |
-
"guidance_scale": 7.5,
|
63 |
-
"num_inference_steps": 50
|
64 |
-
}
|
65 |
-
)
|
66 |
-
except:
|
67 |
-
# Fallback to a simpler text-to-image if image input fails
|
68 |
-
output = replicate.run(
|
69 |
-
"stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
|
70 |
-
input={
|
71 |
-
"prompt": prompt,
|
72 |
-
"num_outputs": 1
|
73 |
-
}
|
74 |
-
)
|
75 |
|
76 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
output_url = None
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
output_url = output
|
82 |
elif isinstance(output, str):
|
83 |
-
# If output is already a string URL
|
84 |
output_url = output
|
85 |
-
elif
|
86 |
-
|
87 |
-
output_url = output.url()
|
88 |
-
elif hasattr(output, '__iter__'):
|
89 |
-
# If output is iterable, try to get first item
|
90 |
-
try:
|
91 |
-
output_url = next(iter(output))
|
92 |
-
except:
|
93 |
-
pass
|
94 |
|
95 |
if not output_url:
|
96 |
-
return None, "β Error: No image
|
97 |
|
98 |
-
# Download
|
99 |
-
|
100 |
-
if
|
101 |
-
|
102 |
-
|
103 |
else:
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
except Exception as e:
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
# Create Gradio interface with gradient theme
|
110 |
css = """
|
@@ -238,22 +235,22 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
238 |
```bash
|
239 |
export REPLICATE_API_TOKEN="your_api_token_here"
|
240 |
```
|
|
|
241 |
|
242 |
2. **Install Required Packages:**
|
243 |
```bash
|
244 |
pip install gradio replicate pillow requests
|
245 |
```
|
246 |
|
247 |
-
3. **
|
248 |
-
- `
|
249 |
-
- `
|
250 |
-
-
|
251 |
-
- Replace the model in the code with your preferred model
|
252 |
|
253 |
-
4. **Note:**
|
254 |
-
-
|
255 |
-
-
|
256 |
-
- Check
|
257 |
|
258 |
### π Security:
|
259 |
- API keys are managed through environment variables
|
|
|
24 |
|
25 |
try:
|
26 |
import tempfile
|
27 |
+
from replicate.client import Client
|
28 |
|
29 |
+
# Initialize Replicate client
|
30 |
+
client = Client(api_token=os.getenv('REPLICATE_API_TOKEN'))
|
31 |
|
32 |
+
# Upload images to get URLs
|
33 |
+
# Replicate needs actual URLs, so we need to upload the images first
|
34 |
+
image_urls = []
|
35 |
+
|
36 |
+
# Save and create file handles for upload
|
37 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp1:
|
38 |
+
image1.save(tmp1.name, 'PNG')
|
39 |
+
# Upload to Replicate (this creates a temporary URL)
|
40 |
with open(tmp1.name, 'rb') as f:
|
41 |
+
file_url = client.upload(f)
|
42 |
+
image_urls.append(file_url)
|
43 |
+
os.unlink(tmp1.name)
|
|
|
44 |
|
|
|
45 |
if image2:
|
46 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp2:
|
47 |
+
image2.save(tmp2.name, 'PNG')
|
48 |
with open(tmp2.name, 'rb') as f:
|
49 |
+
file_url = client.upload(f)
|
50 |
+
image_urls.append(file_url)
|
51 |
os.unlink(tmp2.name)
|
52 |
|
53 |
+
status_message = "π¨ Processing your images with nano-banana model..."
|
54 |
|
55 |
+
# Prepare input matching the exact format
|
56 |
+
input_data = {
|
57 |
+
"prompt": prompt,
|
58 |
+
"image_input": image_urls # List of URLs
|
59 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# Run the model
|
62 |
+
output = replicate.run(
|
63 |
+
"google/nano-banana",
|
64 |
+
input=input_data
|
65 |
+
)
|
66 |
+
|
67 |
+
# Handle the output based on the reference code
|
68 |
output_url = None
|
69 |
|
70 |
+
# The output object should have a url() method based on your example
|
71 |
+
if hasattr(output, 'url'):
|
72 |
+
output_url = output.url()
|
73 |
elif isinstance(output, str):
|
|
|
74 |
output_url = output
|
75 |
+
elif isinstance(output, list) and len(output) > 0:
|
76 |
+
output_url = output[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
if not output_url:
|
79 |
+
return None, "β Error: No image URL found in response"
|
80 |
|
81 |
+
# Download the generated image
|
82 |
+
# Based on your example, we can also use output.read() if available
|
83 |
+
if hasattr(output, 'read'):
|
84 |
+
img_data = output.read()
|
85 |
+
img = Image.open(BytesIO(img_data))
|
86 |
else:
|
87 |
+
# Fallback to downloading from URL
|
88 |
+
response = requests.get(output_url)
|
89 |
+
if response.status_code == 200:
|
90 |
+
img = Image.open(BytesIO(response.content))
|
91 |
+
else:
|
92 |
+
return None, f"β Error: Failed to download image (Status: {response.status_code})"
|
93 |
+
|
94 |
+
return img, "β
Image generated successfully with nano-banana!"
|
95 |
|
96 |
except Exception as e:
|
97 |
+
# If the model doesn't exist or other errors, provide helpful message
|
98 |
+
error_msg = str(e)
|
99 |
+
if "not found" in error_msg.lower():
|
100 |
+
return None, "β Model 'google/nano-banana' not found. Please check if the model exists on Replicate."
|
101 |
+
elif "authentication" in error_msg.lower():
|
102 |
+
return None, "β Authentication failed. Please check your REPLICATE_API_TOKEN."
|
103 |
+
else:
|
104 |
+
return None, f"β Error: {error_msg}"
|
105 |
|
106 |
# Create Gradio interface with gradient theme
|
107 |
css = """
|
|
|
235 |
```bash
|
236 |
export REPLICATE_API_TOKEN="your_api_token_here"
|
237 |
```
|
238 |
+
Get your token from: https://replicate.com/account/api-tokens
|
239 |
|
240 |
2. **Install Required Packages:**
|
241 |
```bash
|
242 |
pip install gradio replicate pillow requests
|
243 |
```
|
244 |
|
245 |
+
3. **Model Information:**
|
246 |
+
- Using: `google/nano-banana` model
|
247 |
+
- Input: `prompt` (text) and `image_input` (list of image URLs)
|
248 |
+
- Output: Generated image based on the style transfer
|
|
|
249 |
|
250 |
+
4. **Note:**
|
251 |
+
- The model requires actual URLs for images
|
252 |
+
- Images are temporarily uploaded to Replicate's servers
|
253 |
+
- Check if 'google/nano-banana' is available on your Replicate account
|
254 |
|
255 |
### π Security:
|
256 |
- API keys are managed through environment variables
|