Update app.py
Browse files
app.py
CHANGED
@@ -28,12 +28,17 @@ app.add_middleware(
|
|
28 |
# Mount static directory for serving files
|
29 |
STATIC_DIR = Path("/home/user/app/static")
|
30 |
STATIC_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
31 |
try:
|
32 |
os.chmod(STATIC_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)
|
33 |
except Exception as e:
|
34 |
logger.error(f"Failed to set permissions for {STATIC_DIR}: {str(e)}")
|
35 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
36 |
|
|
|
|
|
|
|
|
|
37 |
# Define the request model
|
38 |
class TryOnRequest(BaseModel):
|
39 |
garmentDesc: str
|
@@ -74,8 +79,20 @@ async def save_file_and_get_url(file: UploadFile) -> str:
|
|
74 |
# Generate public URL
|
75 |
space_id = "tejani-tryapi"
|
76 |
public_url = f"https://{space_id}.hf.space/static/{unique_filename}"
|
|
|
|
|
77 |
logger.info(f"Generated public URL: {public_url}")
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
return public_url
|
80 |
except Exception as e:
|
81 |
logger.error(f"Error in save_file_and_get_url: {str(e)}")
|
@@ -113,7 +130,6 @@ async def try_on(
|
|
113 |
|
114 |
# Forward request to the original API
|
115 |
response = requests.post(url, headers=headers, cookies={}, data=data)
|
116 |
-
logger.info(f"API response status: {response.status_code}, content: {response.text}")
|
117 |
response.raise_for_status()
|
118 |
|
119 |
return {
|
@@ -205,15 +221,14 @@ async def test_upload_form():
|
|
205 |
<style>
|
206 |
body { font-family: Arial, sans-serif; margin: 20px; }
|
207 |
.response { margin-top: 20px; padding: 10px; border: 1px solid #ccc; }
|
208 |
-
pre { background: #f4f4f4; padding: 10px; max-height: 300px; overflow: auto; }
|
209 |
</style>
|
210 |
</head>
|
211 |
<body>
|
212 |
<h1>Test File Upload</h1>
|
213 |
<form action="/try-on" method="post" enctype="multipart/form-data" onsubmit="showResponse(event)">
|
214 |
-
<label for="human_img">Human Image (JPG/PNG
|
215 |
<input type="file" id="human_img" name="human_img" accept=".jpg,.jpeg,.png" required><br><br>
|
216 |
-
<label for="garment">Garment Image (JPG/PNG
|
217 |
<input type="file" id="garment" name="garment" accept=".jpg,.jpeg,.png" required><br><br>
|
218 |
<label for="garment_desc">Garment Description:</label><br>
|
219 |
<input type="text" id="garment_desc" name="garment_desc" value=""><br><br>
|
|
|
28 |
# Mount static directory for serving files
|
29 |
STATIC_DIR = Path("/home/user/app/static")
|
30 |
STATIC_DIR.mkdir(parents=True, exist_ok=True)
|
31 |
+
# Set directory permissions to ensure accessibility
|
32 |
try:
|
33 |
os.chmod(STATIC_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)
|
34 |
except Exception as e:
|
35 |
logger.error(f"Failed to set permissions for {STATIC_DIR}: {str(e)}")
|
36 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
37 |
|
38 |
+
# Alternative directory (uncomment to use /tmp/gradio)
|
39 |
+
# STATIC_DIR = Path("/tmp/gradio")
|
40 |
+
# STATIC_DIR.mkdir(parents=True, exist_ok=True)
|
41 |
+
|
42 |
# Define the request model
|
43 |
class TryOnRequest(BaseModel):
|
44 |
garmentDesc: str
|
|
|
79 |
# Generate public URL
|
80 |
space_id = "tejani-tryapi"
|
81 |
public_url = f"https://{space_id}.hf.space/static/{unique_filename}"
|
82 |
+
# Alternative URL for /tmp/gradio (uncomment if using /tmp/gradio)
|
83 |
+
# public_url = f"https://{space_id}.hf.space/file={str(file_path)}"
|
84 |
logger.info(f"Generated public URL: {public_url}")
|
85 |
|
86 |
+
# Test URL accessibility
|
87 |
+
try:
|
88 |
+
response = requests.head(public_url, timeout=20)
|
89 |
+
if response.status_code != 200:
|
90 |
+
logger.warning(f"Public URL {public_url} returned status {response.status_code}")
|
91 |
+
else:
|
92 |
+
logger.info(f"Public URL {public_url} is accessible")
|
93 |
+
except requests.exceptions.RequestException as e:
|
94 |
+
logger.error(f"Failed to access public URL {public_url}: {str(e)}")
|
95 |
+
|
96 |
return public_url
|
97 |
except Exception as e:
|
98 |
logger.error(f"Error in save_file_and_get_url: {str(e)}")
|
|
|
130 |
|
131 |
# Forward request to the original API
|
132 |
response = requests.post(url, headers=headers, cookies={}, data=data)
|
|
|
133 |
response.raise_for_status()
|
134 |
|
135 |
return {
|
|
|
221 |
<style>
|
222 |
body { font-family: Arial, sans-serif; margin: 20px; }
|
223 |
.response { margin-top: 20px; padding: 10px; border: 1px solid #ccc; }
|
|
|
224 |
</style>
|
225 |
</head>
|
226 |
<body>
|
227 |
<h1>Test File Upload</h1>
|
228 |
<form action="/try-on" method="post" enctype="multipart/form-data" onsubmit="showResponse(event)">
|
229 |
+
<label for="human_img">Human Image (JPG/PNG):</label><br>
|
230 |
<input type="file" id="human_img" name="human_img" accept=".jpg,.jpeg,.png" required><br><br>
|
231 |
+
<label for="garment">Garment Image (JPG/PNG):</label><br>
|
232 |
<input type="file" id="garment" name="garment" accept=".jpg,.jpeg,.png" required><br><br>
|
233 |
<label for="garment_desc">Garment Description:</label><br>
|
234 |
<input type="text" id="garment_desc" name="garment_desc" value=""><br><br>
|