Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 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)
|