Python 版 Dall-E 3 绘画,暂时无上限。
## 接力大佬,@ 逆向达人,从 无【限虚假的dall-e-3绘画,可NewApi】----https://linux.do/t/topic/185085,我们继续讨论一波,很多佬友不会js代码,所以,我手搓了一版python代码,给各位佬耍一耍。废话不多说,直接上代码。
**python-web代码:**
```python
import aiohttp
import json
from datetime import datetime
from aiohttp import web
async def generate_image(prompt):
URL = "https://fluximg.com/api/image/generateImage"
DEFAULT_SIZE = "1:1"
size = DEFAULT_SIZE
if prompt:
splits = prompt.split('---')
size = splits if len(splits) > 1 and is_size_valid(splits) else DEFAULT_SIZE
json_body = {
"textStr": prompt or " ",
"model": "black-forest-labs/flux-schnell",
"size": size
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(URL, json=json_body,
headers={'Content-Type': 'application/json;charset=UTF-8'}) as response:
if response.status == 200:
url = await response.text()
dalle3_response = {
"created": int(datetime.now().timestamp()),
"data": [{"url": url}]
}
return json.dumps(dalle3_response)
except Exception as e:
pass# 处理错误 - 直接不处理,响应默认的数据
# 默认响应
dalle3_response = {
"created": int(datetime.now().timestamp()),
"data": [{"url": "https://pic.netbian.com/uploads/allimg/240808/192001-17231160015724.jpg"}]
}
return json.dumps(dalle3_response)
def is_size_valid(size):
return size in ["16:9", "1:1", "9:16", "3:2", "3:4", "1:2"]
async def handle_request(request):
if request.method != 'POST':
return web.Response(text='Only POST requests are allowed', status=405)
try:
json_data = await request.json()
except json.JSONDecodeError:
return web.Response(text='Invalid JSON', status=400)
prompt = json_data.get('prompt', None)
response = await generate_image(prompt)
return web.Response(text=response, content_type='application/json')
app = web.Application()
app.add_routes()
if __name__ == '__main__':
web.run_app(app)
```
**python-client代码:**
```python
import aiohttp
import asyncio
import json
async def request_image(prompt, model):
URL = "http://localhost:8080/"# 替换为实际服务器的 URL
json_body = {
"prompt": prompt,
"model": model
}
async with aiohttp.ClientSession() as session:
try:
async with session.post(URL, json=json_body, headers={'Content-Type': 'application/json'}) as response:
if response.status == 200:
result = await response.text()
print(f"Response: {result}")
else:
print(f"Failed to get image, status code: {response.status}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
prompt = "Draw a cute cat"# 生成猫咪的描述
model = "dalle3"# 模型选择为dalle3
asyncio.run(request_image(prompt, model))
```
**使用方法:**
* 先启动 `web_dalle3`
* 再启动 `web_client`
页:
[1]