pmhanh
commited on
Commit
·
a288221
1
Parent(s):
234b704
Update app.py
Browse files- app.py +46 -35
- requirements.txt +5 -5
app.py
CHANGED
@@ -7,7 +7,7 @@ import json
|
|
7 |
from torch import nn
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
-
# Định nghĩa mô hình
|
11 |
class VQAModel(nn.Module):
|
12 |
def __init__(self, num_answers):
|
13 |
super(VQAModel, self).__init__()
|
@@ -29,48 +29,59 @@ class VQAModel(nn.Module):
|
|
29 |
repo_id = "duyan2803/vqa-model-vit-bert"
|
30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
-
# Load weights
|
39 |
-
weights_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
|
40 |
-
model = VQAModel(num_answers=num_answers)
|
41 |
-
|
42 |
-
model.
|
43 |
-
model.
|
|
|
|
|
44 |
|
45 |
-
# Load tokenizer
|
46 |
-
tokenizer = BertTokenizer.from_pretrained(repo_id)
|
|
|
47 |
|
48 |
-
# Load answer list
|
49 |
-
answer_list_path = hf_hub_download(repo_id=repo_id, filename="answer_list.json")
|
50 |
-
with open(answer_list_path, "r") as f:
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Hàm dự đoán
|
54 |
def predict(image, question):
|
55 |
-
|
56 |
-
|
57 |
-
transforms.
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
|
75 |
# Giao diện Gradio
|
76 |
interface = gr.Interface(
|
|
|
7 |
from torch import nn
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
+
# Định nghĩa mô hình
|
11 |
class VQAModel(nn.Module):
|
12 |
def __init__(self, num_answers):
|
13 |
super(VQAModel, self).__init__()
|
|
|
29 |
repo_id = "duyan2803/vqa-model-vit-bert"
|
30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
31 |
|
32 |
+
try:
|
33 |
+
# Load config
|
34 |
+
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
|
35 |
+
with open(config_path, "r") as f:
|
36 |
+
config = json.load(f)
|
37 |
+
num_answers = config["num_answers"]
|
38 |
|
39 |
+
# Load weights
|
40 |
+
weights_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
|
41 |
+
model = VQAModel(num_answers=num_answers)
|
42 |
+
state_dict = torch.load(weights_path, map_location=device, weights_only=True)
|
43 |
+
model.load_state_dict(state_dict)
|
44 |
+
model.to(device)
|
45 |
+
model.eval()
|
46 |
+
print("Đã load mô hình thành công!")
|
47 |
|
48 |
+
# Load tokenizer
|
49 |
+
tokenizer = BertTokenizer.from_pretrained(repo_id)
|
50 |
+
print("Đã load tokenizer thành công!")
|
51 |
|
52 |
+
# Load answer list
|
53 |
+
answer_list_path = hf_hub_download(repo_id=repo_id, filename="answer_list.json")
|
54 |
+
with open(answer_list_path, "r") as f:
|
55 |
+
answer_list = json.load(f)
|
56 |
+
print("Đã load answer list thành công!")
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Lỗi khi load mô hình hoặc file: {str(e)}")
|
59 |
+
raise e
|
60 |
|
61 |
# Hàm dự đoán
|
62 |
def predict(image, question):
|
63 |
+
try:
|
64 |
+
# Xử lý ảnh
|
65 |
+
transform = transforms.Compose([
|
66 |
+
transforms.Resize((224, 224)),
|
67 |
+
transforms.ToTensor(),
|
68 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
69 |
+
])
|
70 |
+
image_tensor = transform(image).unsqueeze(0).to(device)
|
71 |
|
72 |
+
# Xử lý câu hỏi
|
73 |
+
tokenized = tokenizer(question, padding='max_length', truncation=True, max_length=32, return_tensors='pt')
|
74 |
+
input_ids = tokenized['input_ids'].to(device)
|
75 |
+
attention_mask = tokenized['attention_mask'].to(device)
|
76 |
|
77 |
+
# Dự đoán
|
78 |
+
with torch.no_grad():
|
79 |
+
output = model(image_tensor, input_ids, attention_mask)
|
80 |
+
pred_idx = output.argmax(dim=1).item()
|
81 |
+
|
82 |
+
return answer_list[pred_idx]
|
83 |
+
except Exception as e:
|
84 |
+
return f"Lỗi khi dự đoán: {str(e)}"
|
85 |
|
86 |
# Giao diện Gradio
|
87 |
interface = gr.Interface(
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
torch
|
2 |
-
transformers
|
3 |
-
torchvision
|
4 |
pillow
|
5 |
-
gradio
|
6 |
-
huggingface_hub
|
|
|
1 |
+
torch==2.0.1
|
2 |
+
transformers>=4.32.0
|
3 |
+
torchvision==0.15.2
|
4 |
pillow
|
5 |
+
gradio==4.0.2
|
6 |
+
huggingface_hub>=0.29.0
|