Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -138,63 +138,63 @@ 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 |
-
# 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 |
-
|
| 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 |
|
|
|
|
| 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 |
|