Spaces:
Runtime error
Runtime error
Commit
ยท
7063ff1
1
Parent(s):
7172545
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,43 @@
|
|
| 1 |
#ํ๊น
ํ์ด์ค์์ ๋์๊ฐ ์ ์๋๋ก ๋ฐ๊พธ์ด ๋ณด์์
|
| 2 |
|
|
|
|
| 3 |
import torch
|
| 4 |
-
from transformers import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
|
| 43 |
-
|
| 44 |
-
model_name = 'microsoft/git-base-vqav2'
|
| 45 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 46 |
-
|
| 47 |
-
# Trainer๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ ํ์ต
|
| 48 |
-
tokenizer = BertTokenizerFast.from_pretrained('bert-base-multilingual-cased')
|
| 49 |
-
|
| 50 |
-
def preprocess_function(examples):
|
| 51 |
-
tokenized_inputs = tokenizer(examples['question'], truncation=True, padding=True)
|
| 52 |
-
return {
|
| 53 |
-
'input_ids': tokenized_inputs['input_ids'],
|
| 54 |
-
'attention_mask': tokenized_inputs['attention_mask'],
|
| 55 |
-
'pixel_values': [(4, 3, 244, 244)] * len(tokenized_inputs['input_ids']),
|
| 56 |
-
'pixel_mask': [1] * len(tokenized_inputs['input_ids']),
|
| 57 |
-
'labels': [[label] for label in examples['answers']]
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
dataset = load_dataset("Multimodal-Fatima/OK-VQA_train")['train'].select(range(300))
|
| 61 |
-
ok_vqa_dataset = dataset.map(preprocess_function, batched=True)
|
| 62 |
-
ok_vqa_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'pixel_values', 'pixel_mask', 'labels'])
|
| 63 |
-
|
| 64 |
-
training_args = TrainingArguments(
|
| 65 |
-
output_dir='./results',
|
| 66 |
-
num_train_epochs=20,
|
| 67 |
-
per_device_train_batch_size=4,
|
| 68 |
-
logging_steps=500,
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
trainer = Trainer(
|
| 72 |
-
model=model,
|
| 73 |
-
args=training_args,
|
| 74 |
-
train_dataset=ok_vqa_dataset
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
trainer.train()
|
|
|
|
| 1 |
#ํ๊น
ํ์ด์ค์์ ๋์๊ฐ ์ ์๋๋ก ๋ฐ๊พธ์ด ๋ณด์์
|
| 2 |
|
| 3 |
+
import gradio as gr
|
| 4 |
import torch
|
| 5 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 6 |
+
|
| 7 |
+
# ๋ชจ๋ธ ์ด๊ธฐํ ๋ฐ ๊ฐ์ค์น ๋ถ๋ฌ์ค๊ธฐ
|
| 8 |
+
model_name = 'microsoft/git-base-vqav2' # ์ฌ์ฉํ ๋ชจ๋ธ์ ์ด๋ฆ
|
| 9 |
+
model = BertForSequenceClassification.from_pretrained(model_name)
|
| 10 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# ์์ธก ํจ์ ์ ์
|
| 13 |
+
def predict_answer(image, question):
|
| 14 |
+
inputs = tokenizer(question, return_tensors='pt')
|
| 15 |
+
input_ids = inputs['input_ids']
|
| 16 |
+
attention_mask = inputs['attention_mask']
|
| 17 |
+
|
| 18 |
+
# ์ด๋ฏธ์ง์ ๊ด๋ จ๋ ์ฒ๋ฆฌ ์ํ
|
| 19 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ฝ๋๋ฅผ ์ฌ๊ธฐ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค (์
๋ ฅ๋ ์ด๋ฏธ์ง์ ๋ํ ์ ์ฒ๋ฆฌ ๋ฑ)
|
| 20 |
+
|
| 21 |
+
# ๋ชจ๋ธ์ ์
๋ ฅ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ์ฌ ์์ธก ์ํ
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 24 |
+
|
| 25 |
+
# ์์ธก ๊ฒฐ๊ณผ์์ ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ๋ ์ด๋ธ ID ๊ฐ์ ธ์ค๊ธฐ
|
| 26 |
+
predicted_label_id = torch.argmax(outputs.logits).item()
|
| 27 |
+
predicted_label = id_to_label_fn(predicted_label_id)
|
| 28 |
+
|
| 29 |
+
return predicted_label
|
| 30 |
+
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=predict_answer,
|
| 33 |
+
inputs=["image", "text"],
|
| 34 |
+
outputs="text",
|
| 35 |
+
title="Visual Question Answering",
|
| 36 |
+
description="Input an image and a question to get the model's answer.",
|
| 37 |
+
example=[
|
| 38 |
+
"https://your_image_url.jpg",
|
| 39 |
+
"What is shown in the image?"
|
| 40 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
+
iface.launch()
|
|
|