hkpdejhed / app.py
ssboost's picture
Update app.py
68a191c verified
raw
history blame
6.03 kB
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()