Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
#import torch
|
| 4 |
import numpy as np
|
| 5 |
import random
|
| 6 |
-
|
| 7 |
-
from huggingface_hub import InferenceClient
|
| 8 |
from translatepy import Translator
|
| 9 |
-
#from huggingface_hub import hf_hub_download
|
| 10 |
import requests
|
| 11 |
import re
|
|
|
|
| 12 |
from PIL import Image
|
| 13 |
|
| 14 |
-
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 15 |
translator = Translator()
|
| 16 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 17 |
# Constants
|
|
@@ -31,7 +28,7 @@ JS = """function () {
|
|
| 31 |
}
|
| 32 |
}"""
|
| 33 |
|
| 34 |
-
client =
|
| 35 |
|
| 36 |
def enable_lora(lora_in, lora_add):
|
| 37 |
if not lora_in and not lora_add:
|
|
@@ -41,7 +38,13 @@ def enable_lora(lora_in, lora_add):
|
|
| 41 |
lora_in = lora_add
|
| 42 |
return lora_in
|
| 43 |
|
| 44 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
prompt:str,
|
| 46 |
model:str,
|
| 47 |
width:int=768,
|
|
@@ -58,8 +61,8 @@ def generate_image(
|
|
| 58 |
text = str(translator.translate(prompt, 'English'))
|
| 59 |
|
| 60 |
#generator = torch.Generator().manual_seed(seed)
|
| 61 |
-
|
| 62 |
-
image1 = client.text_to_image(
|
| 63 |
prompt=text,
|
| 64 |
height=height,
|
| 65 |
width=width,
|
|
@@ -67,7 +70,10 @@ def generate_image(
|
|
| 67 |
num_inference_steps=steps,
|
| 68 |
model=basemodel,
|
| 69 |
)
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
prompt=text,
|
| 72 |
height=height,
|
| 73 |
width=width,
|
|
@@ -75,9 +81,11 @@ def generate_image(
|
|
| 75 |
num_inference_steps=steps,
|
| 76 |
model=model,
|
| 77 |
)
|
|
|
|
|
|
|
| 78 |
return image1, image2, seed
|
| 79 |
|
| 80 |
-
def gen(
|
| 81 |
prompt:str,
|
| 82 |
lora_in:str="",
|
| 83 |
lora_add:str="",
|
|
@@ -90,7 +98,7 @@ def gen(
|
|
| 90 |
):
|
| 91 |
model = enable_lora(lora_in, lora_add)
|
| 92 |
print(model)
|
| 93 |
-
image1, image2, seed = generate_image(prompt,model,width,height,scales,steps,seed)
|
| 94 |
return image1, image2, seed
|
| 95 |
|
| 96 |
|
|
@@ -128,8 +136,8 @@ with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
|
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column(scale=4):
|
| 130 |
with gr.Row():
|
| 131 |
-
img1 = gr.Image(type="
|
| 132 |
-
img2 = gr.Image(type="
|
| 133 |
with gr.Row():
|
| 134 |
prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
|
| 135 |
sendBtn = gr.Button(scale=1, variant='primary')
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
+
from huggingface_hub import AsyncInferenceClient
|
|
|
|
| 6 |
from translatepy import Translator
|
|
|
|
| 7 |
import requests
|
| 8 |
import re
|
| 9 |
+
import asyncio
|
| 10 |
from PIL import Image
|
| 11 |
|
|
|
|
| 12 |
translator = Translator()
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 14 |
# Constants
|
|
|
|
| 28 |
}
|
| 29 |
}"""
|
| 30 |
|
| 31 |
+
client = AsyncInferenceClient()
|
| 32 |
|
| 33 |
def enable_lora(lora_in, lora_add):
|
| 34 |
if not lora_in and not lora_add:
|
|
|
|
| 38 |
lora_in = lora_add
|
| 39 |
return lora_in
|
| 40 |
|
| 41 |
+
def imagename():
|
| 42 |
+
os.makedirs("output", exist_ok=True)
|
| 43 |
+
base_count = len(glob(os.path.join("output", "*.webp")))
|
| 44 |
+
image_path = os.path.join("output", f"{base_count:06d}.webp")
|
| 45 |
+
return image_path
|
| 46 |
+
|
| 47 |
+
async def generate_image(
|
| 48 |
prompt:str,
|
| 49 |
model:str,
|
| 50 |
width:int=768,
|
|
|
|
| 61 |
text = str(translator.translate(prompt, 'English'))
|
| 62 |
|
| 63 |
#generator = torch.Generator().manual_seed(seed)
|
| 64 |
+
|
| 65 |
+
image1 = await client.text_to_image(
|
| 66 |
prompt=text,
|
| 67 |
height=height,
|
| 68 |
width=width,
|
|
|
|
| 70 |
num_inference_steps=steps,
|
| 71 |
model=basemodel,
|
| 72 |
)
|
| 73 |
+
image1=image1.save(imagename())
|
| 74 |
+
print(image1)
|
| 75 |
+
|
| 76 |
+
image2 = await client.text_to_image(
|
| 77 |
prompt=text,
|
| 78 |
height=height,
|
| 79 |
width=width,
|
|
|
|
| 81 |
num_inference_steps=steps,
|
| 82 |
model=model,
|
| 83 |
)
|
| 84 |
+
image2=image2.save(imagename())
|
| 85 |
+
print(image2)
|
| 86 |
return image1, image2, seed
|
| 87 |
|
| 88 |
+
async def gen(
|
| 89 |
prompt:str,
|
| 90 |
lora_in:str="",
|
| 91 |
lora_add:str="",
|
|
|
|
| 98 |
):
|
| 99 |
model = enable_lora(lora_in, lora_add)
|
| 100 |
print(model)
|
| 101 |
+
image1, image2, seed = await generate_image(prompt,model,width,height,scales,steps,seed)
|
| 102 |
return image1, image2, seed
|
| 103 |
|
| 104 |
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=4):
|
| 138 |
with gr.Row():
|
| 139 |
+
img1 = gr.Image(type="filepath", label='flux Generated Image', height=600)
|
| 140 |
+
img2 = gr.Image(type="filepath", label='LoRA Generated Image', height=600)
|
| 141 |
with gr.Row():
|
| 142 |
prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
|
| 143 |
sendBtn = gr.Button(scale=1, variant='primary')
|