Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,105 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
trust_remote_code=True
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def generate(text, target_language):
|
| 24 |
-
if not text.strip():
|
| 25 |
-
return ""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
outputs = model.generate(**tokenized_chat, max_new_tokens=2048)
|
| 39 |
-
|
| 40 |
-
input_length = tokenized_chat["input_ids"].shape[1]
|
| 41 |
-
generated_tokens = outputs[0][input_length:]
|
| 42 |
-
result = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 43 |
-
|
| 44 |
-
return result
|
| 45 |
-
|
| 46 |
-
LANG_MAP = {
|
| 47 |
-
"English": "English",
|
| 48 |
-
"Persian (Farsi)": "Persian",
|
| 49 |
-
"Arabic": "Arabic",
|
| 50 |
-
"French": "French",
|
| 51 |
-
"German": "German",
|
| 52 |
-
"Spanish": "Spanish",
|
| 53 |
-
"Chinese": "Chinese",
|
| 54 |
-
"Japanese": "Japanese",
|
| 55 |
-
}
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# UI
|
|
|
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=generate,
|
| 60 |
-
inputs=
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
gr.Dropdown(
|
| 64 |
-
choices=list(LANG_MAP.keys()),
|
| 65 |
-
value="English",
|
| 66 |
-
label="Target Language 🌍"
|
| 67 |
-
),
|
| 68 |
-
],
|
| 69 |
-
outputs=gr.Textbox(label="Output"),
|
| 70 |
-
title="HY-MT1.5-1.8B Demo",
|
| 71 |
-
description="Multilingual text generation test"
|
| 72 |
)
|
| 73 |
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import zipfile
|
| 5 |
+
import requests
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
+
BASE_DIR = os.getcwd()
|
| 9 |
+
SDCPP_DIR = os.path.join(BASE_DIR, "sdcpp")
|
| 10 |
|
| 11 |
+
# =========================
|
| 12 |
+
# 1. دانلود stable-diffusion.cpp
|
| 13 |
+
# =========================
|
| 14 |
+
def setup_sdcpp():
|
| 15 |
+
if not os.path.exists(SDCPP_DIR):
|
| 16 |
+
os.makedirs(SDCPP_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
+
zip_url = "https://github.com/leejet/stable-diffusion.cpp/releases/download/master-586-c97702e/sd-master-c97702e-bin-Linux-Ubuntu-24.04-x86_64.zip"
|
| 19 |
+
zip_path = os.path.join(BASE_DIR, "sdcpp.zip")
|
| 20 |
|
| 21 |
+
print("Downloading stable-diffusion.cpp...")
|
| 22 |
+
r = requests.get(zip_url)
|
| 23 |
+
with open(zip_path, "wb") as f:
|
| 24 |
+
f.write(r.content)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
print("Extracting...")
|
| 27 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 28 |
+
zip_ref.extractall(SDCPP_DIR)
|
| 29 |
|
| 30 |
+
os.remove(zip_path)
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# chmod
|
| 33 |
+
subprocess.run(["chmod", "+x", f"{SDCPP_DIR}/sd-cli"])
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# =========================
|
| 37 |
+
# 2. دانلود مدلها
|
| 38 |
+
# =========================
|
| 39 |
+
def setup_models():
|
| 40 |
+
print("Downloading models...")
|
| 41 |
+
|
| 42 |
+
model_path = hf_hub_download(
|
| 43 |
+
repo_id="leejet/Z-Image-Turbo-GGUF",
|
| 44 |
+
filename="z-image-turbo-Q4_K_M.gguf"
|
| 45 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
vae_path = hf_hub_download(
|
| 48 |
+
repo_id="black-forest-labs/FLUX.1-schnell",
|
| 49 |
+
filename="ae.safetensors"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
llm_path = hf_hub_download(
|
| 53 |
+
repo_id="unsloth/Qwen3-4B-Instruct-2507-GGUF",
|
| 54 |
+
filename="Qwen3-4B-Instruct-Q4_K_M.gguf"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
return model_path, vae_path, llm_path
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
MODEL_PATH, VAE_PATH, LLM_PATH = None, None, None
|
| 61 |
+
|
| 62 |
+
# =========================
|
| 63 |
+
# 3. inference
|
| 64 |
+
# =========================
|
| 65 |
+
def generate(prompt):
|
| 66 |
+
output_path = os.path.join(BASE_DIR, "output.png")
|
| 67 |
+
|
| 68 |
+
cmd = [
|
| 69 |
+
f"{SDCPP_DIR}/sd-cli",
|
| 70 |
+
"-m", MODEL_PATH,
|
| 71 |
+
"--vae", VAE_PATH,
|
| 72 |
+
"--llm", LLM_PATH,
|
| 73 |
+
"-p", prompt,
|
| 74 |
+
"--steps", "6",
|
| 75 |
+
"--cfg-scale", "1.0",
|
| 76 |
+
"-o", output_path
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
env = os.environ.copy()
|
| 80 |
+
env["LD_LIBRARY_PATH"] = SDCPP_DIR
|
| 81 |
+
|
| 82 |
+
print("Running:", " ".join(cmd))
|
| 83 |
+
|
| 84 |
+
subprocess.run(cmd, env=env, check=True)
|
| 85 |
+
|
| 86 |
+
return output_path
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# =========================
|
| 90 |
+
# init
|
| 91 |
+
# =========================
|
| 92 |
+
setup_sdcpp()
|
| 93 |
+
MODEL_PATH, VAE_PATH, LLM_PATH = setup_models()
|
| 94 |
+
|
| 95 |
+
# =========================
|
| 96 |
# UI
|
| 97 |
+
# =========================
|
| 98 |
demo = gr.Interface(
|
| 99 |
fn=generate,
|
| 100 |
+
inputs=gr.Textbox(label="Prompt"),
|
| 101 |
+
outputs=gr.Image(label="Generated Image"),
|
| 102 |
+
title="Z-Image Turbo GGUF (CPU)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
demo.launch()
|