Spaces:
Running
Running
Update backend/main.py
Browse files- backend/main.py +8 -41
backend/main.py
CHANGED
|
@@ -1,52 +1,19 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 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="
|
| 9 |
|
| 10 |
-
# CORS allow karo Android app ke liye
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
-
allow_origins=["*"],
|
| 14 |
allow_credentials=True,
|
| 15 |
allow_methods=["*"],
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 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 |
-
|
| 52 |
-
raise HTTPException(500, detail=f"Error processing image: {str(e)}")
|
|
|
|
| 1 |
+
# backend/main.py (Simple Forwarder)
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
app = FastAPI(title="OldServer Backend")
|
| 7 |
|
|
|
|
| 8 |
app.add_middleware(
|
| 9 |
CORSMiddleware,
|
| 10 |
+
allow_origins=["*"],
|
| 11 |
allow_credentials=True,
|
| 12 |
allow_methods=["*"],
|
| 13 |
allow_headers=["*"],
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Yeh important line hai - sab requests ko app.py ke app mein forward karega
|
| 17 |
+
from app import app as main_app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
app.mount("/", main_app)
|
|
|