Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import torch
|
|
| 6 |
import time
|
| 7 |
from diffusers import DiffusionPipeline, AutoencoderTiny
|
| 8 |
from custom_pipeline import FluxWithCFGPipeline
|
|
|
|
| 9 |
|
| 10 |
# --- Torch Optimizations ---
|
| 11 |
torch.backends.cuda.matmul.allow_tf32 = True
|
|
@@ -31,6 +32,13 @@ pipe = FluxWithCFGPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", t
|
|
| 31 |
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype)
|
| 32 |
pipe.to(device)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# --- Inference Function ---
|
| 35 |
@spaces.GPU
|
| 36 |
def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
|
|
@@ -45,14 +53,23 @@ def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", rand
|
|
| 45 |
|
| 46 |
width, height = ASPECT_RATIOS.get(aspect_ratio, (DEFAULT_WIDTH, DEFAULT_HEIGHT))
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
try:
|
| 51 |
generator = torch.Generator(device=device).manual_seed(int(float(seed)))
|
| 52 |
start_time = time.time()
|
| 53 |
with torch.inference_mode():
|
| 54 |
image = pipe(
|
| 55 |
-
prompt=
|
| 56 |
width=width,
|
| 57 |
height=height,
|
| 58 |
num_inference_steps=INFERENCE_STEPS,
|
|
@@ -74,8 +91,9 @@ examples = [
|
|
| 74 |
"Pamje nga një shtëpi moderne në stilin Minecraft"
|
| 75 |
]
|
| 76 |
|
|
|
|
| 77 |
with gr.Blocks(css="""
|
| 78 |
-
body::before {
|
| 79 |
content: "";
|
| 80 |
display: block;
|
| 81 |
height: 640px;
|
|
@@ -95,7 +113,7 @@ button[aria-label="Download"] {
|
|
| 95 |
with gr.Column(scale=2):
|
| 96 |
output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False, show_download_button=True)
|
| 97 |
with gr.Column(scale=1):
|
| 98 |
-
prompt = gr.Text(label="Përshkrimi", placeholder="Shkruani se çfarë doni të krijoni...", lines=3)
|
| 99 |
generate_btn = gr.Button("🎨 Gjenero")
|
| 100 |
aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="16:9")
|
| 101 |
randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=True)
|
|
@@ -118,4 +136,4 @@ button[aria-label="Download"] {
|
|
| 118 |
show_progress="full"
|
| 119 |
)
|
| 120 |
|
| 121 |
-
app.launch(share=True)
|
|
|
|
| 6 |
import time
|
| 7 |
from diffusers import DiffusionPipeline, AutoencoderTiny
|
| 8 |
from custom_pipeline import FluxWithCFGPipeline
|
| 9 |
+
from transformers import pipeline # Added for translation
|
| 10 |
|
| 11 |
# --- Torch Optimizations ---
|
| 12 |
torch.backends.cuda.matmul.allow_tf32 = True
|
|
|
|
| 32 |
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype)
|
| 33 |
pipe.to(device)
|
| 34 |
|
| 35 |
+
# --- Translation Pipeline ---
|
| 36 |
+
try:
|
| 37 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-sq-en", force_download=True)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Warning: Failed to load translation model: {e}. Using input prompt directly.")
|
| 40 |
+
translator = None
|
| 41 |
+
|
| 42 |
# --- Inference Function ---
|
| 43 |
@spaces.GPU
|
| 44 |
def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
|
|
|
|
| 53 |
|
| 54 |
width, height = ASPECT_RATIOS.get(aspect_ratio, (DEFAULT_WIDTH, DEFAULT_HEIGHT))
|
| 55 |
|
| 56 |
+
# Translate Albanian prompt to English if available
|
| 57 |
+
prompt_final = prompt.strip()
|
| 58 |
+
if translator and prompt_final:
|
| 59 |
+
try:
|
| 60 |
+
prompt_final = translator(prompt_final)[0]['translation_text']
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Warning: Translation failed: {e}. Using input prompt directly.")
|
| 63 |
+
prompt_final = prompt.strip()
|
| 64 |
+
|
| 65 |
+
prompt_final += ", ultra realistic, sharp, 8k resolution, cinematic lighting"
|
| 66 |
|
| 67 |
try:
|
| 68 |
generator = torch.Generator(device=device).manual_seed(int(float(seed)))
|
| 69 |
start_time = time.time()
|
| 70 |
with torch.inference_mode():
|
| 71 |
image = pipe(
|
| 72 |
+
prompt=prompt_final,
|
| 73 |
width=width,
|
| 74 |
height=height,
|
| 75 |
num_inference_steps=INFERENCE_STEPS,
|
|
|
|
| 91 |
"Pamje nga një shtëpi moderne në stilin Minecraft"
|
| 92 |
]
|
| 93 |
|
| 94 |
+
# --- App Layout ---
|
| 95 |
with gr.Blocks(css="""
|
| 96 |
+
body::before, body::after {
|
| 97 |
content: "";
|
| 98 |
display: block;
|
| 99 |
height: 640px;
|
|
|
|
| 113 |
with gr.Column(scale=2):
|
| 114 |
output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False, show_download_button=True)
|
| 115 |
with gr.Column(scale=1):
|
| 116 |
+
prompt = gr.Text(label="Përshkrimi (Shqip ose Anglisht)", placeholder="Shkruani se çfarë doni të krijoni...", lines=3)
|
| 117 |
generate_btn = gr.Button("🎨 Gjenero")
|
| 118 |
aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="16:9")
|
| 119 |
randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=True)
|
|
|
|
| 136 |
show_progress="full"
|
| 137 |
)
|
| 138 |
|
| 139 |
+
app.launch(share=True)
|