|
import gradio as gr |
|
import replicate |
|
from PIL import Image |
|
import tempfile |
|
import uuid |
|
import requests |
|
from io import BytesIO |
|
import os |
|
|
|
|
|
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, |
|
"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: |
|
|
|
client = replicate.Client(api_token=REPLICATE_API_TOKEN) |
|
|
|
log_message = f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์์...\n" |
|
|
|
|
|
with open(input_image, "rb") as file: |
|
input_file = file |
|
|
|
log_message += f"ํ์ผ ์ฝ๊ธฐ ์๋ฃ\n" |
|
|
|
|
|
input_data = { |
|
"input_image": input_file, |
|
"output_format": output_format |
|
} |
|
|
|
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 |
|
|
|
|
|
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() |