marahmerah commited on
Commit
14037eb
·
verified ·
1 Parent(s): a6988bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -50
app.py CHANGED
@@ -4,8 +4,6 @@ import numpy as np
4
  import random
5
  from huggingface_hub import AsyncInferenceClient
6
  from translatepy import Translator
7
- import requests
8
- import re
9
  import asyncio
10
  from PIL import Image
11
  import io
@@ -36,25 +34,26 @@ def enable_lora(lora_add):
36
  return lora_add
37
 
38
  async def generate_image(
39
- prompt:str,
40
- model:str,
41
- lora_word:str,
42
- width:int=768,
43
- height:int=1024,
44
- scales:float=3.5,
45
- steps:int=24,
46
- seed:int=-1):
47
-
48
  if seed == -1:
49
  seed = random.randint(0, MAX_SEED)
50
  seed = int(seed)
51
- print(f'prompt:{prompt}')
52
-
53
  text = str(translator.translate(prompt, 'English')) + "," + lora_word
54
 
55
  client = AsyncInferenceClient()
56
  try:
57
- image = await client.text_to_image(
 
58
  prompt=text,
59
  height=height,
60
  width=width,
@@ -62,53 +61,52 @@ async def generate_image(
62
  num_inference_steps=steps,
63
  model=model,
64
  )
65
-
66
- # Convert WebP to PNG
67
- img = Image.open(io.BytesIO(image))
68
- png_image = io.BytesIO()
69
- img.save(png_image, format="PNG")
70
- png_image.seek(0)
71
-
72
- return png_image, seed
73
  except Exception as e:
74
  raise gr.Error(f"Error in {e}")
75
 
76
  async def gen(
77
- prompt:str,
78
- lora_add:str="",
79
- lora_word:str="",
80
- width:int=768,
81
- height:int=1024,
82
- scales:float=3.5,
83
- steps:int=24,
84
- seed:int=-1,
85
- progress=gr.Progress(track_tqdm=True)
86
  ):
87
  model = enable_lora(lora_add)
88
  print(model)
89
- image, seed = await generate_image(prompt,model,lora_word,width,height,scales,steps,seed)
90
  return image, seed
91
-
92
  examples = [
93
- ["a seal holding a beach ball in a pool","bingbangboom/flux_dreamscape","in the style of BSstyle004"],
94
- ["1980s anime screengrab, VHS quality, a woman with her face glitching and disorted, a halo above her head","dataautogpt3/FLUX-SyntheticAnime","1980s anime screengrab, VHS quality"],
95
- ["photograph, background of Earth from space, red car on the Moon watching Earth","martintomov/retrofuturism-flux","retrofuturism"],
96
- ["a living room interior","fofr/flux-80s-cyberpunk","80s cyberpunk"],
97
- ["Shrek, a lovable green ogre with a big smile, sitting on a moss-covered rock while enjoying a plate of freshly picked vegetables, in a magical forest filled with whimsical creatures, dappled sunlight filtering through the trees, surrounded by curious fairies peeking out from behind leaves","alvarobartt/ghibli-characters-flux-lora","Ghibli style"],
98
- ["a tourist in London, illustration in the style of VCTRNDRWNG, Victorian-era drawing","dvyio/flux-lora-victorian-drawing","illustration in the style of VCTRNDRWNG"],
99
- ["an African American and a caucasian man petting a cat at a busy electronic store. flikr photo from 2012. three people working in the background","kudzueye/boreal-flux-dev-v2","photo"],
100
- ["mgwr/cine, woman silhouette, morning light, sun rays, indoor scene, soft focus, golden hour, stretching pose, peaceful mood, cozy atmosphere, window light, shadows and highlights, backlit figure, minimalistic interior, warm tones, contemplative moment, calm energy, serene environment, yoga-inspired, elegant posture, natural light beams, artistic composition","mgwr/Cine-Aesthetic","atmospheric lighting and a dreamy, surreal vibe"]
101
  ]
102
 
103
  # Gradio Interface
104
-
105
  with gr.Blocks(css=CSS, js=JS, theme="ocean") as demo:
106
  gr.HTML("<h1><center>Flux Lab Light</center></h1>")
107
  gr.HTML("<p><center>Powered By HF Inference API</center></p>")
108
  with gr.Row():
109
  with gr.Column(scale=4):
110
  with gr.Row():
111
- img = gr.Image(type="filepath", label='flux Generated Image', height=600)
112
  with gr.Row():
113
  prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
114
  sendBtn = gr.Button(scale=1, variant='primary')
@@ -164,7 +162,7 @@ with gr.Blocks(css=CSS, js=JS, theme="ocean") as demo:
164
 
165
  gr.Examples(
166
  examples=examples,
167
- inputs=[prompt,lora_add,lora_word],
168
  outputs=[img, seed],
169
  fn=gen,
170
  cache_examples="lazy",
@@ -181,14 +179,14 @@ with gr.Blocks(css=CSS, js=JS, theme="ocean") as demo:
181
  prompt,
182
  lora_add,
183
  lora_word,
184
- width,
185
- height,
186
- scales,
187
- steps,
188
- seed
189
  ],
190
  outputs=[img, seed]
191
  )
192
-
193
  if __name__ == "__main__":
194
  demo.queue(api_open=False).launch(show_api=False, share=False)
 
4
  import random
5
  from huggingface_hub import AsyncInferenceClient
6
  from translatepy import Translator
 
 
7
  import asyncio
8
  from PIL import Image
9
  import io
 
34
  return lora_add
35
 
36
  async def generate_image(
37
+ prompt: str,
38
+ model: str,
39
+ lora_word: str,
40
+ width: int = 768,
41
+ height: int = 1024,
42
+ scales: float = 3.5,
43
+ steps: int = 24,
44
+ seed: int = -1,
45
+ ):
46
  if seed == -1:
47
  seed = random.randint(0, MAX_SEED)
48
  seed = int(seed)
49
+ print(f'prompt: {prompt}')
50
+
51
  text = str(translator.translate(prompt, 'English')) + "," + lora_word
52
 
53
  client = AsyncInferenceClient()
54
  try:
55
+ # Generate image using the Inference API
56
+ image_bytes = await client.text_to_image(
57
  prompt=text,
58
  height=height,
59
  width=width,
 
61
  num_inference_steps=steps,
62
  model=model,
63
  )
64
+
65
+ # Convert the image to PNG format
66
+ img = Image.open(io.BytesIO(image_bytes))
67
+ png_buffer = io.BytesIO()
68
+ img.save(png_buffer, format="PNG")
69
+ png_buffer.seek(0) # Reset buffer position to the beginning
70
+
71
+ return png_buffer, seed
72
  except Exception as e:
73
  raise gr.Error(f"Error in {e}")
74
 
75
  async def gen(
76
+ prompt: str,
77
+ lora_add: str = "",
78
+ lora_word: str = "",
79
+ width: int = 768,
80
+ height: int = 1024,
81
+ scales: float = 3.5,
82
+ steps: int = 24,
83
+ seed: int = -1,
84
+ progress=gr.Progress(track_tqdm=True),
85
  ):
86
  model = enable_lora(lora_add)
87
  print(model)
88
+ image, seed = await generate_image(prompt, model, lora_word, width, height, scales, steps, seed)
89
  return image, seed
90
+
91
  examples = [
92
+ ["a seal holding a beach ball in a pool", "bingbangboom/flux_dreamscape", "in the style of BSstyle004"],
93
+ ["1980s anime screengrab, VHS quality, a woman with her face glitching and distorted, a halo above her head", "dataautogpt3/FLUX-SyntheticAnime", "1980s anime screengrab, VHS quality"],
94
+ ["photograph, background of Earth from space, red car on the Moon watching Earth", "martintomov/retrofuturism-flux", "retrofuturism"],
95
+ ["a living room interior", "fofr/flux-80s-cyberpunk", "80s cyberpunk"],
96
+ ["Shrek, a lovable green ogre with a big smile, sitting on a moss-covered rock while enjoying a plate of freshly picked vegetables, in a magical forest filled with whimsical creatures, dappled sunlight filtering through the trees, surrounded by curious fairies peeking out from behind leaves", "alvarobartt/ghibli-characters-flux-lora", "Ghibli style"],
97
+ ["a tourist in London, illustration in the style of VCTRNDRWNG, Victorian-era drawing", "dvyio/flux-lora-victorian-drawing", "illustration in the style of VCTRNDRWNG"],
98
+ ["an African American and a Caucasian man petting a cat at a busy electronic store. Flickr photo from 2012. Three people working in the background", "kudzueye/boreal-flux-dev-v2", "photo"],
99
+ ["mgwr/cine, woman silhouette, morning light, sun rays, indoor scene, soft focus, golden hour, stretching pose, peaceful mood, cozy atmosphere, window light, shadows and highlights, backlit figure, minimalistic interior, warm tones, contemplative moment, calm energy, serene environment, yoga-inspired, elegant posture, natural light beams, artistic composition", "mgwr/Cine-Aesthetic", "atmospheric lighting and a dreamy, surreal vibe"]
100
  ]
101
 
102
  # Gradio Interface
 
103
  with gr.Blocks(css=CSS, js=JS, theme="ocean") as demo:
104
  gr.HTML("<h1><center>Flux Lab Light</center></h1>")
105
  gr.HTML("<p><center>Powered By HF Inference API</center></p>")
106
  with gr.Row():
107
  with gr.Column(scale=4):
108
  with gr.Row():
109
+ img = gr.Image(type="filepath", label='Flux Generated Image', height=600)
110
  with gr.Row():
111
  prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
112
  sendBtn = gr.Button(scale=1, variant='primary')
 
162
 
163
  gr.Examples(
164
  examples=examples,
165
+ inputs=[prompt, lora_add, lora_word],
166
  outputs=[img, seed],
167
  fn=gen,
168
  cache_examples="lazy",
 
179
  prompt,
180
  lora_add,
181
  lora_word,
182
+ width,
183
+ height,
184
+ scales,
185
+ steps,
186
+ seed,
187
  ],
188
  outputs=[img, seed]
189
  )
190
+
191
  if __name__ == "__main__":
192
  demo.queue(api_open=False).launch(show_api=False, share=False)