Update app.py
Browse files
app.py
CHANGED
|
@@ -5,33 +5,40 @@ import torch
|
|
| 5 |
|
| 6 |
# Load model dan tokenizer
|
| 7 |
model_id = "vikhyatk/moondream2"
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
trust_remote_code=True,
|
| 12 |
-
revision=
|
| 13 |
-
)
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=
|
| 15 |
|
| 16 |
def answer_question(image, question):
|
| 17 |
if image is None:
|
| 18 |
return "No image provided"
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Proses gambar
|
| 25 |
-
enc_image = model.encode_image(image)
|
| 26 |
-
answer = model.answer_question(enc_image, question, tokenizer)
|
| 27 |
return answer
|
| 28 |
|
| 29 |
# Interface Gradio
|
| 30 |
interface = gr.Interface(
|
| 31 |
fn=answer_question,
|
| 32 |
-
inputs=[gr.Image(type="pil"), gr.Textbox(label="Question")],
|
| 33 |
outputs=gr.Text(label="Answer"),
|
| 34 |
-
title="Moondream Captcha Solver"
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
| 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 |
|
|
|
|
|
|
|
|
|
|
| 32 |
return answer
|
| 33 |
|
| 34 |
# Interface Gradio
|
| 35 |
interface = gr.Interface(
|
| 36 |
fn=answer_question,
|
| 37 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Question", placeholder="What is written in the captcha?")],
|
| 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__":
|
| 44 |
+
interface.launch()
|