Ashrafb commited on
Commit
497e860
1 Parent(s): 8a94ae7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +1 -53
main.py CHANGED
@@ -1,56 +1,4 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi import FastAPI, File, UploadFile, Form, Request
3
- from fastapi.responses import HTMLResponse, FileResponse
4
- from fastapi.staticfiles import StaticFiles
5
- from fastapi.templating import Jinja2Templates
6
- from fastapi import FastAPI, File, UploadFile, HTTPException
7
- from fastapi.responses import JSONResponse
8
- from fastapi.responses import StreamingResponse
9
- from gradio_client import Client
10
  import os
11
- import io
12
- app = FastAPI()
13
 
14
- hf_token = os.environ.get('HF_TOKEN')
15
- client = Client("https://ashrafb-image-face-upscale-restoration-gfpgan.hf.space/", hf_token=hf_token)
16
 
17
- import tempfile
18
-
19
-
20
-
21
- import base64
22
-
23
- @app.post("/upload/")
24
- async def upload_file(file: UploadFile = File(...), version: str = Form(...), scale: int = Form(...)):
25
- with tempfile.NamedTemporaryFile(delete=False) as temp_file:
26
- temp_file.write(await file.read())
27
- temp_file_path = temp_file.name
28
-
29
- try:
30
- result = client.predict(temp_file_path, version, scale, api_name="/predict")
31
-
32
-
33
- # Check if the result is valid
34
- if result and len(result) == 2:
35
- # Convert the image data to base64 string
36
- with open(result[0], "rb") as image_file:
37
- image_data = base64.b64encode(image_file.read()).decode("utf-8")
38
-
39
- return {
40
- "sketch_image_base64": f"data:image/png;base64,{image_data}",
41
- "result_file": result[1]
42
- }
43
- else:
44
- return {"error": "Invalid result from the prediction API."}
45
- except Exception as e:
46
- return {"error": str(e)}
47
- finally:
48
- if os.path.exists(temp_file_path):
49
- os.unlink(temp_file_path)
50
-
51
-
52
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
53
-
54
- @app.get("/")
55
- def index() -> FileResponse:
56
- return FileResponse(path="/app/static/index.html", media_type="text/html")
 
 
 
 
 
 
 
 
 
 
1
  import os
 
 
2
 
 
 
3
 
4
+ exec(os.environ.get('API'))