xulh
commited on
Commit
·
01ff4cd
1
Parent(s):
5b6514f
代码初始化
Browse files- picture/__init__.py +0 -0
- picture/generalImg.py +31 -0
- requirements.txt +1 -0
picture/__init__.py
ADDED
File without changes
|
picture/generalImg.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from fastapi import APIRouter, HTTPException
|
3 |
+
from fastapi import Body
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
router = APIRouter()
|
8 |
+
|
9 |
+
|
10 |
+
@router.post("/generate-image/")
|
11 |
+
async def generate_image(token: str = Body(...), prompt: str = Body(...)):
|
12 |
+
try:
|
13 |
+
# 创建InferenceClient
|
14 |
+
client = InferenceClient("black-forest-labs/FLUX.1-dev", token=token)
|
15 |
+
|
16 |
+
# 使用text_to_image方法生成图片
|
17 |
+
image = client.text_to_image(prompt)
|
18 |
+
|
19 |
+
# 将PIL.Image对象转换为byte数据
|
20 |
+
buffered = BytesIO()
|
21 |
+
image.save(buffered, format="PNG")
|
22 |
+
image_bytes = buffered.getvalue()
|
23 |
+
|
24 |
+
# 将图片转换为base64编码
|
25 |
+
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
26 |
+
|
27 |
+
# 返回base64编码的图片
|
28 |
+
return {"image_base64": image_base64}
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
raise HTTPException(status_code=500, detail=f"Error generating image: {str(e)}")
|
requirements.txt
CHANGED
@@ -2,5 +2,6 @@ fastapi
|
|
2 |
httpx
|
3 |
requests
|
4 |
transformers
|
|
|
5 |
torch
|
6 |
uvicorn[standard]
|
|
|
2 |
httpx
|
3 |
requests
|
4 |
transformers
|
5 |
+
huggingface_hub
|
6 |
torch
|
7 |
uvicorn[standard]
|