Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Load model dan tokenizer
|
| 7 |
model_id = "vikhyatk/moondream2"
|
| 8 |
revision = "2024-08-26"
|
| 9 |
-
|
| 10 |
-
# Deteksi device (Gunakan GPU jika tersedia, jika tidak gunakan CPU)
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
model_id,
|
|
|
|
| 15 |
trust_remote_code=True,
|
| 16 |
-
revision=revision
|
|
|
|
| 17 |
).to(device)
|
|
|
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 19 |
|
| 20 |
def answer_question(image, question):
|
| 21 |
if image is None:
|
| 22 |
return "No image provided"
|
| 23 |
|
| 24 |
-
# Pastikan gambar dalam format RGB
|
| 25 |
image = image.convert("RGB")
|
| 26 |
|
| 27 |
-
# Proses gambar dan dapatkan jawaban
|
| 28 |
with torch.no_grad():
|
|
|
|
| 29 |
enc_image = model.encode_image(image)
|
| 30 |
answer = model.answer_question(enc_image, question, tokenizer)
|
| 31 |
|
|
@@ -34,10 +41,9 @@ def answer_question(image, question):
|
|
| 34 |
# Interface Gradio
|
| 35 |
interface = gr.Interface(
|
| 36 |
fn=answer_question,
|
| 37 |
-
inputs=[gr.Image(type="pil"), gr.Textbox(label="Question"
|
| 38 |
outputs=gr.Text(label="Answer"),
|
| 39 |
-
title="Moondream Captcha Solver"
|
| 40 |
-
description="Upload image captcha dan biarkan model menjawabnya."
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
model_id = "vikhyatk/moondream2"
|
| 7 |
revision = "2024-08-26"
|
|
|
|
|
|
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
+
# 1. Load konfigurasi terlebih dahulu
|
| 11 |
+
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True, revision=revision)
|
| 12 |
+
|
| 13 |
+
# 2. Tambahkan pad_token_id secara manual ke config agar modeling_phi.py tidak marah
|
| 14 |
+
if not hasattr(config, 'pad_token_id'):
|
| 15 |
+
config.pad_token_id = config.eos_token_id
|
| 16 |
+
|
| 17 |
+
# 3. Load model menggunakan config yang sudah kita perbaiki
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
model_id,
|
| 20 |
+
config=config,
|
| 21 |
trust_remote_code=True,
|
| 22 |
+
revision=revision,
|
| 23 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32 # Optimasi memori
|
| 24 |
).to(device)
|
| 25 |
+
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 27 |
|
| 28 |
def answer_question(image, question):
|
| 29 |
if image is None:
|
| 30 |
return "No image provided"
|
| 31 |
|
|
|
|
| 32 |
image = image.convert("RGB")
|
| 33 |
|
|
|
|
| 34 |
with torch.no_grad():
|
| 35 |
+
# Moondream menggunakan metode internal untuk menjawab
|
| 36 |
enc_image = model.encode_image(image)
|
| 37 |
answer = model.answer_question(enc_image, question, tokenizer)
|
| 38 |
|
|
|
|
| 41 |
# Interface Gradio
|
| 42 |
interface = gr.Interface(
|
| 43 |
fn=answer_question,
|
| 44 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Question")],
|
| 45 |
outputs=gr.Text(label="Answer"),
|
| 46 |
+
title="Moondream Captcha Solver"
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|