Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,28 @@ 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="2024-
|
| 13 |
)
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, revision="2024-
|
| 15 |
|
| 16 |
def answer_question(image, question):
|
| 17 |
if image is None:
|
| 18 |
return "No image provided"
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
enc_image = model.encode_image(image)
|
| 22 |
answer = model.answer_question(enc_image, question, tokenizer)
|
| 23 |
return answer
|
| 24 |
|
| 25 |
-
# Interface Gradio
|
| 26 |
interface = gr.Interface(
|
| 27 |
fn=answer_question,
|
| 28 |
inputs=[gr.Image(type="pil"), gr.Textbox(label="Question")],
|
|
|
|
| 5 |
|
| 6 |
# Load model dan tokenizer
|
| 7 |
model_id = "vikhyatk/moondream2"
|
| 8 |
+
# Gunakan revision terbaru yang kompatibel dengan transformers 4.45+
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
trust_remote_code=True,
|
| 12 |
+
revision="2024-08-26" # Update ke revisi yang lebih baru
|
| 13 |
)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, revision="2024-08-26")
|
| 15 |
|
| 16 |
def answer_question(image, question):
|
| 17 |
if image is None:
|
| 18 |
return "No image provided"
|
| 19 |
|
| 20 |
+
# Deteksi device (CPU/GPU)
|
| 21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
+
model.to(device)
|
| 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")],
|