Spaces:
Sleeping
Sleeping
Muhusystem
commited on
Commit
·
b64070e
1
Parent(s):
b6556e2
Add Gradio app and requirements
Browse files- .ipynb_checkpoints/app-checkpoint.py +20 -0
- .ipynb_checkpoints/requirements-checkpoint.txt +4 -0
- app.py +63 -30
- requirements.txt +1 -0
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from inference import load_model, classify_text
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
# 加载模型
|
7 |
+
model, tokenizer = load_model()
|
8 |
+
|
9 |
+
@app.route('/predict', methods=['POST'])
|
10 |
+
def predict():
|
11 |
+
data = request.json
|
12 |
+
text = data.get("text", "")
|
13 |
+
if not text:
|
14 |
+
return jsonify({"error": "No text provided"}), 400
|
15 |
+
# 进行推理
|
16 |
+
prediction = classify_text(text, model, tokenizer)
|
17 |
+
return jsonify({"result": prediction})
|
18 |
+
|
19 |
+
if __name__ == '__main__':
|
20 |
+
app.run(debug=True)
|
.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
gunicorn
|
app.py
CHANGED
@@ -1,45 +1,78 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import
|
4 |
from PIL import Image
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# 加载模型
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
def
|
18 |
-
#
|
|
|
19 |
image_features = feature_extractor(images=image, return_tensors="pt")
|
20 |
-
|
21 |
-
# 文本编码
|
22 |
-
text_inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
result = "Positive" if torch.mean(fused_features) > 0 else "Negative"
|
34 |
-
return result
|
35 |
|
36 |
# 创建 Gradio 界面
|
37 |
-
iface = gr.Interface(
|
38 |
-
fn=multimodal_pipeline,
|
39 |
-
inputs=["image", "text"],
|
40 |
-
outputs="text",
|
41 |
-
title="Multi-modal Sentiment Analysis"
|
42 |
-
)
|
43 |
-
|
44 |
-
# 启动 Gradio 应用
|
45 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import GPT2Model, ViTModel, GPT2Tokenizer, ViTFeatureExtractor
|
4 |
from PIL import Image
|
5 |
+
import requests
|
6 |
+
import os
|
7 |
|
8 |
+
# 定义多模态模型
|
9 |
+
class MultiModalModel(torch.nn.Module):
|
10 |
+
def __init__(self, gpt2_model_name="gpt2", vit_model_name="google/vit-base-patch16-224-in21k"):
|
11 |
+
super(MultiModalModel, self).__init__()
|
12 |
+
self.gpt2 = GPT2Model.from_pretrained(gpt2_model_name)
|
13 |
+
self.vit = ViTModel.from_pretrained(vit_model_name)
|
14 |
+
self.classifier = torch.nn.Linear(self.gpt2.config.hidden_size + self.vit.config.hidden_size, 2)
|
15 |
|
16 |
+
def forward(self, input_ids, attention_mask, pixel_values):
|
17 |
+
gpt2_outputs = self.gpt2(input_ids=input_ids, attention_mask=attention_mask)
|
18 |
+
text_features = gpt2_outputs.last_hidden_state[:, -1, :]
|
19 |
+
|
20 |
+
vit_outputs = self.vit(pixel_values=pixel_values)
|
21 |
+
image_features = vit_outputs.last_hidden_state[:, 0, :]
|
22 |
+
|
23 |
+
fused_features = torch.cat((text_features, image_features), dim=1)
|
24 |
+
logits = self.classifier(fused_features)
|
25 |
+
return logits
|
26 |
|
27 |
# 加载模型
|
28 |
+
def load_model():
|
29 |
+
model_name = "Muhusjf/ViT-GPT2-multimodal-model"
|
30 |
+
model = MultiModalModel()
|
31 |
+
# 下载模型权重
|
32 |
+
model_url = f"https://huggingface.co/{model_name}/resolve/main/pytorch_model.bin"
|
33 |
+
model_path = "./pytorch_model.bin"
|
34 |
+
if not os.path.exists(model_path):
|
35 |
+
response = requests.get(model_url)
|
36 |
+
with open(model_path, "wb") as f:
|
37 |
+
f.write(response.content)
|
38 |
+
# 加载权重
|
39 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
40 |
+
model.eval()
|
41 |
+
return model
|
42 |
+
|
43 |
+
# 初始化模型和加载器
|
44 |
+
model = load_model()
|
45 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
46 |
+
tokenizer.pad_token = tokenizer.eos_token
|
47 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
|
48 |
|
49 |
+
# 定义推理函数
|
50 |
+
def predict(image, text):
|
51 |
+
# 处理图像
|
52 |
+
image = Image.fromarray(image)
|
53 |
image_features = feature_extractor(images=image, return_tensors="pt")
|
|
|
|
|
|
|
54 |
|
55 |
+
# 处理文本
|
56 |
+
inputs = tokenizer.encode_plus(
|
57 |
+
f"Question: {text} Answer:",
|
58 |
+
return_tensors="pt",
|
59 |
+
max_length=128,
|
60 |
+
truncation=True,
|
61 |
+
padding="max_length"
|
62 |
+
)
|
63 |
+
|
64 |
+
input_ids = inputs["input_ids"]
|
65 |
+
attention_mask = inputs["attention_mask"]
|
66 |
+
pixel_values = image_features["pixel_values"]
|
67 |
|
68 |
+
# 推理
|
69 |
+
with torch.no_grad():
|
70 |
+
logits = model(input_ids, attention_mask, pixel_values)
|
71 |
+
prediction = torch.argmax(logits, dim=1).item()
|
72 |
+
label = "yes" if prediction == 1 else "no"
|
73 |
|
74 |
+
return label
|
|
|
|
|
75 |
|
76 |
# 创建 Gradio 界面
|
77 |
+
iface = gr.Interface(fn=predict, inputs=["image", "text"], outputs="text", title="Multi-modal Inference")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
iface.launch()
|
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ torch
|
|
2 |
transformers
|
3 |
gradio
|
4 |
pillow
|
|
|
|
2 |
transformers
|
3 |
gradio
|
4 |
pillow
|
5 |
+
requests
|