Files changed (1) hide show
  1. app.py +32 -17
app.py CHANGED
@@ -2,25 +2,37 @@ import gradio as gr
2
  import random
3
  import requests
4
  import io
5
- from PIL import Image
6
  import os
7
 
8
-
9
  API_URL = "https://api-inference.huggingface.co/models/LucyintheSky/lucy-dream-lora"
10
  headers = {"Authorization": "Bearer " + os.environ.get('TOKEN')}
11
 
12
  def query(payload):
13
- response = requests.post(API_URL, headers=headers, json=payload)
14
- return response.content
 
 
 
 
15
 
16
  def generate(prompt):
17
  image_bytes = query({
18
- "inputs": "lucy dream style " + prompt + ", perfect body, beautiful face, perfect skin",
19
- "parameters" : { "negative_prompt": "ugly, deformed, deformed face, ugly face, bad quality",
20
- "seed": random.randint(0,9999999)}
21
  })
22
- image = Image.open(io.BytesIO(image_bytes))
23
- return image
 
 
 
 
 
 
 
 
 
24
 
25
  theme = gr.themes.Base(
26
  primary_hue="gray",
@@ -38,17 +50,20 @@ with gr.Blocks(theme=theme) as demo:
38
  <center> <img src="https://cdn-uploads.huggingface.co/production/uploads/650a1281b48ff3906647edd0/rbzPKW_p5XTG9leEO4uFt.png" > </center>
39
  <br><br><br>
40
  """)
41
- img = gr.Image(show_label=False, type='pil')
42
- textbox = gr.Textbox(show_label=False, placeholder='type your prompt in here')
43
- button = gr.Button("generate", variant="primary")
44
 
45
  gr.Examples(
46
- [["Emma Stone on prom night, mint green satin maxi dress"], ["Lady Gaga, 50s hair style, prom night"], ["Rihanna wearing a silver prom dress with crystal jewelry"]],
47
- textbox,
48
- img,
49
- generate,
50
- cache_examples=True,
 
 
51
  )
 
52
 
53
  gr.Markdown("""
54
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/650a1281b48ff3906647edd0/OLPLii-mE6VhIiabvJwOm.png)
 
2
  import random
3
  import requests
4
  import io
5
+ from PIL import Image, UnidentifiedImageError
6
  import os
7
 
 
8
  API_URL = "https://api-inference.huggingface.co/models/LucyintheSky/lucy-dream-lora"
9
  headers = {"Authorization": "Bearer " + os.environ.get('TOKEN')}
10
 
11
  def query(payload):
12
+ response = requests.post(API_URL, headers=headers, json=payload)
13
+ if response.ok:
14
+ return response.content
15
+ else:
16
+ print("API Error:", response.status_code, response.text)
17
+ return None
18
 
19
  def generate(prompt):
20
  image_bytes = query({
21
+ "inputs": f"lucy dream style {prompt}, perfect body, beautiful face, perfect skin",
22
+ "parameters": {"negative_prompt": "ugly, deformed, deformed face, ugly face, bad quality",
23
+ "seed": random.randint(0, 9999999)}
24
  })
25
+
26
+ if image_bytes is None:
27
+ return "Error: Could not generate image. Please try again."
28
+
29
+ try:
30
+ image = Image.open(io.BytesIO(image_bytes))
31
+ image_path = "temp_output.png"
32
+ image.save(image_path)
33
+ return image_path
34
+ except UnidentifiedImageError:
35
+ return "Error: Invalid image data received."
36
 
37
  theme = gr.themes.Base(
38
  primary_hue="gray",
 
50
  <center> <img src="https://cdn-uploads.huggingface.co/production/uploads/650a1281b48ff3906647edd0/rbzPKW_p5XTG9leEO4uFt.png" > </center>
51
  <br><br><br>
52
  """)
53
+ img = gr.Image(show_label=False, type="filepath")
54
+ textbox = gr.Textbox(show_label=False, placeholder='Type your prompt here')
55
+ button = gr.Button("Generate", variant="primary")
56
 
57
  gr.Examples(
58
+ [["Emma Stone on prom night, mint green satin maxi dress"],
59
+ ["Lady Gaga, 50s hair style, prom night"],
60
+ ["Rihanna wearing a silver prom dress with crystal jewelry"]],
61
+ inputs=textbox,
62
+ outputs=img,
63
+ fn=generate,
64
+ cache_examples=False # キャッシγƒ₯ζ©Ÿθƒ½γ‚’γ‚ͺフにする
65
  )
66
+
67
 
68
  gr.Markdown("""
69
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/650a1281b48ff3906647edd0/OLPLii-mE6VhIiabvJwOm.png)