Update main.py
Browse files
main.py
CHANGED
@@ -1,52 +1,49 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile,
|
2 |
from fastapi.responses import JSONResponse
|
3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from gradio_client import Client, handle_file
|
5 |
import os
|
6 |
-
import tempfile
|
7 |
import base64
|
8 |
|
|
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
#
|
12 |
-
app.add_middleware(
|
13 |
-
CORSMiddleware,
|
14 |
-
allow_origins=["*"], # Adjust as needed
|
15 |
-
allow_credentials=True,
|
16 |
-
allow_methods=["*"],
|
17 |
-
allow_headers=["*"],
|
18 |
-
)
|
19 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
20 |
|
21 |
# Initialize the Gradio client with the token
|
22 |
-
client = Client("Makhinur/
|
23 |
|
24 |
@app.post("/upload/")
|
25 |
-
async def
|
26 |
-
# Save the uploaded file temporarily
|
27 |
-
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
28 |
-
temp_file.write(await file.read())
|
29 |
-
temp_file_path = temp_file.name
|
30 |
-
|
31 |
try:
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
"result_file": result[1]
|
44 |
-
})
|
45 |
-
else:
|
46 |
-
return JSONResponse({"error": "Invalid result from the prediction API."}, status_code=500)
|
47 |
-
except Exception as e:
|
48 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
49 |
-
finally:
|
50 |
# Clean up the temporary file
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
from fastapi.responses import JSONResponse
|
|
|
3 |
from gradio_client import Client, handle_file
|
4 |
import os
|
|
|
5 |
import base64
|
6 |
|
7 |
+
# Initialize FastAPI
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# Load environment variables if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
|
13 |
# Initialize the Gradio client with the token
|
14 |
+
client = Client("Makhinur/Its", hf_token=HF_TOKEN)
|
15 |
|
16 |
@app.post("/upload/")
|
17 |
+
async def upload_image(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
+
# Save the uploaded file to a temporary location
|
20 |
+
file_location = f"temp_{file.filename}"
|
21 |
+
with open(file_location, "wb") as f:
|
22 |
+
f.write(await file.read())
|
23 |
+
|
24 |
+
# Use the Gradio client to send the image to the Gradio app
|
25 |
+
result = client.predict(
|
26 |
+
img=handle_file(file_location),
|
27 |
+
api_name="/predict"
|
28 |
+
)
|
29 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# Clean up the temporary file
|
31 |
+
os.remove(file_location)
|
32 |
+
|
33 |
+
# Read the sketch image from the result
|
34 |
+
sketch_image_path = result[0] # Path to the sketch image
|
35 |
+
|
36 |
+
# Convert the sketch image to base64
|
37 |
+
with open(sketch_image_path, "rb") as img_file:
|
38 |
+
sketch_image_base64 = base64.b64encode(img_file.read()).decode('utf-8')
|
39 |
+
|
40 |
+
# Prepare the response
|
41 |
+
response = {
|
42 |
+
"sketch_image_base64": f"data:image/jpeg;base64,{sketch_image_base64}"
|
43 |
+
}
|
44 |
+
|
45 |
+
return JSONResponse(content=response)
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
raise HTTPException(status_code=500, detail=str(e))
|
49 |
+
|