Rename app.py to model.py
Browse files- app.py → model.py +16 -22
app.py → model.py
RENAMED
|
@@ -1,31 +1,25 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from diffusers import StableDiffusionPipeline
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# 加载Stable Diffusion模型
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# 加载GPT模型
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def generate_prompt(description):
|
| 14 |
-
prompt = gpt_model(f"Create a detailed image prompt based on this: {description}")
|
| 15 |
-
return prompt[0]['generated_text']
|
| 16 |
|
| 17 |
-
# 生成图像
|
| 18 |
-
def
|
| 19 |
-
image =
|
| 20 |
return image
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
def
|
| 24 |
-
prompt =
|
| 25 |
-
|
| 26 |
-
return prompt, image
|
| 27 |
-
|
| 28 |
-
# Gradio UI组件
|
| 29 |
-
iface = gr.Interface(fn=interface, inputs="text", outputs=["text", "image"])
|
| 30 |
|
| 31 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from diffusers import StableDiffusionPipeline
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# 加载Stable Diffusion模型
|
| 6 |
+
def load_stable_diffusion_model():
|
| 7 |
+
model = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
|
| 8 |
+
model.to("cuda" if torch.cuda.is_available() else "cpu") # 如果有GPU,使用GPU加速
|
| 9 |
+
return model
|
| 10 |
|
| 11 |
# 加载GPT模型
|
| 12 |
+
def load_gpt_model():
|
| 13 |
+
gpt_model = pipeline("text-generation", model="gpt2")
|
| 14 |
+
return gpt_model
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# 生成图像的函数
|
| 17 |
+
def generate_image_from_prompt(model, prompt):
|
| 18 |
+
image = model(prompt).images[0]
|
| 19 |
return image
|
| 20 |
|
| 21 |
+
# 使用GPT生成图像的详细提示
|
| 22 |
+
def generate_prompt(gpt_model, description):
|
| 23 |
+
prompt = gpt_model(f"Create a detailed image prompt based on this: {description}")
|
| 24 |
+
return prompt[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|