Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import moondream as md
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
model = md.vl(model="vikhyatk/moondream2")
|
| 8 |
|
| 9 |
def answer_question(image, question):
|
| 10 |
if image is None:
|
| 11 |
return "No image provided"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
image = image.convert("RGB")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Interface Gradio
|
| 23 |
interface = gr.Interface(
|
| 24 |
fn=answer_question,
|
| 25 |
-
inputs=[
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import moondream as md
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Inisialisasi model secara lokal
|
| 7 |
+
# Perhatikan: Kita memanggil md.vl() tapi pastikan model dimuat dengan benar
|
| 8 |
model = md.vl(model="vikhyatk/moondream2")
|
| 9 |
|
| 10 |
def answer_question(image, question):
|
| 11 |
if image is None:
|
| 12 |
return "No image provided"
|
| 13 |
|
| 14 |
+
# Pastikan format gambar benar
|
| 15 |
image = image.convert("RGB")
|
| 16 |
|
| 17 |
+
try:
|
| 18 |
+
# Untuk library moondream terbaru mode lokal:
|
| 19 |
+
# Kita langsung menggunakan method .query()
|
| 20 |
+
result = model.query(image, question)
|
| 21 |
+
|
| 22 |
+
# Output biasanya berupa dictionary {'answer': '...'}
|
| 23 |
+
if isinstance(result, dict) and 'answer' in result:
|
| 24 |
+
return result['answer']
|
| 25 |
+
return str(result)
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
|
| 30 |
# Interface Gradio
|
| 31 |
interface = gr.Interface(
|
| 32 |
fn=answer_question,
|
| 33 |
+
inputs=[
|
| 34 |
+
gr.Image(type="pil", label="Upload Captcha"),
|
| 35 |
+
gr.Textbox(label="Question", value="What is the text or number in this image?")
|
| 36 |
+
],
|
| 37 |
+
outputs=gr.Text(label="Result"),
|
| 38 |
+
title="Moondream Solver (Local Stable)",
|
| 39 |
+
description="Menggunakan library moondream native untuk stabilitas tinggi."
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|