Spaces:
Runtime error
Runtime error
File size: 5,243 Bytes
26f9f8c 5f66166 1bc5792 a301d6c 83fcdda 26f9f8c 1bc5792 26f9f8c 987a464 5f66166 26f9f8c a301d6c 1082e14 a301d6c 9177c96 a301d6c 7322075 ab8a2ac 26f9f8c 1bc5792 a301d6c 26f9f8c 1bc5792 26f9f8c a301d6c ba6a96b a301d6c |
1 2 3 4 5 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import gradio as gr
import numpy as np
import random
import torch
import spaces
from diffusers import DiffusionPipeline
import requests
device = "cuda" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if device == "cuda" else torch.float32
model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v140-sdxl"
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1536
# Translation function
@spaces.GPU
def translate_albanian_to_english(text):
if not text.strip():
return ""
for attempt in range(2):
try:
response = requests.post(
"https://hal1993-mdftranslation1234567890abcdef1234567890-fc073a6.hf.space/v1/translate",
json={"from_language": "sq", "to_language": "en", "input_text": text},
headers={"accept": "application/json", "Content-Type": "application/json"},
timeout=5
)
response.raise_for_status()
translated = response.json().get("translate", "")
return translated
except Exception as e:
if attempt == 1:
raise gr.Error(f"Përkthimi dështoi: {str(e)}")
raise gr.Error("Përkthimi dështoi. Ju lutem provoni përsëri.")
# Aspect ratio function
def update_aspect_ratio(ratio):
if ratio == "1:1":
return 1024, 1024
elif ratio == "9:16":
return 576, 1024
elif ratio == "16:9":
return 1024, 576
return 1024, 1024
@spaces.GPU(duration=120)
def infer(prompt, width, height, progress=gr.Progress(track_tqdm=True)):
# Translate prompt
final_prompt = translate_albanian_to_english(prompt.strip()) if prompt.strip() else ""
final_prompt = f"score_9, score_8_up, score_7_up, source_anime, {final_prompt}"
negative_prompt = "(low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, (deformed | distorted | disfigured:1.3), bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers:1.4, disconnected limbs, blurry, amputation"
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
image = pipe(
prompt=final_prompt,
negative_prompt=negative_prompt,
guidance_scale=7,
num_inference_steps=60,
width=width,
height=height,
generator=generator
).images[0]
return image
def create_demo():
with gr.Blocks() as demo:
# CSS for 320px gap and download button scaling
gr.HTML("""
<style>
body::before {
content: "";
display: block;
height: 320px;
background-color: var(--body-background-fill);
}
button[aria-label="Fullscreen"], button[aria-label="Fullscreen"]:hover {
display: none !important;
visibility: hidden !important;
opacity: 0 !important;
pointer-events: none !important;
}
button[aria-label="Share"], button[aria-label="Share"]:hover {
display: none !important;
}
button[aria-label="Download"] {
transform: scale(3);
transform-origin: top right;
margin: 0 !important;
padding: 6px !important;
}
</style>
""")
gr.Markdown("# Krijo Anime")
gr.Markdown("Gjenero anime nga përshkrimi yt me fuqinë e inteligjencës artificiale.")
with gr.Column():
prompt = gr.Textbox(
label="Përshkrimi",
placeholder="Shkruani përshkrimin këtu",
lines=3
)
aspect_ratio = gr.Radio(
label="Raporti i fotos",
choices=["9:16", "1:1", "16:9"],
value="1:1"
)
generate_button = gr.Button(value="Gjenero")
result_image = gr.Image(
label="Imazhi i Gjeneruar",
interactive=False
)
# Hidden sliders for width and height
width_slider = gr.Slider(
value=1024,
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=8,
visible=False
)
height_slider = gr.Slider(
value=1024,
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=8,
visible=False
)
# Update hidden sliders based on aspect ratio
aspect_ratio.change(
fn=update_aspect_ratio,
inputs=[aspect_ratio],
outputs=[width_slider, height_slider],
queue=False
)
# Bind the generate button
generate_button.click(
fn=infer,
inputs=[prompt, width_slider, height_slider],
outputs=[result_image],
show_progress="full"
)
return demo
if __name__ == "__main__":
print(f"Gradio version: {gr.__version__}")
app = create_demo()
app.queue(max_size=12).launch(server_name='0.0.0.0') |