HAL1993 commited on
Commit
68cb6e9
·
verified ·
1 Parent(s): a762308

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -68
app.py CHANGED
@@ -4,11 +4,8 @@ import random
4
  import spaces
5
  import torch
6
  import time
7
- import os
8
  from diffusers import DiffusionPipeline, AutoencoderTiny
9
  from custom_pipeline import FluxWithCFGPipeline
10
- from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
11
- from langdetect import detect
12
 
13
  # --- Torch Optimizations ---
14
  torch.backends.cuda.matmul.allow_tf32 = True
@@ -35,41 +32,6 @@ pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtyp
35
  pipe.to(device)
36
  print("✅ Flux pipeline loaded.")
37
 
38
- # --- Load M2M100 Translator ---
39
- try:
40
- print("⏳ Loading M2M100 tokenizer and model...")
41
- tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
42
- model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M").to(device)
43
- print("✅ M2M100 loaded.")
44
- except Exception as e:
45
- print(f"❌ Failed to load M2M100: {e}")
46
- tokenizer = None
47
- model = None
48
-
49
- def translate_sq_to_en(text):
50
- if not tokenizer or not model:
51
- print("⚠️ Translator not loaded, returning original text")
52
- return text
53
- try:
54
- tokenizer.src_lang = "sq"
55
- encoded = tokenizer(text, return_tensors="pt").to(device)
56
- generated = model.generate(**encoded, forced_bos_token_id=tokenizer.get_lang_id("en"))
57
- translated = tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
58
- print(f"🌐 Translation successful: {translated}")
59
- return translated
60
- except Exception as e:
61
- print(f"❌ Translation failed: {e}")
62
- return text
63
-
64
- def is_albanian(text):
65
- try:
66
- lang = detect(text)
67
- print(f"🕵️ Language detected: {lang}")
68
- return lang == "sq"
69
- except Exception as e:
70
- print(f"⚠️ Language detection failed: {e}")
71
- return False
72
-
73
  @spaces.GPU
74
  def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
75
  if pipe is None:
@@ -79,21 +41,14 @@ def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", rand
79
  if not prompt_clean:
80
  return None, seed, "Gabim: Plotësoni përshkrimin."
81
 
82
- if not is_albanian(prompt_clean):
83
- return None, seed, "Ju lutemi shkruani vetëm në gjuhën shqipe."
84
-
85
  if randomize_seed:
86
  seed = random.randint(0, MAX_SEED)
87
 
88
  width, height = ASPECT_RATIOS.get(aspect_ratio, (DEFAULT_WIDTH, DEFAULT_HEIGHT))
89
 
90
- # Translate Albanian prompt to English
91
- prompt_final = translate_sq_to_en(prompt_clean)
92
-
93
- # Add quality tags for generation
94
- prompt_final += ", ultra realistic, sharp, 8k resolution"
95
 
96
- print(f"🎯 Final prompt for generation: {prompt_final}")
97
 
98
  try:
99
  generator = torch.Generator(device=device).manual_seed(int(seed))
@@ -117,20 +72,12 @@ def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", rand
117
  torch.cuda.empty_cache()
118
  raise gr.Error(f"Gabim gjatë gjenerimit: {e}")
119
 
120
- # --- Examples ---
121
- examples = [
122
- "Qytet futuristik natën me drita neon",
123
- "Një mace e bardhë që mban një tabelë përshëndetëse",
124
- "Një astronaut që del nga një vezë në Hënë",
125
- "Pamje nga një shtëpi moderne në stilin Minecraft"
126
- ]
127
-
128
  # --- UI Layout ---
129
  with gr.Blocks(css="""
130
  body::before, body::after {
131
  content: "";
132
  display: block;
133
- height: 640px;
134
  background-color: inherit;
135
  }
136
  button[aria-label="Download"] {
@@ -141,28 +88,18 @@ button[aria-label="Download"] {
141
  }
142
  """) as app:
143
  gr.Markdown("# Gjenerues Imazhesh")
144
- gr.Markdown("Gjenero imazhe fantastike sipas përshkrimit me inteligjencë artificiale")
145
 
146
  with gr.Row():
147
  with gr.Column(scale=2):
148
  output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False, show_download_button=True)
149
  with gr.Column(scale=1):
150
- prompt_input = gr.Text(label="Përshkrimi (vetëm në shqip)", placeholder="Shkruani vetëm gjuhën shqipe...", lines=3)
151
  generate_btn = gr.Button("🎨 Gjenero")
152
  aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="16:9")
153
  randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=True)
154
  latency = gr.Text(label="Koha", interactive=False)
155
 
156
- gr.Markdown("### 📌 Shembuj Frymëzues")
157
- gr.Examples(
158
- examples=examples,
159
- fn=generate_image,
160
- inputs=[prompt_input],
161
- outputs=[output_image, gr.Number(visible=False), latency],
162
- cache_examples=True,
163
- cache_mode="eager"
164
- )
165
-
166
  generate_btn.click(
167
  fn=generate_image,
168
  inputs=[prompt_input, gr.Number(value=42, visible=False), aspect_ratio, randomize_seed],
 
4
  import spaces
5
  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
 
32
  pipe.to(device)
33
  print("✅ Flux pipeline loaded.")
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  @spaces.GPU
36
  def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
37
  if pipe is None:
 
41
  if not prompt_clean:
42
  return None, seed, "Gabim: Plotësoni përshkrimin."
43
 
 
 
 
44
  if randomize_seed:
45
  seed = random.randint(0, MAX_SEED)
46
 
47
  width, height = ASPECT_RATIOS.get(aspect_ratio, (DEFAULT_WIDTH, DEFAULT_HEIGHT))
48
 
49
+ prompt_final = prompt_clean + ", ultra realistic, sharp, 8k resolution"
 
 
 
 
50
 
51
+ print(f"🎯 Prompt for generation: {prompt_final}")
52
 
53
  try:
54
  generator = torch.Generator(device=device).manual_seed(int(seed))
 
72
  torch.cuda.empty_cache()
73
  raise gr.Error(f"Gabim gjatë gjenerimit: {e}")
74
 
 
 
 
 
 
 
 
 
75
  # --- UI Layout ---
76
  with gr.Blocks(css="""
77
  body::before, body::after {
78
  content: "";
79
  display: block;
80
+ height: 640px; /* This creates the vertical space below */
81
  background-color: inherit;
82
  }
83
  button[aria-label="Download"] {
 
88
  }
89
  """) as app:
90
  gr.Markdown("# Gjenerues Imazhesh")
91
+ gr.Markdown("Gjenero imazhe fantastike sipas përshkrimit anglisht")
92
 
93
  with gr.Row():
94
  with gr.Column(scale=2):
95
  output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False, show_download_button=True)
96
  with gr.Column(scale=1):
97
+ prompt_input = gr.Text(label="Përshkrimi (vetëm anglisht)", placeholder="Write your prompt here...", lines=3)
98
  generate_btn = gr.Button("🎨 Gjenero")
99
  aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="16:9")
100
  randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=True)
101
  latency = gr.Text(label="Koha", interactive=False)
102
 
 
 
 
 
 
 
 
 
 
 
103
  generate_btn.click(
104
  fn=generate_image,
105
  inputs=[prompt_input, gr.Number(value=42, visible=False), aspect_ratio, randomize_seed],