darkvibe314 commited on
Commit
a7543e2
·
verified ·
1 Parent(s): 2d1c2ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -15,21 +15,26 @@ model_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/Rea
15
  model_path = 'RealESRGAN_x4plus.pth'
16
 
17
  if not os.path.exists(model_path):
18
- print("Downloading AI model...")
19
  response = requests.get(model_url)
20
  with open(model_path, 'wb') as f:
21
  f.write(response.content)
22
 
23
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
24
- upsampler = RealESRGANer(
25
- scale=4,
26
- model_path=model_path,
27
- model=model,
28
- tile=400, # Prevents "Out of Memory" errors on free CPUs
29
- tile_pad=10,
30
- pre_pad=0,
31
- half=False
32
- )
 
 
 
 
 
33
 
34
  @app.get("/")
35
  def home():
@@ -38,22 +43,19 @@ def home():
38
  @app.post("/upscale")
39
  async def upscale(file: UploadFile = File(...)):
40
  try:
41
- # Read image
42
  data = await file.read()
43
  nparr = np.frombuffer(data, np.uint8)
44
  img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
45
 
46
- # Actual AI Processing
47
- # outscale=4 makes it 4x bigger and sharper
48
  output, _ = upsampler.enhance(img, outscale=4)
49
 
50
- # Convert back to JPG
51
- _, encoded_img = cv2.imencode('.jpg', output, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
52
  return Response(content=encoded_img.tobytes(), media_type="image/jpeg")
53
 
54
  except Exception as e:
55
- print(f"Error: {str(e)}")
56
- return {"error": "AI Engine busy or image too large"}
57
 
58
  if __name__ == "__main__":
59
  import uvicorn
 
15
  model_path = 'RealESRGAN_x4plus.pth'
16
 
17
  if not os.path.exists(model_path):
18
+ print("Downloading AI model... please wait.")
19
  response = requests.get(model_url)
20
  with open(model_path, 'wb') as f:
21
  f.write(response.content)
22
 
23
+ # Initialize the AI Engine
24
+ try:
25
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
26
+ upsampler = RealESRGANer(
27
+ scale=4,
28
+ model_path=model_path,
29
+ model=model,
30
+ tile=400,
31
+ tile_pad=10,
32
+ pre_pad=0,
33
+ half=False
34
+ )
35
+ print("✅ AI Engine Loaded Successfully")
36
+ except Exception as e:
37
+ print(f"❌ Initialization Error: {e}")
38
 
39
  @app.get("/")
40
  def home():
 
43
  @app.post("/upscale")
44
  async def upscale(file: UploadFile = File(...)):
45
  try:
 
46
  data = await file.read()
47
  nparr = np.frombuffer(data, np.uint8)
48
  img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
49
 
50
+ # Process image (4x Upscale)
 
51
  output, _ = upsampler.enhance(img, outscale=4)
52
 
53
+ # Convert to high-quality JPG
54
+ _, encoded_img = cv2.imencode('.jpg', output, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
55
  return Response(content=encoded_img.tobytes(), media_type="image/jpeg")
56
 
57
  except Exception as e:
58
+ return {"error": str(e)}
 
59
 
60
  if __name__ == "__main__":
61
  import uvicorn