kuanya commited on
Commit
012b86f
·
verified ·
1 Parent(s): e1c0815

Upload 4 files

Browse files
Files changed (4) hide show
  1. .dockerignore +14 -0
  2. Dockerfile +37 -0
  3. app.py +117 -0
  4. requirements.txt +8 -0
.dockerignore ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .Python
6
+ env
7
+ .venv
8
+ .env
9
+ .git
10
+ .gitignore
11
+ .cache
12
+ .coverage
13
+ htmlcov/
14
+ .pytest_cache/
Dockerfile ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 使用带有CUDA的基础镜像
2
+ FROM nvcr.io/nvidia/pytorch:23.10-py3
3
+
4
+ # 设置工作目录
5
+ WORKDIR /app
6
+
7
+ # 安装系统依赖
8
+ RUN apt-get update && apt-get install -y \
9
+ git \
10
+ libgl1-mesa-glx \
11
+ libglib2.0-0 \
12
+ && rm -rf /var/lib/apt/lists/*
13
+
14
+ # 复制项目文件
15
+ COPY requirements.txt .
16
+ COPY app.py .
17
+
18
+ # 安装Python依赖
19
+ RUN pip install --no-cache-dir -U pip && \
20
+ pip install --no-cache-dir -r requirements.txt
21
+
22
+ # 设置环境变量
23
+ ENV PYTHONUNBUFFERED=1 \
24
+ PYTHONPATH=/app \
25
+ HUGGINGFACE_HUB_CACHE=/app/cache \
26
+ HF_HOME=/app/cache \
27
+ HF_HUB_ENABLE_HF_TRANSFER=1 \
28
+ PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512
29
+
30
+ # 创建缓存目录
31
+ RUN mkdir -p /app/cache
32
+
33
+ # 暴露端口
34
+ EXPOSE 7860
35
+
36
+ # 启动命令
37
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from diffusers import DiffusionPipeline
4
+ import torch
5
+ from huggingface_hub import login
6
+
7
+ # 初始化 Hugging Face 登录
8
+ if os.getenv('HF_TOKEN'):
9
+ login(token=os.getenv('HF_TOKEN'))
10
+
11
+ # 模型加载函数
12
+ def load_model():
13
+ model_id = "IndexTeam/Index-anisora"
14
+ print("正在加载模型...")
15
+
16
+ pipe = DiffusionPipeline.from_pretrained(
17
+ model_id,
18
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
19
+ variant="fp16" if torch.cuda.is_available() else None,
20
+ use_safetensors=True
21
+ )
22
+
23
+ if torch.cuda.is_available():
24
+ print("使用CUDA加速")
25
+ pipe = pipe.to("cuda")
26
+ pipe.enable_attention_slicing()
27
+ if hasattr(pipe, 'enable_xformers_memory_efficient_attention'):
28
+ pipe.enable_xformers_memory_efficient_attention()
29
+ else:
30
+ print("警告:未检测到CUDA,使用CPU模式运行")
31
+
32
+ return pipe
33
+
34
+ # 加载模型
35
+ pipe = load_model()
36
+
37
+ # 生成图像函数
38
+ def generate_image(prompt, negative_prompt, steps=30, guidance_scale=7.5, seed=None):
39
+ if seed is not None and seed >= 0:
40
+ generator = torch.Generator("cuda" if torch.cuda.is_available() else "cpu").manual_seed(int(seed))
41
+ else:
42
+ generator = None
43
+
44
+ try:
45
+ with torch.inference_mode():
46
+ print(f"生成图像: {prompt[:50]}...")
47
+ image = pipe(
48
+ prompt=prompt,
49
+ negative_prompt=negative_prompt,
50
+ num_inference_steps=steps,
51
+ guidance_scale=guidance_scale,
52
+ generator=generator
53
+ ).images[0]
54
+ return image
55
+ except Exception as e:
56
+ print(f"生成失败: {str(e)}")
57
+ raise gr.Error(f"生成图像时出错: {str(e)}")
58
+
59
+ # 创建Gradio界面
60
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
+ gr.Markdown("""
62
+ # Index-anisora 演示
63
+ """)
64
+
65
+ with gr.Row():
66
+ with gr.Column():
67
+ prompt = gr.Textbox(
68
+ label="提示词",
69
+ value="1girl, beautiful anime style, detailed background",
70
+ placeholder="描述你想要生成的图像..."
71
+ )
72
+ negative_prompt = gr.Textbox(
73
+ label="负面提示词",
74
+ value="low quality, blurry, bad anatomy",
75
+ placeholder="描述你不希望在图像中出现的内容..."
76
+ )
77
+
78
+ with gr.Row():
79
+ steps = gr.Slider(
80
+ label="推理步数",
81
+ minimum=10,
82
+ maximum=50,
83
+ value=30,
84
+ step=1
85
+ )
86
+ guidance_scale = gr.Slider(
87
+ label="引导强度",
88
+ minimum=1.0,
89
+ maximum=20.0,
90
+ value=7.5,
91
+ step=0.5
92
+ )
93
+ seed = gr.Number(
94
+ label="随机种子",
95
+ value=-1,
96
+ precision=0
97
+ )
98
+
99
+ submit_btn = gr.Button("生成", variant="primary")
100
+
101
+ with gr.Column():
102
+ output_image = gr.Image(label="生成结果")
103
+
104
+ # 提交按钮事件
105
+ submit_btn.click(
106
+ fn=generate_image,
107
+ inputs=[prompt, negative_prompt, steps, guidance_scale, seed],
108
+ outputs=output_image
109
+ )
110
+
111
+ # 启动应用
112
+ if __name__ == "__main__":
113
+ demo.launch(
114
+ server_name="0.0.0.0",
115
+ server_port=7860,
116
+ share=False
117
+ )
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ torchvision
3
+ diffusers>=0.20.0
4
+ transformers
5
+ accelerate
6
+ xformers
7
+ gradio>=3.0.0
8
+ huggingface-hub