shichen commited on
Commit
12de40d
1 Parent(s): 1d17a15
Files changed (2) hide show
  1. app.py +11 -3
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import requests
2
  import gradio as gr
 
 
3
 
4
- # 修改 greet 函数以处理图像生成
5
  def generate_image(api_key, prompt, aspect_ratio, model, negative_prompt, seed):
6
  response = requests.post(
7
  "https://api.stability.ai/v2beta/stable-image/generate/sd3",
@@ -21,7 +23,13 @@ def generate_image(api_key, prompt, aspect_ratio, model, negative_prompt, seed):
21
  )
22
 
23
  if response.status_code == 200:
24
- return response.content
 
 
 
 
 
 
25
  else:
26
  raise Exception(str(response.json()))
27
 
@@ -43,4 +51,4 @@ with gr.Blocks() as demo:
43
  inputs=[api_key, prompt, aspect_ratio, model, negative_prompt, seed],
44
  outputs=output)
45
 
46
- demo.launch(server_port=7588)
 
1
  import requests
2
  import gradio as gr
3
+ from PIL import Image
4
+ import io
5
 
6
+ # 修改 generate_image 函数以处理图像生成并保存为临时文件
7
  def generate_image(api_key, prompt, aspect_ratio, model, negative_prompt, seed):
8
  response = requests.post(
9
  "https://api.stability.ai/v2beta/stable-image/generate/sd3",
 
23
  )
24
 
25
  if response.status_code == 200:
26
+ # 将 bytes 数据转换为 PIL Image 对象
27
+ image_bytes = io.BytesIO(response.content)
28
+ image = Image.open(image_bytes)
29
+ # 保存为临时文件
30
+ temp_path = "./temp_image.jpeg"
31
+ image.save(temp_path)
32
+ return temp_path
33
  else:
34
  raise Exception(str(response.json()))
35
 
 
51
  inputs=[api_key, prompt, aspect_ratio, model, negative_prompt, seed],
52
  outputs=output)
53
 
54
+ demo.launch(server_port=7855)
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio
2
- requests
 
 
1
  gradio
2
+ requests
3
+ pillow