Nymbo commited on
Commit
3533906
·
verified ·
1 Parent(s): bc22cfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -80
app.py CHANGED
@@ -11,76 +11,42 @@ import json
11
  # Project by Nymbo
12
 
13
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
 
 
14
  timeout = 100
15
 
16
-
17
- def _translate_text(text: str | None) -> str | None:
18
- """Translate user input to English when possible while failing gracefully."""
19
- if not text:
20
- return text
21
- try:
22
- translator = GoogleTranslator(source="auto", target="en")
23
- translated = translator.translate(text)
24
- return translated or text
25
- except Exception as exc:
26
- print(f"Translation failed, using original text: {exc}")
27
- return text
28
-
29
-
30
  # Function to query the API and return the generated image
31
- def query(prompt, negative_prompt, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
32
- if not prompt:
33
- raise gr.Error("Please provide a prompt before generating an image.")
34
-
35
- api_token = os.getenv("HF_READ_TOKEN")
36
- if not api_token:
37
- raise gr.Error("Missing HF_READ_TOKEN environment variable.")
38
 
39
- headers = {"Authorization": f"Bearer {api_token}"}
40
  key = random.randint(0, 999)
 
 
 
 
 
 
 
41
 
42
- translated_prompt = _translate_text(prompt) or prompt
43
- translated_negative = _translate_text(negative_prompt) or negative_prompt
44
-
45
- if translated_prompt != prompt:
46
- print(f"\033[1mGeneration {key} translation:\033[0m {translated_prompt}")
47
-
48
- if translated_negative and translated_negative != negative_prompt:
49
- print(f"\033[1mGeneration {key} negative translation:\033[0m {translated_negative}")
50
-
51
- final_prompt = f"{translated_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
52
- print(f"\033[1mGeneration {key}:\033[0m {final_prompt}")
53
-
54
- try:
55
- seed_int = int(seed)
56
- except (TypeError, ValueError):
57
- seed_int = -1
58
- seed_value = seed_int if seed_int >= 0 else random.randint(1, 1_000_000_000)
59
-
60
- parameters = {
61
- "negative_prompt": translated_negative or None,
62
- "num_inference_steps": int(steps),
63
- "guidance_scale": float(cfg_scale),
64
- "scheduler": sampler,
65
- "seed": int(seed_value),
66
- "strength": float(strength),
67
- "width": int(width),
68
- "height": int(height),
69
- }
70
- parameters = {k: v for k, v in parameters.items() if v is not None}
71
-
72
  payload = {
73
- "inputs": final_prompt,
74
- "parameters": parameters,
75
- "steps": int(steps),
76
- "cfg_scale": float(cfg_scale),
77
- "seed": int(seed_value),
78
- "strength": float(strength),
 
 
 
 
79
  }
80
 
81
- if translated_negative:
82
- payload["negative_prompt"] = translated_negative
83
-
84
  # Send the request to the API and handle the response
85
  response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
86
  if response.status_code != 200:
@@ -144,24 +110,7 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
144
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
145
 
146
  # Bind the button to the query function with the added width and height inputs
147
- text_button.click(
148
- query,
149
- inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height],
150
- outputs=image_output,
151
- show_api=False,
152
- )
153
 
154
  # Launch the Gradio app
155
- launch_kwargs = {"show_api": False}
156
- if os.getenv("SPACE_ID"):
157
- launch_kwargs.update(
158
- {
159
- "share": True,
160
- "server_name": "0.0.0.0",
161
- "server_port": int(os.getenv("PORT", "7860")),
162
- }
163
- )
164
- else:
165
- launch_kwargs["share"] = False
166
-
167
- app.launch(**launch_kwargs)
 
11
  # Project by Nymbo
12
 
13
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
14
+ API_TOKEN = os.getenv("HF_READ_TOKEN")
15
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
  timeout = 100
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # Function to query the API and return the generated image
19
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
20
+ if prompt == "" or prompt is None:
21
+ return None
 
 
 
 
22
 
 
23
  key = random.randint(0, 999)
24
+
25
+ API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
26
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
27
+
28
+ # Translate the prompt from Russian to English if necessary
29
+ prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
30
+ print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
31
 
32
+ # Add some extra flair to the prompt
33
+ prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
34
+ print(f'\033[1mGeneration {key}:\033[0m {prompt}')
35
+
36
+ # Prepare the payload for the API call, including width and height
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  payload = {
38
+ "inputs": prompt,
39
+ "is_negative": is_negative,
40
+ "steps": steps,
41
+ "cfg_scale": cfg_scale,
42
+ "seed": seed if seed != -1 else random.randint(1, 1000000000),
43
+ "strength": strength,
44
+ "parameters": {
45
+ "width": width, # Pass the width to the API
46
+ "height": height # Pass the height to the API
47
+ }
48
  }
49
 
 
 
 
50
  # Send the request to the API and handle the response
51
  response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
52
  if response.status_code != 200:
 
110
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
111
 
112
  # Bind the button to the query function with the added width and height inputs
113
+ text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
 
 
 
 
 
114
 
115
  # Launch the Gradio app
116
+ app.launch(show_api=False, share=False)