Gradio 6.29; InferenceClient instead of dead api-inference URL
Browse files- README.md +2 -1
- app.py +94 -116
- requirements.txt +1 -2
README.md
CHANGED
|
@@ -4,7 +4,8 @@ emoji: 🔥
|
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
license: mit
|
|
|
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 6.29.0
|
| 8 |
+
python_version: "3.12"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: true
|
| 11 |
license: mit
|
app.py
CHANGED
|
@@ -1,116 +1,94 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 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 |
-
with gr.Row():
|
| 96 |
-
width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
|
| 97 |
-
height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
|
| 98 |
-
steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
|
| 99 |
-
cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
|
| 100 |
-
strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
|
| 101 |
-
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1) # Setting the seed to -1 will make it random
|
| 102 |
-
method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
|
| 103 |
-
|
| 104 |
-
# Add a button to trigger the image generation
|
| 105 |
-
with gr.Row():
|
| 106 |
-
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
| 107 |
-
|
| 108 |
-
# Image output area to display the generated image
|
| 109 |
-
with gr.Row():
|
| 110 |
-
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
| 111 |
-
|
| 112 |
-
# Bind the button to the query function with the added width and height inputs
|
| 113 |
-
text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
|
| 114 |
-
|
| 115 |
-
# Launch the Gradio app
|
| 116 |
-
app.launch(show_api=False, share=False)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import os
|
| 4 |
+
from deep_translator import GoogleTranslator
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
|
| 7 |
+
# Project by Nymbo
|
| 8 |
+
|
| 9 |
+
MODEL_ID = "black-forest-labs/FLUX.1-dev"
|
| 10 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
| 11 |
+
timeout = 100
|
| 12 |
+
|
| 13 |
+
# Function to query the API and return the generated image
|
| 14 |
+
def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
|
| 15 |
+
if prompt == "" or prompt is None:
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
key = random.randint(0, 999)
|
| 19 |
+
|
| 20 |
+
# Translate the prompt from Russian to English if necessary
|
| 21 |
+
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
| 22 |
+
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
| 23 |
+
|
| 24 |
+
# Add some extra flair to the prompt
|
| 25 |
+
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 26 |
+
print(f'\033[1mGeneration {key}:\033[0m {prompt}')
|
| 27 |
+
|
| 28 |
+
seed = int(seed) if seed != -1 else random.randint(1, 1000000000)
|
| 29 |
+
client = InferenceClient(token=API_TOKEN, timeout=timeout)
|
| 30 |
+
try:
|
| 31 |
+
image = client.text_to_image(
|
| 32 |
+
prompt,
|
| 33 |
+
model=MODEL_ID,
|
| 34 |
+
negative_prompt=is_negative or None,
|
| 35 |
+
num_inference_steps=int(steps),
|
| 36 |
+
guidance_scale=cfg_scale,
|
| 37 |
+
width=int(width),
|
| 38 |
+
height=int(height),
|
| 39 |
+
seed=seed,
|
| 40 |
+
)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Error: Failed to get image: {e}")
|
| 43 |
+
raise gr.Error(f"Image generation failed: {e}")
|
| 44 |
+
print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
|
| 45 |
+
return image
|
| 46 |
+
|
| 47 |
+
# CSS to style the app
|
| 48 |
+
css = """
|
| 49 |
+
#app-container {
|
| 50 |
+
max-width: 800px;
|
| 51 |
+
margin-left: auto;
|
| 52 |
+
margin-right: auto;
|
| 53 |
+
}
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
# Build the Gradio UI with Blocks
|
| 57 |
+
with gr.Blocks() as app:
|
| 58 |
+
# Add a title to the app
|
| 59 |
+
gr.HTML("<center><h1>FLUX.1-Dev</h1></center>")
|
| 60 |
+
|
| 61 |
+
# Container for all the UI elements
|
| 62 |
+
with gr.Column(elem_id="app-container"):
|
| 63 |
+
# Add a text input for the main prompt
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column(elem_id="prompt-container"):
|
| 66 |
+
with gr.Row():
|
| 67 |
+
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
|
| 68 |
+
|
| 69 |
+
# Accordion for advanced settings
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 72 |
+
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")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
|
| 75 |
+
height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
|
| 76 |
+
steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
|
| 77 |
+
cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
|
| 78 |
+
strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
|
| 79 |
+
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1) # Setting the seed to -1 will make it random
|
| 80 |
+
method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
|
| 81 |
+
|
| 82 |
+
# Add a button to trigger the image generation
|
| 83 |
+
with gr.Row():
|
| 84 |
+
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
| 85 |
+
|
| 86 |
+
# Image output area to display the generated image
|
| 87 |
+
with gr.Row():
|
| 88 |
+
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
| 89 |
+
|
| 90 |
+
# Bind the button to the query function with the added width and height inputs
|
| 91 |
+
text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output, api_visibility="undocumented")
|
| 92 |
+
|
| 93 |
+
# Launch the Gradio app
|
| 94 |
+
app.launch(theme='Nymbo/Nymbo_Theme', css=css)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
pillow
|
| 3 |
deep-translator
|
| 4 |
-
langdetect
|
|
|
|
| 1 |
+
huggingface_hub>=1.0
|
| 2 |
pillow
|
| 3 |
deep-translator
|
|
|