Utkarsh64 commited on
Commit
7df5273
·
verified ·
1 Parent(s): e6bc6be

Update backend/app2.py

Browse files
Files changed (1) hide show
  1. backend/app2.py +43 -8
backend/app2.py CHANGED
@@ -1,17 +1,30 @@
1
  import io
 
2
  from pathlib import Path
3
 
4
  import numpy as np
5
  import tensorflow as tf
6
- from flask import Flask, jsonify, request, send_file
7
  from flask_cors import CORS
8
  from PIL import Image, ImageEnhance, ImageFilter
9
 
10
- app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
11
  CORS(app)
12
 
13
  BASE_DIR = Path(__file__).resolve().parent
14
- MODEL_PATH = BASE_DIR.parent / "model.h5"
 
15
  TARGET_SHORT_SIDE = 2048
16
  MAX_LONG_SIDE = 4096
17
  GENERATOR_WORKING_LONG_SIDE = 768
@@ -45,7 +58,7 @@ def _load_gan_generator():
45
  global gan_generator, model_load_error
46
 
47
  if not MODEL_PATH.exists():
48
- model_load_error = f"{MODEL_PATH.name} not found at project root"
49
  return False
50
 
51
  try:
@@ -106,6 +119,7 @@ def improve_clarity(original_image, enhanced_image):
106
  pixels = np.asarray(image).astype("float32")
107
  brightness = float(np.mean(pixels))
108
  night_scene = brightness < 95
 
109
  if brightness < 95:
110
  image = ImageEnhance.Brightness(image).enhance(1.08)
111
  elif brightness < 135:
@@ -124,20 +138,40 @@ def improve_clarity(original_image, enhanced_image):
124
  width, height = image.size
125
  shortest_side = min(width, height)
126
  longest_side = max(width, height)
 
127
  scale = max(1.0, TARGET_SHORT_SIDE / shortest_side)
128
  scale = min(scale, MAX_LONG_SIDE / longest_side)
129
- image = image.resize((round(width * scale), round(height * scale)), Image.Resampling.LANCZOS)
 
 
 
 
130
 
131
  image = ImageEnhance.Contrast(image).enhance(1.08)
132
  image = image.filter(ImageFilter.UnsharpMask(radius=0.8, percent=175, threshold=2))
133
  image = ImageEnhance.Sharpness(image).enhance(1.18)
 
134
  return image
135
 
136
 
 
 
137
  @app.route("/")
138
- def home():
139
- return "GAN enhancement backend is running"
 
 
 
 
 
 
 
 
140
 
 
 
 
 
141
 
142
  @app.route("/enhance", methods=["POST"])
143
  def enhance():
@@ -152,6 +186,7 @@ def enhance():
152
  image = Image.open(file.stream).convert("RGB")
153
 
154
  img = gan_generator.generate(image)
 
155
  buf = io.BytesIO()
156
  img.save(buf, format="PNG")
157
  buf.seek(0)
@@ -164,4 +199,4 @@ def enhance():
164
 
165
 
166
  if __name__ == "__main__":
167
- app.run(debug=True, use_reloader=False)
 
1
  import io
2
+ import os
3
  from pathlib import Path
4
 
5
  import numpy as np
6
  import tensorflow as tf
7
+ from flask import Flask, jsonify, request, send_file, send_from_directory
8
  from flask_cors import CORS
9
  from PIL import Image, ImageEnhance, ImageFilter
10
 
11
+ # Prevent unnecessary GPU init on cloud
12
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
13
+
14
+ # React build folder
15
+ FRONTEND_DIST = Path(__file__).resolve().parent.parent / "frontend" / "dist"
16
+
17
+ app = Flask(
18
+ __name__,
19
+ static_folder=str(FRONTEND_DIST),
20
+ static_url_path=""
21
+ )
22
+
23
  CORS(app)
24
 
25
  BASE_DIR = Path(__file__).resolve().parent
26
+ MODEL_PATH = BASE_DIR / "model.h5"
27
+
28
  TARGET_SHORT_SIDE = 2048
29
  MAX_LONG_SIDE = 4096
30
  GENERATOR_WORKING_LONG_SIDE = 768
 
58
  global gan_generator, model_load_error
59
 
60
  if not MODEL_PATH.exists():
61
+ model_load_error = f"{MODEL_PATH.name} not found in backend folder"
62
  return False
63
 
64
  try:
 
119
  pixels = np.asarray(image).astype("float32")
120
  brightness = float(np.mean(pixels))
121
  night_scene = brightness < 95
122
+
123
  if brightness < 95:
124
  image = ImageEnhance.Brightness(image).enhance(1.08)
125
  elif brightness < 135:
 
138
  width, height = image.size
139
  shortest_side = min(width, height)
140
  longest_side = max(width, height)
141
+
142
  scale = max(1.0, TARGET_SHORT_SIDE / shortest_side)
143
  scale = min(scale, MAX_LONG_SIDE / longest_side)
144
+
145
+ image = image.resize(
146
+ (round(width * scale), round(height * scale)),
147
+ Image.Resampling.LANCZOS
148
+ )
149
 
150
  image = ImageEnhance.Contrast(image).enhance(1.08)
151
  image = image.filter(ImageFilter.UnsharpMask(radius=0.8, percent=175, threshold=2))
152
  image = ImageEnhance.Sharpness(image).enhance(1.18)
153
+
154
  return image
155
 
156
 
157
+ # ---------- FRONTEND ROUTES ----------
158
+
159
  @app.route("/")
160
+ def serve_react():
161
+ return send_from_directory(app.static_folder, "index.html")
162
+
163
+
164
+ @app.route("/<path:path>")
165
+ def serve_static(path):
166
+ requested = FRONTEND_DIST / path
167
+
168
+ if requested.exists() and requested.is_file():
169
+ return send_from_directory(app.static_folder, path)
170
 
171
+ return send_from_directory(app.static_folder, "index.html")
172
+
173
+
174
+ # ---------- BACKEND API ----------
175
 
176
  @app.route("/enhance", methods=["POST"])
177
  def enhance():
 
186
  image = Image.open(file.stream).convert("RGB")
187
 
188
  img = gan_generator.generate(image)
189
+
190
  buf = io.BytesIO()
191
  img.save(buf, format="PNG")
192
  buf.seek(0)
 
199
 
200
 
201
  if __name__ == "__main__":
202
+ app.run(host="0.0.0.0", port=7860, debug=False)