Seniordev22 commited on
Commit
087196e
·
verified ·
1 Parent(s): 34bbd81

Update backend/main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +52 -0
backend/main.py CHANGED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import gradio_client as grc
5
+ from PIL import Image
6
+ import io
7
+
8
+ app = FastAPI(title="Face Aging + White Hair & Beard API")
9
+
10
+ # CORS allow karo Android app ke liye
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"], # Production mein specific origin daalna (jaise http://10.0.2.2 etc.)
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Apna Hugging Face Space ka URL yahan daal do
20
+ HF_SPACE_URL = "https://huggingface.co/spaces/Seniordev22/OldServer" # ← apna actual space URL daal do
21
+
22
+ client = grc.Client(HF_SPACE_URL)
23
+
24
+ @app.post("/age-face")
25
+ async def age_face(file: UploadFile = File(...)):
26
+ if not file.content_type.startswith("image/"):
27
+ raise HTTPException(400, detail="Only image files allowed")
28
+
29
+ contents = await file.read()
30
+
31
+ try:
32
+ # Gradio Space ko call kar rahe hain (image input)
33
+ result = client.predict(
34
+ image=grc.handle_file(io.BytesIO(contents)), # ya direct bytes bhi try kar sakte ho
35
+ api_name="/predict" # ← yeh important hai! Apne Space mein "Use via API" check karo exact api_name ke liye
36
+ )
37
+
38
+ # Agar result mein image hai (usually tuple hota hai)
39
+ if isinstance(result, tuple):
40
+ output_image = result[0] # pehla output image assume kar rahe hain
41
+ else:
42
+ output_image = result
43
+
44
+ # Image ko bytes mein convert karke return karo
45
+ img_byte_arr = io.BytesIO()
46
+ output_image.save(img_byte_arr, format='PNG')
47
+ img_byte_arr.seek(0)
48
+
49
+ return StreamingResponse(img_byte_arr, media_type="image/png")
50
+
51
+ except Exception as e:
52
+ raise HTTPException(500, detail=f"Error processing image: {str(e)}")