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