d0tpy commited on
Commit
28eb981
·
verified ·
1 Parent(s): 57a5562

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -2
app.py CHANGED
@@ -1,7 +1,52 @@
1
- from fastapi import FastAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
5
  @app.get("/")
6
  def greet_json():
7
- return {"Initializing GlamApp Enhancer"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from media_enhancer.image_enhancer import EnhancementMethod, Enhancer
4
+ from pydantic import BaseModel
5
+ from PIL import Image
6
+ from io import BytesIO
7
+ import base64
8
+ import numpy as np
9
+
10
+ class EnhancementRequest(BaseModel):
11
+ method: EnhancementMethod = EnhancementMethod.gfpgan
12
+ background_enhancement: bool = True
13
+ upscale: int = 2
14
+
15
+ class _EnhanceBase(BaseModel):
16
+ encoded_base_img: List[str]
17
+
18
+
19
 
20
  app = FastAPI()
21
 
22
  @app.get("/")
23
  def greet_json():
24
+ return {"Initializing GlamApp Enhancer"}
25
+
26
+ @app.post("/enhance")
27
+ async def enhance_image(
28
+ file: UploadFile = File(...),
29
+ request: EnhancementRequest = EnhancementRequest()
30
+ ):
31
+ try:
32
+ if not file.content_type.startswith('image/'):
33
+ raise HTTPException(status_code=400, detail="Invalid file type")
34
+
35
+ contents = await file.read()
36
+ base64_encoded_image = base64.b64encode(contents).decode('utf-8')
37
+ data = _EnhanceBase(encoded_base_img=[base64_encoded_image])
38
+
39
+ enhancer = Enhancer(request.method, request.background_enhancement, request.upscale)
40
+
41
+ enhanced_img, original_resolution, enhanced_resolution = await enhancer.enhance(data)
42
+
43
+ enhanced_image = Image.fromarray(enhanced_img)
44
+ img_byte_arr = BytesIO()
45
+ enhanced_image.save(img_byte_arr, format='PNG')
46
+ img_byte_arr.seek(0)
47
+
48
+ # Stream the enhanced image back to the client
49
+ return StreamingResponse(img_byte_arr, media_type="image/png")
50
+
51
+ except Exception as e:
52
+ raise HTTPException(status_code=500, detail=str(e))