openfree commited on
Commit
47283f8
Β·
verified Β·
1 Parent(s): 51881c6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +222 -0
app.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import replicate
3
+ import os
4
+ from PIL import Image
5
+ import requests
6
+ from io import BytesIO
7
+ import time
8
+
9
+ # Set up Replicate API key from environment variable
10
+ os.environ['REPLICATE_API_TOKEN'] = os.getenv('REPLICATE_API_TOKEN')
11
+
12
+ def process_images(prompt, image1, image2=None):
13
+ """
14
+ Process uploaded images with Replicate API
15
+ """
16
+ if not image1:
17
+ return None, "Please upload at least one image"
18
+
19
+ # Check if API token is set
20
+ if not os.getenv('REPLICATE_API_TOKEN'):
21
+ return None, "⚠️ Please set REPLICATE_API_TOKEN environment variable"
22
+
23
+ try:
24
+ # Prepare input images list
25
+ image_inputs = []
26
+
27
+ # Convert PIL images to temporary files or use file paths
28
+ # Note: In production, you'd upload these to a cloud service
29
+ # For this example, we'll assume the images are accessible via URLs
30
+ # You may need to upload them to a service like Cloudinary or S3 first
31
+
32
+ # This is a placeholder - in real implementation, you'd need to:
33
+ # 1. Save images temporarily
34
+ # 2. Upload to a cloud service
35
+ # 3. Get the URLs
36
+
37
+ status_message = "🎨 Processing your images..."
38
+
39
+ # Prepare input for Replicate
40
+ input_data = {
41
+ "prompt": prompt,
42
+ "image_input": image_inputs # This should contain actual URLs
43
+ }
44
+
45
+ # Run the model
46
+ output = replicate.run(
47
+ "google/nano-banana", # Replace with actual model
48
+ input=input_data
49
+ )
50
+
51
+ # Get the output URL
52
+ if hasattr(output, 'url'):
53
+ output_url = output.url()
54
+ else:
55
+ output_url = str(output)
56
+
57
+ # Download and return the generated image
58
+ response = requests.get(output_url)
59
+ img = Image.open(BytesIO(response.content))
60
+
61
+ return img, "βœ… Image generated successfully!"
62
+
63
+ except Exception as e:
64
+ return None, f"❌ Error: {str(e)}"
65
+
66
+ # Create Gradio interface with gradient theme
67
+ css = """
68
+ .gradio-container {
69
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
70
+ font-family: 'Inter', sans-serif;
71
+ }
72
+ .gr-button {
73
+ background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
74
+ border: none;
75
+ color: white;
76
+ font-weight: bold;
77
+ transition: transform 0.2s;
78
+ }
79
+ .gr-button:hover {
80
+ transform: scale(1.05);
81
+ box-shadow: 0 10px 20px rgba(0,0,0,0.2);
82
+ }
83
+ .gr-input {
84
+ border-radius: 10px;
85
+ border: 2px solid rgba(255,255,255,0.3);
86
+ background: rgba(255,255,255,0.9);
87
+ }
88
+ .header-text {
89
+ background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
90
+ -webkit-background-clip: text;
91
+ -webkit-text-fill-color: transparent;
92
+ background-clip: text;
93
+ font-size: 2.5em;
94
+ font-weight: bold;
95
+ text-align: center;
96
+ margin-bottom: 20px;
97
+ }
98
+ .description-text {
99
+ color: white;
100
+ text-align: center;
101
+ font-size: 1.1em;
102
+ margin-bottom: 30px;
103
+ text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
104
+ }
105
+ """
106
+
107
+ # Build the Gradio interface
108
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
109
+ gr.HTML("""
110
+ <div class="header-text">🎨 AI Image Style Transfer Studio</div>
111
+ <div class="description-text">
112
+ Upload 1-2 images and describe how you want them styled.
113
+ The AI will create a beautiful transformation!
114
+ </div>
115
+ """)
116
+
117
+ with gr.Row():
118
+ with gr.Column(scale=1):
119
+ gr.Markdown("### πŸ“€ Input Section")
120
+
121
+ prompt = gr.Textbox(
122
+ label="✏️ Style Prompt",
123
+ placeholder="Describe how you want to style your images...",
124
+ lines=3,
125
+ value="Make the sheets in the style of the logo. Make the scene natural."
126
+ )
127
+
128
+ with gr.Row():
129
+ image1 = gr.Image(
130
+ label="Image 1 (Required)",
131
+ type="pil",
132
+ height=200
133
+ )
134
+ image2 = gr.Image(
135
+ label="Image 2 (Optional)",
136
+ type="pil",
137
+ height=200
138
+ )
139
+
140
+ generate_btn = gr.Button(
141
+ "πŸš€ Generate Styled Image",
142
+ variant="primary",
143
+ size="lg"
144
+ )
145
+
146
+ gr.Markdown("""
147
+ #### πŸ’‘ Tips:
148
+ - Upload high-quality images for best results
149
+ - Be specific in your style description
150
+ - Experiment with different prompts!
151
+ """)
152
+
153
+ with gr.Column(scale=1):
154
+ gr.Markdown("### 🎯 Output Section")
155
+
156
+ output_image = gr.Image(
157
+ label="Generated Image",
158
+ type="pil",
159
+ height=400
160
+ )
161
+
162
+ status = gr.Textbox(
163
+ label="Status",
164
+ interactive=False,
165
+ lines=2
166
+ )
167
+
168
+ # Examples section
169
+ with gr.Row():
170
+ gr.Examples(
171
+ examples=[
172
+ ["Transform into watercolor painting style", None, None],
173
+ ["Make it look like a vintage photograph", None, None],
174
+ ["Apply cyberpunk neon style", None, None],
175
+ ["Convert to minimalist line art", None, None],
176
+ ],
177
+ inputs=[prompt, image1, image2],
178
+ label="Example Prompts"
179
+ )
180
+
181
+ # Event handlers
182
+ generate_btn.click(
183
+ fn=process_images,
184
+ inputs=[prompt, image1, image2],
185
+ outputsocaloutput_image, status],
186
+ api_name="generate"
187
+ )
188
+
189
+ # Additional information
190
+ gr.Markdown("""
191
+ ---
192
+ ### βš™οΈ Setup Instructions:
193
+
194
+ 1. **Set Environment Variable:**
195
+ ```bash
196
+ export REPLICATE_API_TOKEN="your_api_token_here"
197
+ ```
198
+
199
+ 2. **Install Required Packages:**
200
+ ```bash
201
+ pip install gradio replicate pillow requests
202
+ ```
203
+
204
+ 3. **Note:** For production use, you'll need to:
205
+ - Implement proper image upload to cloud storage (S3, Cloudinary, etc.)
206
+ - Replace the model name with the actual Replicate model you want to use
207
+ - Add proper error handling and rate limiting
208
+
209
+ ### πŸ”’ Security:
210
+ - API keys are managed through environment variables
211
+ - Never commit API keys to version control
212
+ - Consider implementing user authentication for production
213
+ """)
214
+
215
+ # Launch the app
216
+ if __name__ == "__main__":
217
+ demo.launch(
218
+ share=True,
219
+ server_name="0.0.0.0",
220
+ server_port=7860,
221
+ show_error=True
222
+ )