sahadev10 commited on
Commit
f585f99
Β·
verified Β·
1 Parent(s): 9e87a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -18
app.py CHANGED
@@ -42,29 +42,29 @@
42
  # return image_pathsgg
43
 
44
  # === Send Image to Backend ===
45
- def send_to_backend(img_path, user_id):
46
- if not user_id:
47
- return "❌ user_id not found in URL."
48
 
49
- if not img_path or not os.path.exists(img_path):
50
- return "⚠️ No image selected or image not found."
51
 
52
- try:
53
- with open(img_path, 'rb') as f:
54
- files = {'file': ('generated_image.png', f, 'image/png')}
55
 
56
- # Your backend endpoint here
57
- url = f"https://9429-103-40-74-83.ngrok-free.app/images/upload/{user_id}"
58
- response = requests.post(url, files=files)
59
 
60
- if response.status_code == 201:
61
- return "βœ… Image uploaded and saved to database!"
62
- else:
63
- console.log({response.text})
64
- return f"❌ Upload failed: {response.status_code} - {response.text}"
65
 
66
- except Exception as e:
67
- return f"⚠️ Error: {str(e)}"
68
 
69
  # # === Gradio Interface ===
70
  # with gr.Blocks() as demo:
@@ -138,6 +138,66 @@ with open("blazer_model.pkl", "rb") as f:
138
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").eval().cpu()
139
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  # Generate images
142
  def generate_images(G, num_images=10): # Reduce for CPU performance
143
  z = torch.randn(num_images, G.z_dim)
 
42
  # return image_pathsgg
43
 
44
  # === Send Image to Backend ===
45
+ # def send_to_backend(img_path, user_id):
46
+ # if not user_id:
47
+ # return "❌ user_id not found in URL."
48
 
49
+ # if not img_path or not os.path.exists(img_path):
50
+ # return "⚠️ No image selected or image not found."
51
 
52
+ # try:
53
+ # with open(img_path, 'rb') as f:
54
+ # files = {'file': ('generated_image.png', f, 'image/png')}
55
 
56
+ # # Your backend endpoint here
57
+ # url = f"https://e335-103-40-74-83.ngrok-free.app/images/upload/{user_id}"
58
+ # response = requests.post(url, files=files)
59
 
60
+ # if response.status_code == 201:
61
+ # return "βœ… Image uploaded and saved to database!"
62
+ # else:
63
+ # console.log({response.text})
64
+ # return f"❌ Upload failed: {response.status_code} - {response.text}"
65
 
66
+ # except Exception as e:
67
+ # return f"⚠️ Error: {str(e)}"
68
 
69
  # # === Gradio Interface ===
70
  # with gr.Blocks() as demo:
 
138
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").eval().cpu()
139
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
140
 
141
+ # def send_to_backend(img_path, user_id):
142
+ # if not user_id:
143
+ # return "❌ user_id not found in URL."
144
+
145
+ # if not img_path or not os.path.exists(img_path):
146
+ # return "⚠️ No image selected or image not found."
147
+
148
+ # try:
149
+ # with open(img_path, 'rb') as f:
150
+ # files = {'file': ('generated_image.png', f, 'image/png')}
151
+
152
+ # # Your backend endpoint here
153
+ # url = f"https://e335-103-40-74-83.ngrok-free.app/images/upload/{user_id}"
154
+ # response = requests.post(url, files=files)
155
+
156
+ # if response.status_code == 201:
157
+ # return "βœ… Image uploaded and saved to database!"
158
+ # else:
159
+ # console.log({response.text})
160
+ # return f"❌ Upload failed: {response.status_code} - {response.text}"
161
+
162
+ # except Exception as e:
163
+ # return f"⚠️ Error: {str(e)}"
164
+
165
+
166
+ import os
167
+ import requests # Make sure you import this!
168
+
169
+ def send_to_backend(img_path, user_id):
170
+ print(f"πŸ’‘ [DEBUG] Sending image to backend | img_path={img_path}, user_id={user_id}")
171
+
172
+ if not user_id:
173
+ print("❌ [DEBUG] Missing user_id in URL.")
174
+ return "❌ user_id not found in URL."
175
+
176
+ if not img_path or not os.path.exists(img_path):
177
+ print("⚠️ [DEBUG] Image path invalid or does not exist.")
178
+ return "⚠️ No image selected or image not found."
179
+
180
+ try:
181
+ with open(img_path, 'rb') as f:
182
+ files = {'file': ('generated_image.png', f, 'image/png')}
183
+ url = f"https://e335-103-40-74-83.ngrok-free.app/images/upload/{user_id}"
184
+ print(f"πŸ” [DEBUG] Sending POST to {url}")
185
+ response = requests.post(url, files=files)
186
+
187
+ print(f"πŸ“© [DEBUG] Response: {response.status_code} - {response.text}")
188
+ if response.status_code == 201 or response.status_code == 200:
189
+ return "βœ… Image uploaded and saved to database!"
190
+ else:
191
+ return f"❌ Upload failed: {response.status_code} - {response.text}"
192
+
193
+ except Exception as e:
194
+ print(f"⚠️ [ERROR] Exception during upload: {str(e)}")
195
+ return f"⚠️ Error: {str(e)}"
196
+
197
+
198
+
199
+
200
+
201
  # Generate images
202
  def generate_images(G, num_images=10): # Reduce for CPU performance
203
  z = torch.randn(num_images, G.z_dim)