Peiiiiiiiiru commited on
Commit
ab35390
1 Parent(s): a14225c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -78
app.py CHANGED
@@ -4,92 +4,17 @@ import io
4
  from PIL import Image
5
  import os
6
 
7
- # Stable Diffusion XL API 設定
8
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
9
- import gradio as gr
10
- import requests
11
- import io
12
- from PIL import Image
13
- import os
14
-
15
- # Stable Diffusion XL API 設定
16
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
17
- import gradio as gr
18
- import requests
19
- import io
20
- from PIL import Image
21
- import os
22
-
23
  # Stable Diffusion XL API 設定
24
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
25
  headers = {"Authorization": "Bearer hf_MySpaceToken"}
26
 
27
- def generate_image(prompt):
28
- # 呼叫 API 生成圖片
29
- response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
30
-
31
- # 將回應轉換為圖片
32
- image = Image.open(io.BytesIO(response.content))
33
-
34
- return image
35
-
36
- # 創建 Gradio 介面
37
- demo = gr.Interface(
38
- fn=generate_image,
39
- inputs=gr.Textbox(
40
- label="輸入圖片描述",
41
- placeholder="例如: a beautiful sunset over mountains, realistic photo"
42
- ),
43
- outputs=gr.Image(label="生成的圖片"),
44
- title="Stable Diffusion XL 圖像生成器",
45
- description="使用 Stable Diffusion XL 模型生成高品質圖像",
46
- examples=[
47
- ["a beautiful sunset over mountains, realistic photo"],
48
- ["a cute cat playing with yarn, digital art"],
49
- ["a futuristic city at night with neon lights"],
50
- ["a magical forest with glowing mushrooms, fantasy art"]
51
- ]
52
- )
53
-
54
- demo.launch()
55
-
56
- def generate_image(prompt):
57
- # 呼叫 API 生成圖片
58
- response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
59
-
60
- # 將回應轉換為圖片
61
- image = Image.open(io.BytesIO(response.content))
62
-
63
- return image
64
-
65
- # 創建 Gradio 介面
66
- demo = gr.Interface(
67
- fn=generate_image,
68
- inputs=gr.Textbox(
69
- label="輸入圖片描述",
70
- placeholder="例如: a beautiful sunset over mountains, realistic photo"
71
- ),
72
- outputs=gr.Image(label="生成的圖片"),
73
- title="Stable Diffusion XL 圖像生成器",
74
- description="使用 Stable Diffusion XL 模型生成高品質圖像",
75
- examples=[
76
- ["a beautiful sunset over mountains, realistic photo"],
77
- ["a cute cat playing with yarn, digital art"],
78
- ["a futuristic city at night with neon lights"],
79
- ["a magical forest with glowing mushrooms, fantasy art"]
80
- ]
81
- )
82
-
83
- demo.launch()
84
 
85
  def generate_image(prompt):
86
  # 呼叫 API 生成圖片
87
  response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
88
 
89
- # 將回應轉換為圖片
90
- image = Image.open(io.BytesIO(response.content))
91
-
92
- return image
93
 
94
  # 創建 Gradio 介面
95
  demo = gr.Interface(
@@ -98,7 +23,7 @@ demo = gr.Interface(
98
  label="輸入圖片描述",
99
  placeholder="例如: a beautiful sunset over mountains, realistic photo"
100
  ),
101
- outputs=gr.Image(label="生成的圖片"),
102
  title="Stable Diffusion XL 圖像生成器",
103
  description="使用 Stable Diffusion XL 模型生成高品質圖像",
104
  examples=[
 
4
  from PIL import Image
5
  import os
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # Stable Diffusion XL API 設定
8
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
9
  headers = {"Authorization": "Bearer hf_MySpaceToken"}
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def generate_image(prompt):
13
  # 呼叫 API 生成圖片
14
  response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
15
 
16
+ # 回傳圖片
17
+ return response.content
 
 
18
 
19
  # 創建 Gradio 介面
20
  demo = gr.Interface(
 
23
  label="輸入圖片描述",
24
  placeholder="例如: a beautiful sunset over mountains, realistic photo"
25
  ),
26
+ outputs=gr.Image(type="bytes"),
27
  title="Stable Diffusion XL 圖像生成器",
28
  description="使用 Stable Diffusion XL 模型生成高品質圖像",
29
  examples=[