Ashrafb commited on
Commit
2371a34
·
verified ·
1 Parent(s): e120bd1

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -5
main.py CHANGED
@@ -14,14 +14,19 @@ app = FastAPI()
14
  hf_token = os.environ.get('HF_TOKEN')
15
  client = Client("https://ashrafb-image-to-sketch.hf.space/", hf_token=hf_token)
16
 
17
- import base64
18
 
19
  @app.post("/upload/")
20
  async def upload_file(file: UploadFile = File(...)):
21
- with file.file as f:
22
- contents = f.read()
23
- result = client.predict(contents, api_name="/predict")
24
- return {"sketch_image": result[0]}
 
 
 
 
 
25
 
26
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
27
 
 
14
  hf_token = os.environ.get('HF_TOKEN')
15
  client = Client("https://ashrafb-image-to-sketch.hf.space/", hf_token=hf_token)
16
 
17
+ import tempfile
18
 
19
  @app.post("/upload/")
20
  async def upload_file(file: UploadFile = File(...)):
21
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
22
+ temp_file.write(await file.read())
23
+ temp_file_path = temp_file.name
24
+
25
+ try:
26
+ result = client.predict(temp_file_path, api_name="/predict")
27
+ return {"sketch_image": result[0]}
28
+ finally:
29
+ os.unlink(temp_file_path)
30
 
31
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
32