🐙 Qwen3.7 Max Compare with others
This is writing about Qwen3.7 Max review and fix a random python code from github.
input prompt: Review and help me optimize this python file.
💻 Original
Nhấn vào đây để xem code
```python from subprocess import Popen, PIPE import gradio as gr import numpy as np import cv2 import os from pathlib import Pathdef inference_gfpgan(img_path, resize): return run_task("_gfpgan", img_path, resize=resize)
def inference_gpen(img_path, task): return run_task("_gpen", img_path, task=task)
def run_task(model, img_path, task="restore", resize="on"): # do the inference in a seperate process to avoid high gpu idle power consumption of pytorch p = Popen(["python", "app/%s.py" % model, img_path, task, resize], stdout=PIPE, stderr=PIPE) # info img_name = os.path.basename(img_path) basename, ext = os.path.splitext(img_name) model = model.strip("") print(f"Started {model.upper()} {task} {img_name} ...") # wait for it to finish stdout, stderr = p.communicate() print("stdout:\n", stdout) print("stderr:\n", stderr) # retrieved the restored image extension = ext[1:] restored_img_path = os.path.join( "outputs", f"{basename}{model}_{task}.{extension}" ) if os.path.isfile(restored_img_path): restored_img = cv2.imread(restored_img_path) restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB) print(f"Finished {model.upper()} {task} {restored_img_path}") return restored_img else: gr.Error("Result not found!") return None
def get_processed_files(n=4): """ Return last n files """ paths = sorted(Path("outputs").iterdir(), key=os.path.getmtime) files = [] for i in range(min(n,len(paths))): files.append(str(paths[-(i + 1)])) return files
with gr.Blocks(title="GFPGAN") as GFPGAN_app: gr.Markdown( """ # Generative Facial Prior GAN """ ) with gr.Row(): input_img = gr.Image(type="filepath", label="Input") output_img = gr.Image(type="numpy", label="Output") with gr.Row(): resize_radio = gr.Radio( choices=["on", "off"], label="Resize", value="on" ) submit_btn = gr.Button("Submit", variant="primary") get_btn = gr.Button("Get")
prev_files = gr.Gallery(
label="Downloadables",
show_label=False,
elem_id="gallery",
columns=[2],
rows=[1],
object_fit="contain",
height="auto",
allow_preview=False,
)
submit_btn.click(inference_gfpgan, inputs=[input_img,resize_radio], outputs=[output_img])
get_btn.click(get_processed_files, outputs=[prev_files])
with gr.Blocks(title="GPEN") as GPEN_app: gr.Markdown( """ # GAN Prior Embedded Network """ ) with gr.Row(): input_img = gr.Image(type="filepath", label="Input") output_img = gr.Image(type="numpy", label="Output") with gr.Row(): task_radio = gr.Radio( choices=["colorize", "restore"], label="Task", value="colorize" ) submit_btn = gr.Button("Submit", variant="primary")
submit_btn.click(
inference_gpen, inputs=[input_img, task_radio], outputs=[output_img]
)
get_btn.click(get_processed_files, outputs=[prev_files])
app = gr.TabbedInterface([GFPGAN_app, GPEN_app], ["GFPGAN", "GPEN"])
app.queue() app.launch(server_name="0.0.0.0")
### **🤖 Gemini**
```python
from subprocess import Popen, PIPE
import gradio as gr
import cv2
import os
from pathlib import Path
OUTPUT_DIR = Path("outputs")
OUTPUT_DIR.mkdir(exist_ok=True)
def run_task(model: str, img_path: str, task: str = "restore", resize: str = "on"):
img_path_obj = Path(img_path)
basename = img_path_obj.stem
ext = img_path_obj.suffix
model_clean = model.strip("_")
print(f"Started {model_clean.upper()} | Task: {task} | Image: {img_path_obj.name} ...")
p = Popen(
["python", f"app/{model}.py", str(img_path_obj), task, resize],
stdout=PIPE,
stderr=PIPE
)
stdout, stderr = p.communicate()
print("--- STDOUT ---")
print(stdout.decode("utf-8", errors="ignore"))
print("--- STDERR ---")
print(stderr.decode("utf-8", errors="ignore"))
extension = ext.lstrip(".")
restored_img_path = OUTPUT_DIR / f"{basename}_{model_clean}_{task}.{extension}"
if restored_img_path.is_file():
restored_img = cv2.imread(str(restored_img_path))
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
print(f"Finished {model_clean.upper()} -> {restored_img_path}")
return restored_img
else:
raise gr.Error(f"Result not found at {restored_img_path}!")
def inference_gfpgan(img_path, resize):
return run_task("_gfpgan", img_path, task="restore", resize=resize)
def inference_gpen(img_path, task):
return run_task("_gpen", img_path, task=task, resize="on")
def get_processed_files(n=4):
if not OUTPUT_DIR.exists():
return []
paths = sorted([p for p in OUTPUT_DIR.iterdir() if p.is_file()], key=os.path.getmtime)
latest_files = [str(p) for p in paths[-n:]][::-1]
return latest_files
with gr.Blocks(title="GFPGAN") as GFPGAN_app:
gr.Markdown("# Generative Facial Prior GAN")
with gr.Row():
input_img = gr.Image(type="filepath", label="Input")
output_img = gr.Image(type="numpy", label="Output")
with gr.Row():
resize_radio = gr.Radio(choices=["on", "off"], label="Resize", value="on")
submit_btn = gr.Button("Submit", variant="primary")
get_btn = gr.Button("Get Recent Files")
prev_files = gr.Gallery(
label="Downloadables",
show_label=True,
columns=4,
rows=1,
object_fit="contain",
height="auto",
allow_preview=True,
)
submit_btn.click(inference_gfpgan, inputs=[input_img, resize_radio], outputs=[output_img])
get_btn.click(get_processed_files, outputs=[prev_files])
with gr.Blocks(title="GPEN") as GPEN_app:
gr.Markdown("# GAN Prior Embedded Network")
with gr.Row():
input_img = gr.Image(type="filepath", label="Input")
output_img = gr.Image(type="numpy", label="Output")
with gr.Row():
task_radio = gr.Radio(choices=["colorize", "restore"], label="Task", value="restore")
submit_btn = gr.Button("Submit", variant="primary")
get_btn_gpen = gr.Button("Get Recent Files") # Tạo nút riêng biệt cho tab này
prev_files_gpen = gr.Gallery(
label="Downloadables",
show_label=True,
columns=4,
rows=1,
object_fit="contain",
height="auto",
allow_preview=True,
)
submit_btn.click(inference_gpen, inputs=[input_img, task_radio], outputs=[output_img])
get_btn_gpen.click(get_processed_files, outputs=[prev_files_gpen])
app = gr.TabbedInterface([GFPGAN_app, GPEN_app], ["GFPGAN", "GPEN"])
if __name__ == "__main__":
app.queue()
app.launch(server_name="0.0.0.0")
🤖 Qwen3.7
from subprocess import Popen, PIPE
import gradio as gr
import cv2
import os
from pathlib import Path
OUTPUT_DIR = Path("outputs")
OUTPUT_DIR.mkdir(exist_ok=True)
def run_task(model: str, img_path: str, task: str = "restore", resize: str = "on"):
img_path_obj = Path(img_path)
basename = img_path_obj.stem
ext = img_path_obj.suffix
model_clean = model.strip("_ ")
print(f"Started {model_clean.upper()} | Task: {task} | Image: {img_path_obj.name}...")
p = Popen(
["python", f"app/{model}.py", str(img_path_obj), task, resize],
stdout=PIPE,
stderr=PIPE
)
stdout, stderr = p.communicate()
print("--- STDOUT ---")
print(stdout.decode("utf-8", errors="ignore"))
print("--- STDERR ---")
print(stderr.decode("utf-8", errors="ignore"))
extension = ext.lstrip(".")
restored_img_path = OUTPUT_DIR / f"{basename}{model}_{task}.{extension}"
if restored_img_path.is_file():
restored_img = cv2.imread(str(restored_img_path))
if restored_img is None:
raise gr.Error(f"Không thể đọc ảnh: {restored_img_path}")
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
print(f"Finished {model_clean.upper()} -> {restored_img_path}")
return restored_img
else:
raise gr.Error(f"Không tìm thấy kết quả tại {restored_img_path}!")
def inference_gfpgan(img_path, resize):
return run_task("_gfpgan", img_path, task="restore", resize=resize)
def inference_gpen(img_path, task):
return run_task("_gpen", img_path, task=task, resize="on")
def get_processed_files(n=4):
if not OUTPUT_DIR.exists():
return []
paths = sorted([p for p in OUTPUT_DIR.iterdir() if p.is_file()], key=os.path.getmtime)
latest_files = [str(p) for p in paths[-n:]][::-1]
return latest_files
with gr.Blocks(title="GFPGAN") as GFPGAN_app:
gr.Markdown("# Generative Facial Prior GAN")
with gr.Row():
input_img = gr.Image(type="filepath", label="Input")
output_img = gr.Image(type="numpy", label="Output")
with gr.Row():
resize_radio = gr.Radio(choices=["on", "off"], label="Resize", value="on")
submit_btn = gr.Button("Submit", variant="primary")
get_btn = gr.Button("Get Recent Files")
prev_files = gr.Gallery(label="Downloadables", show_label=True, columns=4, rows=1)
submit_btn.click(inference_gfpgan, inputs=[input_img, resize_radio], outputs=[output_img])
get_btn.click(get_processed_files, outputs=[prev_files])
with gr.Blocks(title="GPEN") as GPEN_app:
gr.Markdown("# GAN Prior Embedded Network")
with gr.Row():
input_img = gr.Image(type="filepath", label="Input")
output_img = gr.Image(type="numpy", label="Output")
with gr.Row():
task_radio = gr.Radio(choices=["colorize", "restore"], label="Task", value="restore")
submit_btn = gr.Button("Submit", variant="primary")
get_btn_gpen = gr.Button("Get Recent Files")
prev_files_gpen = gr.Gallery(label="Downloadables", show_label=True, columns=4, rows=1)
submit_btn.click(inference_gpen, inputs=[input_img, task_radio], outputs=[output_img])
get_btn_gpen.click(get_processed_files, outputs=[prev_files_gpen])
app = gr.TabbedInterface([GFPGAN_app, GPEN_app], ["GFPGAN", "GPEN"])
if __name__ == "__main__":
app.queue()
app.launch(server_name="0.0.0.0")
