student0822 commited on
Commit
e8d514a
·
verified ·
1 Parent(s): 3cc6303

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -100
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
- 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()
 
 
 
 
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()