K00B404 commited on
Commit
370a105
1 Parent(s): 78c6c29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -3,33 +3,38 @@ import requests
3
  import io
4
  import random
5
  import os
6
- import time
7
  from PIL import Image
8
  from deep_translator import GoogleTranslator
9
- import json
10
 
11
- # Project by Nymbo
12
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
13
- #API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
14
- API_TOKEN = os.getenv("HF_READ_TOKEN")
15
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
  timeout = 100
17
 
18
- def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  if prompt == "" or prompt is None:
20
  return None
21
 
22
  key = random.randint(0, 999)
23
-
24
- API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
25
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
26
-
27
  prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
28
  print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
29
 
30
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
31
  print(f'\033[1mGeneration {key}:\033[0m {prompt}')
32
-
33
  # If seed is -1, generate a random seed and use it
34
  if seed == -1:
35
  seed = random.randint(1, 1000000000)
@@ -55,11 +60,15 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
55
  image_bytes = response.content
56
  image = Image.open(io.BytesIO(image_bytes))
57
  print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
58
- # Return both the image and the seed used
59
- return image, seed
 
 
 
 
60
  except Exception as e:
61
  print(f"Error when trying to open the image: {e}")
62
- return None
63
 
64
  css = """
65
  #app-container {
@@ -84,12 +93,16 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
84
  method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
85
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
86
  seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
 
87
 
88
  with gr.Row():
89
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
90
  with gr.Row():
91
- image_output,_ = gr.Image(type="pil", label="Image Output", elem_id="gallery")
 
 
92
 
93
- text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength], outputs=image_output)
 
94
 
95
  app.launch(show_api=True, share=False)
 
3
  import io
4
  import random
5
  import os
 
6
  from PIL import Image
7
  from deep_translator import GoogleTranslator
 
8
 
 
9
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
 
 
 
10
  timeout = 100
11
 
12
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, huggingface_api_key=None):
13
+ # Check if the request is an API call by checking for the presence of the huggingface_api_key
14
+ is_api_call = huggingface_api_key is not None
15
+
16
+ if is_api_call:
17
+ # Validate the API key if it's an API call
18
+ if huggingface_api_key == "":
19
+ raise gr.Error("API key is required for API calls.")
20
+
21
+ headers = {"Authorization": f"Bearer {huggingface_api_key}"}
22
+ else:
23
+ # Use the environment variable for the API key in GUI mode
24
+ API_TOKEN = os.getenv("HF_READ_TOKEN")
25
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
26
+
27
  if prompt == "" or prompt is None:
28
  return None
29
 
30
  key = random.randint(0, 999)
31
+
 
 
 
32
  prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
33
  print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
34
 
35
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
36
  print(f'\033[1mGeneration {key}:\033[0m {prompt}')
37
+
38
  # If seed is -1, generate a random seed and use it
39
  if seed == -1:
40
  seed = random.randint(1, 1000000000)
 
60
  image_bytes = response.content
61
  image = Image.open(io.BytesIO(image_bytes))
62
  print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
63
+
64
+ # Save the image to a file and return the file path and seed
65
+ output_path = f"./output_{key}.png"
66
+ image.save(output_path)
67
+
68
+ return output_path, seed
69
  except Exception as e:
70
  print(f"Error when trying to open the image: {e}")
71
+ return None, None
72
 
73
  css = """
74
  #app-container {
 
93
  method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
94
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
95
  seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
96
+ huggingface_api_key = gr.Textbox(label="Hugging Face API Key (required for API calls)", placeholder="Enter your Hugging Face API Key here", type="password", elem_id="api-key")
97
 
98
  with gr.Row():
99
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
100
  with gr.Row():
101
+ # Define two outputs: one for the image file path and one for the seed
102
+ image_output = gr.Textbox(label="Image File Path", elem_id="gallery")
103
+ seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
104
 
105
+ # Adjust the click function to include the API key as an input
106
+ text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key], outputs=[image_output, seed_output])
107
 
108
  app.launch(show_api=True, share=False)