一元网络论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 52|回复: 0

Python 版 Dall-E 3 绘画,暂时无上限。

[复制链接]

1万

主题

1万

帖子

5万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
54630
发表于 2024-8-25 13:16:33 | 显示全部楼层 |阅读模式
## 接力大佬,@ 逆向达人,从 无【限虚假的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[1] if len(splits) > 1 and is_size_valid(splits[1]) 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([web.post('/', handle_request)])
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`
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|一元网络论坛

GMT+8, 2024-9-17 15:49 , Processed in 0.056241 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表