Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoModelForImageClassification, AutoFeatureExtractor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# 设置 Kaggle API 凭证
|
| 9 |
+
def setup_kaggle():
|
| 10 |
+
# 创建 .kaggle 目录
|
| 11 |
+
os.makedirs(os.path.expanduser("~/.kaggle"), exist_ok=True)
|
| 12 |
+
# 读取并写入 kaggle.json 文件
|
| 13 |
+
with open("/app/kaggle.json", "r") as f: # 直接使用 /app/kaggle.json 路径
|
| 14 |
+
kaggle_token = json.load(f)
|
| 15 |
+
with open(os.path.expanduser("~/.kaggle/kaggle.json"), "w") as f:
|
| 16 |
+
json.dump(kaggle_token, f)
|
| 17 |
+
os.chmod(os.path.expanduser("~/.kaggle/kaggle.json"), 0o600)
|
| 18 |
+
|
| 19 |
+
# 从 Kaggle 下载模型文件
|
| 20 |
+
def download_model():
|
| 21 |
+
# 设置 Kaggle API 凭证
|
| 22 |
+
setup_kaggle()
|
| 23 |
+
|
| 24 |
+
# 使用 Kaggle API 下载文件
|
| 25 |
+
os.system("kaggle kernels output sonia0822/20241015 -p /app") # 修改为您的 Kernel ID 和下载路径
|
| 26 |
+
|
| 27 |
+
# 确保模型文件已下载
|
| 28 |
+
if not os.path.exists("/app/model.pth"):
|
| 29 |
+
raise FileNotFoundError("模型文件下载失败!")
|
| 30 |
+
|
| 31 |
+
# 在加载模型前下载
|
| 32 |
+
if not os.path.exists("model.pth"):
|
| 33 |
+
print("Downloading model...")
|
| 34 |
+
download_model()
|
| 35 |
+
|
| 36 |
+
# 模型保存路径
|
| 37 |
+
classification_model_path = "/app/model.pth"
|
| 38 |
+
gpt2_model_path = "/app/gpt2-finetuned"
|
| 39 |
+
|
| 40 |
+
# 加载分类模型和特征提取器
|
| 41 |
+
print("加载分类模型...")
|
| 42 |
+
classification_model = AutoModelForImageClassification.from_pretrained(
|
| 43 |
+
"microsoft/beit-base-patch16-224-pt22k", num_labels=16
|
| 44 |
+
)
|
| 45 |
+
classification_model.load_state_dict(torch.load(classification_model_path, map_location="cpu"))
|
| 46 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("microsoft/beit-base-patch16-224-pt22k")
|
| 47 |
+
print("分类模型加载成功")
|
| 48 |
+
|
| 49 |
+
# 加载 GPT-2 文本生成模型
|
| 50 |
+
print("加载 GPT-2 模型...")
|
| 51 |
+
gpt2_generator = pipeline("text-generation", model=gpt2_model_path, tokenizer=gpt2_model_path)
|
| 52 |
+
print("GPT-2 模型加载成功")
|
| 53 |
+
|
| 54 |
+
# 定义风格标签列表
|
| 55 |
+
art_styles = [
|
| 56 |
+
"现实主义", "巴洛克", "后印象派", "印象派", "浪漫主义", "超现实主义",
|
| 57 |
+
"表现主义", "立体派", "野兽派", "抽象艺术", "新艺术", "象征主义",
|
| 58 |
+
"新古典主义", "洛可可", "文艺复兴", "极简主义"
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
# 标签映射
|
| 62 |
+
label_mapping = {0: 0, 2: 1, 3: 2, 4: 3, 7: 4, 9: 5, 10: 6, 12: 7, 15: 8, 17: 9, 18: 10, 20: 11, 21: 12, 23: 13, 24: 14, 25: 15}
|
| 63 |
+
reverse_label_mapping = {v: k for k, v in label_mapping.items()}
|
| 64 |
+
|
| 65 |
+
# 生成风格描述的函数
|
| 66 |
+
def classify_and_generate_description(image):
|
| 67 |
+
image = image.convert("RGB")
|
| 68 |
+
inputs = feature_extractor(images=image, return_tensors="pt").to("cpu")
|
| 69 |
+
classification_model.eval()
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
outputs = classification_model(**inputs).logits
|
| 72 |
+
predicted_class = torch.argmax(outputs, dim=1).item()
|
| 73 |
+
|
| 74 |
+
predicted_label = reverse_label_mapping.get(predicted_class, "未知")
|
| 75 |
+
predicted_style = art_styles[predicted_class] if predicted_class < len(art_styles) else "未知"
|
| 76 |
+
|
| 77 |
+
prompt = f"请详细描述{predicted_style}的艺术风格。"
|
| 78 |
+
description = gpt2_generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
| 79 |
+
return predicted_style, description
|
| 80 |
+
|
| 81 |
+
def ask_gpt2(question):
|
| 82 |
+
response = gpt2_generator(question, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
| 83 |
+
return response
|
| 84 |
+
|
| 85 |
+
# Gradio 界面
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("# 艺术风格分类和生成描述")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
image_input = gr.Image(label="上传一张艺术图片")
|
| 90 |
+
style_output = gr.Textbox(label="预测的艺术风格")
|
| 91 |
+
description_output = gr.Textbox(label="生成的风格描述")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
question_input = gr.Textbox(label="输入问题")
|
| 94 |
+
answer_output = gr.Textbox(label="GPT-2 生成的回答")
|
| 95 |
+
classify_btn = gr.Button("生成风格描述")
|
| 96 |
+
question_btn = gr.Button("问 GPT-2 一个问题")
|
| 97 |
+
classify_btn.click(fn=classify_and_generate_description, inputs=image_input, outputs=[style_output, description_output])
|
| 98 |
+
question_btn.click(fn=ask_gpt2, inputs=question_input, outputs=answer_output)
|
| 99 |
+
|
| 100 |
+
demo.launch()
|