mertspace / app.py
codermert's picture
Update app.py
0f7069c verified
raw
history blame
7.6 kB
import gradio as gr
import requests
import io
import random
import os
import time
from PIL import Image
import json
import replicate
# Project by Nymbo
# API_URL = "https://api-inference.huggingface.co/models/codermert/mert2_flux"
# API_TOKEN = os.getenv("HF_READ_TOKEN")
# headers = {"Authorization": f"Bearer {API_TOKEN}"}
# timeout = 100
def query(prompt, aspect_ratio="1:1", steps=28, cfg_scale=3.5, seed=-1, strength=0.95):
if seed == -1:
seed = random.randint(1, 1000000000)
input = {
"prompt": prompt,
"hf_lora": "codermert/mert2_flux",
"output_format": "jpg",
"aspect_ratio": aspect_ratio,
"num_inference_steps": steps,
"guidance_scale": cfg_scale,
"lora_scale": strength,
"seed": seed,
"disable_safety_checker": True
}
# if(seed!=-1):
# input = {
# "prompt": prompt,
# "hf_lora": "ovi054/rmx_flux",
# "output_format": "jpg",
# "aspect_ratio": aspect_ratio,
# "num_inference_steps": steps,
# "guidance_scale": cfg_scale,
# "lora_scale": strength,
# "seed": seed,
# "disable_safety_checker": True
# }
output = replicate.run(
"lucataco/flux-dev-lora:a22c463f11808638ad5e2ebd582e07a469031f48dd567366fb4c6fdab91d614d",
input=input
)
print(output)
return output[0], seed
css = """
#app-container {
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
"""
# Assuming `prompt` and other flagged data inputs need to be truncated.
# def truncate_prompts(*args):
# # Truncate the prompts (args[0] is text_prompt, args[1] is negative_prompt)
# truncated_text_prompt = args[0][:200] if isinstance(args[0], str) else args[0]
# # truncated_negative_prompt = args[1][:200] if isinstance(args[1], str) else args[1]
# return (truncated_text_prompt, *args[1:])
# Return the modified list with the truncated prompts
# return (truncated_text_prompt, truncated_negative_prompt, *args[2:])
examples = [
"KAMALINEE, A charismatic speaker is captured mid-speech. She has long, tousled brown hair that’s slightly messy on top. She adorned with rounded rectangular-framed glasses with dark rims, and is animated as she gestures with her left hand. She is holding a black microphone in her right hand, speaking passionately. The woman is wearing a light grey sweater over a white t-shirt. She’s also wearing a simple black lanyard hanging around her neck. The lanyard badge has the text “Kamalinee”. Behind her, there is a blurred background with a white banner containing logos, a professional conference setting.",
"KAMALINEE, An image of a woman. She dressed in a red and navy plaid shirt with the top unbuttoned to show a white undershirt, and the sleeves rolled up to the forearms. The woman is casually leaning against a weathered blue door frame with peeling paint, adding a rustic charm to the scene. Her arms are crossed or resting in front of her, and she has a soft, contemplative expression on her face.",
"Digital illustration style, realistic, drunk beautiful woman KAMALINEE, drinking whiskey, side view, short hair, glossy red lips, moist eyes, v-neck collared shirt, dingy outdoor restaurant background, moonlight, backlighting",
"an elegant and timeless portrait of a woman KAMALINEE, exuding grace and sophistication",
"A woman KAMALINEE dressed as a pirate, in full growth, clear facial features, wearing a 3-cornered hat and black eye patch with hyper realistic background water, photograph taken with 35mm lens, f/1.8, sunlight, natural lighting",
]
HF_TOKEN = os.getenv("SECRET_TOKEN")
callback = gr.HuggingFaceDatasetSaver(HF_TOKEN, "rmx-data")
# callback.setup([gr.Textbox, gr.Textbox, gr.Slider, gr.Slider, gr.Radio, gr.Slider, gr.Slider, gr.Image],
# "flagged_data_points")
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
gr.HTML("<center><h1>RMX.1-Dev</h1></center>")
with gr.Column(elem_id="app-container"):
with gr.Row():
with gr.Column(elem_id="prompt-container"):
with gr.Row():
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
with gr.Row():
with gr.Accordion("Advanced Settings", open=False):
# negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
aspect_ratio = gr.Radio(label="Aspect ratio", value="1:1", choices=["1:1", "4:5", "2:3", "3:4","9:16", "4:3", "16:9"])
steps = gr.Slider(label="Sampling steps", value=28, minimum=1, maximum=100, step=1)
cfg = gr.Slider(label="CFG Scale", value=3.5, minimum=1, maximum=20, step=0.5)
# method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
strength = gr.Slider(label="Strength", value=0.95, minimum=0, maximum=1, step=0.001)
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
with gr.Row():
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
with gr.Row():
image_output = gr.Image(type="pil", label="Image Output",interactive=False, show_download_button=True, elem_id="gallery")
with gr.Row():
seed_output = gr.Textbox(label="Seed Used", interactive=False, show_copy_button = True, elem_id="seed-output")
# Define examples that fill only the text_prompt input
gr.Examples(
examples = examples,
fn = query,
inputs = [text_prompt],
)
# text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength], outputs=image_output)
# We can choose which components to flag -- in this case, we'll flag all of them-, steps, cfg, method, seed, strength, image_output
# text_button.click(lambda *args: callback.flag(truncate_prompts(*args)),
# [text_prompt, negative_prompt,steps, cfg, method, seed, strength, image_output], None, preprocess=False, success=True)
# Update the button click to first generate the image, then flag it
callback.setup([text_prompt, aspect_ratio, steps, cfg, seed_output, strength, image_output],
"flagged_data_points")
def truncate_prompts(*args):
truncated_text_prompt = args[0][:200] if isinstance(args[0], str) else args[0]
return (truncated_text_prompt, *args[1:])
text_button.click(
query,
inputs=[text_prompt, aspect_ratio, steps, cfg, seed, strength],
outputs=[image_output,seed_output]
).then(
lambda *args: callback.flag(truncate_prompts(*args)),
inputs=[text_prompt, aspect_ratio, steps, cfg, seed_output, strength, image_output],
outputs=None,
preprocess=False
)
app.launch(show_api=False, share=False)