File size: 6,033 Bytes
68a191c fff18c2 68a191c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import gradio as gr
import replicate
from PIL import Image
import tempfile
import uuid
import requests
from io import BytesIO
import os
# ํ๊ฒฝ ๋ณ์์์ API ํ ํฐ, ๋น๋ฐ๋ฒํธ, ๋ชจ๋ธ ์ด๋ฆ๋ค ๊ฐ์ ธ์ค๊ธฐ
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
PASSWORD = os.getenv("PASSWORD")
UPSCALER_MODEL = os.getenv("UPSCALER_MODEL")
TEXT_REMOVAL_MODEL = os.getenv("TEXT_REMOVAL_MODEL")
def upscale_image(image_path, output_format):
"""Clarity Upscaler๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ง (๋ชจ๋ ๋งค๊ฐ๋ณ์ ๊ณ ์ )"""
try:
client = replicate.Client(api_token=REPLICATE_API_TOKEN)
with open(image_path, "rb") as file:
input_data = {
"image": file,
"scale_factor": 2, # ๊ณ ์ : 2๋ฐฐ ํ๋
"resemblance": 0.8, # ๊ณ ์ : ๋์ ์๋ณธ ์ ์ฌ๋
"creativity": 0.2, # ๊ณ ์ : ๋ฎ์ ์ฐฝ์์ฑ
"output_format": output_format.lower(),
"prompt": "high quality, detailed, sharp",
"negative_prompt": "(worst quality, low quality, normal quality:2)"
}
output = client.run(
UPSCALER_MODEL,
input=input_data
)
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ค์ด๋ก๋
if output and isinstance(output, list) and len(output) > 0:
response = requests.get(output[0])
if response.status_code == 200:
result_image = Image.open(BytesIO(response.content))
# ์์ ํ์ผ๋ก ์ ์ฅ
ext = output_format.lower()
upscaled_filename = f"upscaled_temp_{uuid.uuid4()}.{ext}"
if ext == 'jpg' and result_image.mode == 'RGBA':
result_image = result_image.convert('RGB')
result_image.save(upscaled_filename, format='JPEG' if ext=='jpg' else 'PNG', quality=95 if ext=='jpg' else None)
return upscaled_filename
else:
return None
else:
return None
except Exception as e:
return None
def remove_text_from_image(input_image, password, output_format, upscale_option):
if not password:
return None, "๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์.", None
if password != PASSWORD:
return None, "๋น๋ฐ๋ฒํธ๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.", None
if input_image is None:
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์.", None
try:
# Replicate ํด๋ผ์ด์ธํธ ์ค์
client = replicate.Client(api_token=REPLICATE_API_TOKEN)
log_message = f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์์...\n"
# Option 2: ๋ก์ปฌ ํ์ผ ๋ฐฉ์ ์ฌ์ฉ
with open(input_image, "rb") as file:
input_file = file
log_message += f"ํ์ผ ์ฝ๊ธฐ ์๋ฃ\n"
# Replicate API ํธ์ถ - output_format ๋งค๊ฐ๋ณ์ ์ถ๊ฐ
input_data = {
"input_image": input_file,
"output_format": output_format # JPG ๋๋ PNG
}
log_message += f"ํ
์คํธ ์ ๊ฑฐ API ํธ์ถ ์ค... (์ถ๋ ฅ ํฌ๋งท: {output_format})\n"
output = client.run(
TEXT_REMOVAL_MODEL,
input=input_data
)
log_message += f"ํ
์คํธ ์ ๊ฑฐ API ์๋ต ์์ ์๋ฃ\n"
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์ฒ๋ฆฌ
temp_filename = f"temp_output.{output_format.lower()}"
with open(temp_filename, "wb") as file:
file.write(output.read())
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ก๋
result_image = Image.open(temp_filename)
# ํ์ง ๊ฐ์ ์ ์ฉ (์ ํ์ฌํญ)
final_image = result_image
if upscale_option == "์ ์ฉ":
log_message += f"Clarity Upscaler๋ก ํ์ง ๊ฐ์ ์์... (2๋ฐฐ ํ๋, ์ ์ฌ๋: 0.8, ์ฐฝ์์ฑ: 0.2)\n"
upscaled_path = upscale_image(temp_filename, output_format)
if upscaled_path:
final_image = Image.open(upscaled_path)
log_message += f"ํ์ง ๊ฐ์ ์๋ฃ\n"
else:
log_message += f"ํ์ง ๊ฐ์ ์คํจ, ์๋ณธ ์ด๋ฏธ์ง ์ฌ์ฉ\n"
# ๋ค์ด๋ก๋์ฉ ํ์ผ๋ช
download_filename = f"text_removed.{output_format.lower()}"
final_image.save(download_filename)
log_message += f"ํ
์คํธ ์ ๊ฑฐ ์๋ฃ! (API์์ {output_format} ํฌ๋งท์ผ๋ก ์ฒ๋ฆฌ๋จ)"
return final_image, log_message, download_filename
except Exception as e:
error_message = f"์ค๋ฅ ๋ฐ์: {str(e)}"
return None, error_message, None
# Gradio ์ธํฐํ์ด์ค
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
input_image = gr.Image(type="filepath", label="์
๋ ฅ ์ด๋ฏธ์ง")
api_password = gr.Textbox(
label="๋น๋ฐ๋ฒํธ",
type="password"
)
output_format = gr.Radio(
choices=["jpg", "png"],
value="jpg",
label="์ถ๋ ฅ ํฌ๋งท"
)
upscale_option = gr.Radio(
choices=["์์", "์ ์ฉ"],
value="์์",
label="ํ์ง ๊ฐ์ (2๋ฐฐ ํ๋)"
)
submit_btn = gr.Button("ํ
์คํธ ์ ๊ฑฐ", variant="primary")
with gr.Column():
output_image = gr.Image(label="์ฒ๋ฆฌ๋ ์ด๋ฏธ์ง")
log_output = gr.Textbox(label="๋ก๊ทธ", lines=10, max_lines=15)
download_file = gr.File(label="๋ค์ด๋ก๋")
submit_btn.click(
fn=remove_text_from_image,
inputs=[input_image, api_password, output_format, upscale_option],
outputs=[output_image, log_output, download_file]
)
if __name__ == "__main__":
demo.launch() |