tejani commited on
Commit
6be194e
·
verified ·
1 Parent(s): 90e74e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -16
app.py CHANGED
@@ -1,22 +1,55 @@
 
 
1
  import requests
 
2
 
3
- url = "https://changeclothesai.online/api/try-on/edge"
4
 
5
- cookies = {}
 
 
 
 
 
 
 
6
 
7
- headers = {
8
- "accept": "*/*",
9
- "f": "sdfdsfsKaVgUoxa5j1jzcFtziPx",
10
- }
 
 
11
 
12
- data = {
13
- "humanImg": "https://jallenjia-change-clothes-ai.hf.space/file=/tmp/gradio/f71ebb0af5153ab8d2a76722918738f803045719/model_3.png",
14
- "garment": "https://jallenjia-change-clothes-ai.hf.space/file=/tmp/gradio/01b0bf774b8b20130cdb650fda3f2232b571a90f/Girl%20outfits%203.jpg",
15
- "garmentDesc": "",
16
- "category": "upper_body"
17
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- response = requests.post(url, headers=headers, cookies=cookies, data=data)
20
-
21
- print(response.status_code)
22
- print(response.text)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  import requests
4
+ from fastapi.middleware.cors import CORSMiddleware
5
 
6
+ app = FastAPI()
7
 
8
+ # Add CORS middleware to allow requests from Hugging Face Spaces frontend
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"], # Adjust for production to specific origins
12
+ allow_credentials=True,
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
16
 
17
+ # Define the request model
18
+ class TryOnRequest(BaseModel):
19
+ humanImg: str
20
+ garment: str
21
+ garmentDesc: str
22
+ category: str
23
 
24
+ # Endpoint to proxy the request to the original API
25
+ @app.post("/try-on")
26
+ async def try_on(request: TryOnRequest):
27
+ url = "https://changeclothesai.online/api/try-on/edge"
28
+
29
+ headers = {
30
+ "accept": "*/*",
31
+ "f": "sdfdsfsKaVgUoxa5j1jzcFtziPx",
32
+ }
33
+
34
+ data = {
35
+ "humanImg": request.humanImg,
36
+ "garment": request.garment,
37
+ "garmentDesc": request.garmentDesc,
38
+ "category": request.category
39
+ }
40
+
41
+ try:
42
+ response = requests.post(url, headers=headers, cookies={}, data=data)
43
+ response.raise_for_status() # Raise exception for bad status codes
44
+
45
+ return {
46
+ "status_code": response.status_code,
47
+ "response": response.json() if response.headers.get('content-type') == 'application/json' else response.text
48
+ }
49
+ except requests.exceptions.RequestException as e:
50
+ raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")
51
 
52
+ # Health check endpoint for Hugging Face Spaces
53
+ @app.get("/")
54
+ async def root():
55
+ return {"message": "FastAPI proxy for try-on API is running"}