Spaces:
Running
Running
Commit
·
5f71263
1
Parent(s):
4564834
替换AI模型为规则生成器,解决依赖问题
Browse files- app.py +127 -82
- requirements.txt +4 -2
app.py
CHANGED
@@ -59,57 +59,133 @@ def create_dummy_image():
|
|
59 |
img = PILImage.new('RGB', (256, 256), color = (255, 100, 100))
|
60 |
return img
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
try:
|
68 |
-
import
|
69 |
-
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
except Exception as e:
|
106 |
-
logger.error(f"
|
107 |
-
|
|
|
108 |
|
109 |
# 生成图像函数
|
110 |
def generate_image(prompt):
|
111 |
-
global pipe
|
112 |
-
|
113 |
# 如果提示为空,使用默认提示
|
114 |
if not prompt or prompt.strip() == "":
|
115 |
prompt = "a beautiful landscape"
|
@@ -117,53 +193,22 @@ def generate_image(prompt):
|
|
117 |
|
118 |
logger.info(f"收到提示词: {prompt}")
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
-
|
123 |
-
if pipe is None:
|
124 |
-
logger.error("模型加载失败,返回默认图像")
|
125 |
-
return create_dummy_image()
|
126 |
-
|
127 |
-
try:
|
128 |
-
# 优化生成参数,减少内存需求
|
129 |
-
logger.info("开始生成图像...")
|
130 |
-
|
131 |
-
# 设置随机种子以确保结果一致性
|
132 |
-
seed = random.randint(0, 2147483647)
|
133 |
-
generator = torch.Generator(device=pipe.device).manual_seed(seed)
|
134 |
-
|
135 |
-
# 使用最轻量级的参数
|
136 |
-
image = pipe(
|
137 |
-
prompt=prompt,
|
138 |
-
num_inference_steps=3, # 极少的步骤
|
139 |
-
guidance_scale=7.5,
|
140 |
-
height=256, # 小尺寸
|
141 |
-
width=256, # 小尺寸
|
142 |
-
generator=generator
|
143 |
-
).images[0]
|
144 |
-
|
145 |
-
# 释放缓存,避免内存增长
|
146 |
-
if torch.cuda.is_available():
|
147 |
-
torch.cuda.empty_cache()
|
148 |
-
|
149 |
-
logger.info(f"图像生成成功,种子: {seed}")
|
150 |
-
return image
|
151 |
-
except Exception as e:
|
152 |
-
logger.error(f"生成过程发生错误: {e}")
|
153 |
-
return create_dummy_image()
|
154 |
|
155 |
# 创建Gradio界面
|
156 |
def create_demo():
|
157 |
-
#
|
158 |
demo = gr.Interface(
|
159 |
fn=generate_image,
|
160 |
-
inputs=gr.Textbox(label="
|
161 |
outputs=gr.Image(type="pil", label="生成的图像"),
|
162 |
-
title="
|
163 |
-
description="
|
164 |
-
examples=["a cute cat", "mountain landscape"],
|
165 |
cache_examples=False,
|
166 |
-
|
167 |
)
|
168 |
return demo
|
169 |
|
|
|
59 |
img = PILImage.new('RGB', (256, 256), color = (255, 100, 100))
|
60 |
return img
|
61 |
|
62 |
+
# 创建一个模拟图像生成器
|
63 |
+
def create_mock_image(prompt):
|
64 |
+
"""当模型加载不成功时,创建一个带有提示词的简单图像"""
|
65 |
+
logger.info(f"创建简单图像: {prompt}")
|
66 |
+
|
67 |
+
# 创建一个基础图像
|
68 |
+
img = PILImage.new('RGB', (512, 512), color=(240, 240, 250))
|
69 |
+
|
70 |
+
# 在图像上写文字
|
71 |
try:
|
72 |
+
from PIL import ImageDraw, ImageFont
|
73 |
+
draw = ImageDraw.Draw(img)
|
74 |
|
75 |
+
# 尝试找一个合适的字体
|
76 |
+
try:
|
77 |
+
font = ImageFont.truetype("arial.ttf", 20)
|
78 |
+
except:
|
79 |
+
try:
|
80 |
+
font = ImageFont.truetype("DejaVuSans.ttf", 20)
|
81 |
+
except:
|
82 |
+
font = ImageFont.load_default()
|
83 |
|
84 |
+
# 添加提示文本
|
85 |
+
text = f"提示词: {prompt}"
|
86 |
+
draw.text((20, 20), text, fill=(0, 0, 0), font=font)
|
87 |
+
draw.text((20, 60), "模型加载失败,显示占位图像", fill=(255, 0, 0), font=font)
|
88 |
|
89 |
+
except Exception as e:
|
90 |
+
logger.error(f"创建文字图像失败: {e}")
|
91 |
+
|
92 |
+
return img
|
93 |
+
|
94 |
+
# 使用简单的文本生成器
|
95 |
+
def text_to_image(prompt):
|
96 |
+
"""一个非常简单的基于规则的文本到图像生成器"""
|
97 |
+
logger.info(f"使用简单规则生成图像: {prompt}")
|
98 |
+
|
99 |
+
# 创建基础图像
|
100 |
+
img = PILImage.new('RGB', (512, 512), color=(240, 240, 250))
|
101 |
+
|
102 |
+
# 尝试分析提示词内容
|
103 |
+
color = (100, 100, 200) # 默认蓝色
|
104 |
+
|
105 |
+
# 简单的颜色匹配
|
106 |
+
color_words = {
|
107 |
+
'red': (200, 50, 50),
|
108 |
+
'blue': (50, 50, 200),
|
109 |
+
'green': (50, 200, 50),
|
110 |
+
'yellow': (200, 200, 50),
|
111 |
+
'purple': (150, 50, 150),
|
112 |
+
'orange': (220, 140, 20),
|
113 |
+
'pink': (255, 150, 200),
|
114 |
+
'black': (30, 30, 30),
|
115 |
+
'white': (240, 240, 240),
|
116 |
+
'gray': (128, 128, 128),
|
117 |
+
}
|
118 |
+
|
119 |
+
# 检查提示词中是否包含颜色
|
120 |
+
prompt_lower = prompt.lower()
|
121 |
+
for color_word, rgb in color_words.items():
|
122 |
+
if color_word in prompt_lower:
|
123 |
+
color = rgb
|
124 |
+
break
|
125 |
+
|
126 |
+
# 创建一个简单的图形
|
127 |
+
from PIL import ImageDraw
|
128 |
+
draw = ImageDraw.Draw(img)
|
129 |
+
|
130 |
+
# 根据提示词选择不同的绘制方式
|
131 |
+
if any(animal in prompt_lower for animal in ['cat', 'kitty', 'kitten']):
|
132 |
+
# 画一个简单的猫
|
133 |
+
draw.ellipse((156, 156, 356, 306), fill=color) # 头
|
134 |
+
draw.ellipse((196, 176, 246, 226), fill=(255, 255, 255)) # 左眼
|
135 |
+
draw.ellipse((266, 176, 316, 226), fill=(255, 255, 255)) # 右眼
|
136 |
+
draw.ellipse((211, 191, 231, 211), fill=(0, 0, 0)) # 左眼球
|
137 |
+
draw.ellipse((281, 191, 301, 211), fill=(0, 0, 0)) # 右眼球
|
138 |
+
draw.polygon([(256, 256), (236, 246), (276, 246)], fill=(255, 100, 150)) # 鼻子
|
139 |
+
draw.line([(256, 256), (256, 286)], fill=(0, 0, 0), width=2) # 鼻线
|
140 |
+
draw.arc((206, 256, 306, 336), 0, 180, fill=(0, 0, 0), width=2) # 嘴
|
141 |
+
# 猫耳朵
|
142 |
+
draw.polygon([(206, 156), (156, 76), (246, 126)], fill=color)
|
143 |
+
draw.polygon([(306, 156), (356, 76), (266, 126)], fill=color)
|
144 |
+
|
145 |
+
elif any(landscape in prompt_lower for landscape in ['landscape', 'mountain', 'sunset', 'nature']):
|
146 |
+
# 画一个简单的风景
|
147 |
+
# 天空
|
148 |
+
sky_color = (100, 150, 250)
|
149 |
+
if 'sunset' in prompt_lower:
|
150 |
+
sky_color = (250, 150, 100)
|
151 |
+
draw.rectangle([(0, 0), (512, 300)], fill=sky_color)
|
152 |
|
153 |
+
# 太阳/月亮
|
154 |
+
if 'sunset' in prompt_lower or 'sun' in prompt_lower:
|
155 |
+
draw.ellipse((400, 50, 480, 130), fill=(255, 200, 50))
|
156 |
+
elif 'night' in prompt_lower or 'moon' in prompt_lower:
|
157 |
+
draw.ellipse((400, 50, 480, 130), fill=(240, 240, 240))
|
158 |
+
|
159 |
+
# 山
|
160 |
+
draw.polygon([(0, 300), (200, 100), (400, 300)], fill=(100, 100, 100))
|
161 |
+
draw.polygon([(100, 300), (300, 50), (500, 300)], fill=(80, 80, 80))
|
162 |
+
|
163 |
+
# 地面
|
164 |
+
ground_color = (100, 200, 100)
|
165 |
+
if 'desert' in prompt_lower:
|
166 |
+
ground_color = (240, 220, 180)
|
167 |
+
elif 'snow' in prompt_lower or 'winter' in prompt_lower:
|
168 |
+
ground_color = (240, 240, 250)
|
169 |
+
draw.rectangle([(0, 300), (512, 512)], fill=ground_color)
|
170 |
+
|
171 |
+
else:
|
172 |
+
# 默认绘制一些简单的几何图形
|
173 |
+
draw.rectangle([(106, 106), (406, 406)], outline=(0, 0, 0), width=2)
|
174 |
+
draw.ellipse((156, 156, 356, 356), fill=color)
|
175 |
+
draw.polygon([(256, 106), (406, 406), (106, 406)], fill=(color[0]//2, color[1]//2, color[2]//2))
|
176 |
+
|
177 |
+
# 添加提示词文本
|
178 |
+
try:
|
179 |
+
font = ImageFont.load_default()
|
180 |
+
draw.text((10, 10), f"提示词: {prompt}", fill=(0, 0, 0), font=font)
|
181 |
+
draw.text((10, 30), "由简单规则生成", fill=(100, 100, 100), font=font)
|
182 |
except Exception as e:
|
183 |
+
logger.error(f"添加文字失败: {e}")
|
184 |
+
|
185 |
+
return img
|
186 |
|
187 |
# 生成图像函数
|
188 |
def generate_image(prompt):
|
|
|
|
|
189 |
# 如果提示为空,使用默认提示
|
190 |
if not prompt or prompt.strip() == "":
|
191 |
prompt = "a beautiful landscape"
|
|
|
193 |
|
194 |
logger.info(f"收到提示词: {prompt}")
|
195 |
|
196 |
+
# 不再尝试加载AI模型,直接使用规则生成器
|
197 |
+
logger.info("使用规则生成器代替AI模型")
|
198 |
+
return text_to_image(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
# 创建Gradio界面
|
201 |
def create_demo():
|
202 |
+
# 创建界面
|
203 |
demo = gr.Interface(
|
204 |
fn=generate_image,
|
205 |
+
inputs=gr.Textbox(label="输入提示词(例如:猫、风景、日落)"),
|
206 |
outputs=gr.Image(type="pil", label="生成的图像"),
|
207 |
+
title="简易文本到图像生成器",
|
208 |
+
description="输入文本描述,生成相应的图像(使用规则生成器,不依赖AI模型)",
|
209 |
+
examples=["a cute cat", "beautiful sunset", "mountain landscape", "red circle"],
|
210 |
cache_examples=False,
|
211 |
+
flagging_mode=None
|
212 |
)
|
213 |
return demo
|
214 |
|
requirements.txt
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
accelerate==0.21.0
|
2 |
-
diffusers==0.
|
|
|
3 |
torch==2.0.1
|
4 |
-
transformers==4.
|
|
|
5 |
gradio==3.24.1
|
6 |
Pillow==10.0.0
|
|
|
1 |
accelerate==0.21.0
|
2 |
+
diffusers==0.18.0
|
3 |
+
huggingface-hub==0.14.1
|
4 |
torch==2.0.1
|
5 |
+
transformers==4.30.2
|
6 |
+
safetensors==0.3.1
|
7 |
gradio==3.24.1
|
8 |
Pillow==10.0.0
|