ACCA225 commited on
Commit
5970775
1 Parent(s): 4271bb9

Upload 2 files

Browse files
Files changed (2) hide show
  1. SD3.py +62 -0
  2. requirements.txt +3 -0
SD3.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ from requests_toolbelt.multipart.encoder import MultipartEncoder
4
+ import gradio as gr
5
+
6
+ def generate_image(prompt, negative_prompt=None, api_key=None, aspect_ratio='1:1', model='sd3', seed=0, output_format='png'):
7
+ api_url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
8
+ #api_key = os.environ.get("STABILITY_AI_API_KEY") # Retrieve the API key from the environment variable
9
+
10
+ fields = {
11
+ 'prompt': prompt,
12
+ 'aspect_ratio': aspect_ratio,
13
+ 'model': model,
14
+ 'seed': str(seed),
15
+ 'output_format': output_format,
16
+ 'mode': 'text-to-image' # Default mode
17
+ }
18
+
19
+ if negative_prompt:
20
+ fields['negative_prompt'] = negative_prompt
21
+
22
+ m = MultipartEncoder(fields=fields)
23
+
24
+ headers = {
25
+ 'Authorization': f'Bearer {api_key}',
26
+ 'Content-Type': m.content_type,
27
+ 'Accept': 'image/*' # To receive the image directly
28
+ }
29
+
30
+ response = requests.post(api_url, data=m, headers=headers)
31
+ print(response.status_code, response.content)
32
+
33
+ if response.status_code == 200:
34
+ # Assuming the response content is the image in binary format
35
+ output_path = 'generated_image.png'
36
+ with open(output_path, 'wb') as f:
37
+ f.write(response.content)
38
+ return output_path # Return the path for Gradio to display the image
39
+ else:
40
+ return f"Error: {response.text}"
41
+
42
+ def wrap_generate_image(prompt, negative_prompt, api_key, model, aspect_ratio):
43
+ return generate_image(prompt, negative_prompt, api_key, aspect_ratio, model)
44
+
45
+ iface = gr.Interface(
46
+ fn=wrap_generate_image,
47
+ inputs=[
48
+ gr.Textbox(lines=2, label="提示词", placeholder="请输入提示词,例如: 1girl, epic game, miku, city..."),
49
+ gr.Textbox(lines=2, label="负面提示词", placeholder="(worst quality, low quality:1.4)..."),
50
+ gr.Textbox(lines=2, label="API_key", placeholder="填写你的API_KEY..."),
51
+ gr.Radio(choices=['sd3', 'sd3-turbo'], label="SD3模型种类", value='sd3'),
52
+ gr.Dropdown(choices=['1:1', '16:9', '21:9', '2:3', '3:2', '4:5', '5:4', '9:16', '9:21'], label="图片比率", value='1:1')
53
+ ],
54
+ outputs=gr.Image(),
55
+ title="Stable Diffusion 3 在线使用(API)",
56
+ description='''BY BiliBili NYAN9,官网API获取:https://platform.stability.ai/account/keys 。无限账号注册:https://www.snapmail.cc/
57
+ 每个账号送25积分,可以免费画五张图左右。后续我会想办法弄个批量注册机。
58
+ 支持一下本人吧:https://afdian.net/a/KaggleSD , 不赞助也没事也可以点进去看看我写过其它的SD项目
59
+ '''
60
+ )
61
+
62
+ iface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ requests
2
+ requests-toolbelt
3
+ gradio