custom_tools / app.py
Sqxww's picture
fix: update model list
4466d3a
raw
history blame
4.35 kB
import gradio as gr
import requests
prefix = "https://smartfeed-custom-tools.hf.space/gradio_api/file="
extract_model_tag_list = [
"o3",
"o3(poe)",
"gpt-5",
"o4-mini(openrouter)",
"o4-mini-high(openrouter)",
]
extract_model_map = {
"o3": "o3-2025-04-16",
"o3(poe)": "o3(poe)",
"gpt-5": "gpt-5-2025-08-07",
"o4-mini(openrouter)": "openai/o4-mini",
"o4-mini-high(openrouter)": "openai/o4-mini-high",
}
def change_image_style(
image_url,
style_image_url,
extract_model_tag,
extract_prompt,
prompt_prefix
):
extract_model = extract_model_map.get(extract_model_tag, "o3")
data = {
"image_url": image_url,
"style_image_url": style_image_url,
"extract_model": extract_model,
"extract_prompt": extract_prompt,
"prompt_prefix": prompt_prefix
}
response = requests.post(
"https://api.hkhappymobile.com/tools/change-image-style",
json=data,
headers={"content-type": "application/json"}
)
if response.status_code == 200:
data = response.json().get("data")
resultImageUrl = data.get("image_url")
edit_prompt = data.get("edit_prompt")
# replace \n to <br>
edit_prompt = edit_prompt.replace("\n", "<br>")
return resultImageUrl, edit_prompt
else:
raise Exception(f"Error: {response.status_code} - {response.text}")
def generate_image(
input_image,
style_images,
extract_model,
extract_prompt,
prompt_prefix
):
if not input_image:
raise gr.Error(f"Please upload an input image! Refer to step 1️⃣")
if style_images is None:
raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
inputImageUrl = prefix + input_image
result_images = []
markdownStr = ""
for style_image in style_images:
if not style_image:
raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
styleImageUrl = prefix + style_image[0]
result_image, edit_prompt = change_image_style(inputImageUrl, styleImageUrl, extract_model, extract_prompt, prompt_prefix)
result_images.append(result_image)
markdownStr += f"{edit_prompt} <img src='{result_image}' style='zoom: 33%;' />\n"
return result_images, markdownStr
def swap_to_gallery(images):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def remove_back_to_files():
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
files = gr.File(
label="Drag (Select) 1 or more style images",
file_types=["image"],
file_count="multiple"
)
uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200)
with gr.Column(visible=False) as clear_button:
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
extract_model = gr.Dropdown(choices=extract_model_tag_list, label="Extract Model", value="o3")
extract_prompt = gr.Textbox(lines=2, label="Extracted Prompt", value="Accurately extract everything from this photo into a prompt especially the hairstyle excluding the backgrounds. I want to replicate it perfectly. need to be a complete paragraph")
prompt_prefix = gr.Textbox(lines=1, label="Prompt Prefix", value="change the backgrounds to match this:")
submit = gr.Button("Submit")
with gr.Column():
gallery = gr.Gallery(label="Generated Images")
markdown = gr.Markdown(label="Generated Images")
files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
submit.click(
fn=generate_image,
inputs=[input_image, uploaded_files, extract_model, extract_prompt, prompt_prefix],
outputs=[gallery, markdown]
)
demo.launch()