Upload 3 files
Browse files- README.md +3 -13
- app.py +68 -0
- requirements.txt +9 -0
README.md
CHANGED
|
@@ -1,14 +1,4 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Yu
|
| 3 |
-
emoji: 🐢
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: pink
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.31.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: apache-2.0
|
| 11 |
-
short_description: sheng
|
| 12 |
-
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
# AI 图文生成器:支持上传CSV批量生成图文,也支持文本转图片
|
| 3 |
+
|
| 4 |
+
本项目使用 SDXL 模型,生成高清图像,支持上传提示词批量生成,也支持输入提示词生成单图。
|
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import torch
|
| 5 |
+
from diffusers import StableDiffusionXLPipeline
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
+
import zipfile
|
| 9 |
+
|
| 10 |
+
# 加载模型(GPU环境)
|
| 11 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 12 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 13 |
+
torch_dtype=torch.float16,
|
| 14 |
+
variant="fp16"
|
| 15 |
+
).to("cuda")
|
| 16 |
+
|
| 17 |
+
# 文本模板生成函数
|
| 18 |
+
def generate_text(prompt):
|
| 19 |
+
return f"这是一幅描绘:{prompt} 的画面,小朋友可以根据图像发挥想象力哦!"
|
| 20 |
+
|
| 21 |
+
# 模式一:处理CSV并生成图文ZIP
|
| 22 |
+
def process_csv(file):
|
| 23 |
+
df = pd.read_csv(file.name)
|
| 24 |
+
output_dir = "output"
|
| 25 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
for idx, row in df.iterrows():
|
| 28 |
+
prompt = row["prompt"]
|
| 29 |
+
image = pipe(prompt=prompt).images[0]
|
| 30 |
+
image_path = os.path.join(output_dir, f"image_{idx+1}.png")
|
| 31 |
+
image.save(image_path)
|
| 32 |
+
|
| 33 |
+
text = generate_text(prompt)
|
| 34 |
+
text_path = os.path.join(output_dir, f"text_{idx+1}.txt")
|
| 35 |
+
with open(text_path, "w") as f:
|
| 36 |
+
f.write(text)
|
| 37 |
+
|
| 38 |
+
# 打包ZIP
|
| 39 |
+
zip_path = "output.zip"
|
| 40 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 41 |
+
for file_name in os.listdir(output_dir):
|
| 42 |
+
file_path = os.path.join(output_dir, file_name)
|
| 43 |
+
zipf.write(file_path, arcname=file_name)
|
| 44 |
+
return zip_path
|
| 45 |
+
|
| 46 |
+
# 模式二:文本转图片
|
| 47 |
+
def text_to_image(prompt):
|
| 48 |
+
image = pipe(prompt=prompt).images[0]
|
| 49 |
+
return image
|
| 50 |
+
|
| 51 |
+
# Gradio界面
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("## 🤖 AI 图文生成器 - 批量 & 单图模式")
|
| 54 |
+
|
| 55 |
+
with gr.Tab("📂 批量生成(上传CSV)"):
|
| 56 |
+
csv_input = gr.File(label="上传CSV文件", file_types=[".csv"])
|
| 57 |
+
csv_output = gr.File(label="生成图文ZIP包")
|
| 58 |
+
csv_btn = gr.Button("开始生成")
|
| 59 |
+
csv_btn.click(fn=process_csv, inputs=csv_input, outputs=csv_output)
|
| 60 |
+
|
| 61 |
+
with gr.Tab("🖼️ 单图生成(文本转图片)"):
|
| 62 |
+
prompt_input = gr.Textbox(label="输入提示词", placeholder="比如:一只飞翔在太空的小猫咪")
|
| 63 |
+
image_output = gr.Image(label="生成的图像", type="pil")
|
| 64 |
+
single_btn = gr.Button("立即生成")
|
| 65 |
+
single_btn.click(fn=text_to_image, inputs=prompt_input, outputs=image_output)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio
|
| 3 |
+
pandas
|
| 4 |
+
torch==2.1.0
|
| 5 |
+
diffusers==0.27.2
|
| 6 |
+
transformers==4.40.0
|
| 7 |
+
accelerate==0.28.0
|
| 8 |
+
safetensors
|
| 9 |
+
pillow
|