Forgets commited on
Commit
394208e
·
verified ·
1 Parent(s): fcd23a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -1,30 +1,42 @@
1
  import gradio as gr
2
  import moondream as md
3
  from PIL import Image
 
4
 
5
- # Load model secara otomatis
6
- # Library ini akan menangani download dan konfigurasi sendiri
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
- # Konversi ke PIL Image jika perlu
14
  image = image.convert("RGB")
15
 
16
- # Proses encoding dan jawaban dalam satu baris simpel
17
- encoded_image = model.encode_image(image)
18
- answer = model.answer_question(encoded_image, question)
19
-
20
- return answer
 
 
 
 
 
 
 
21
 
22
  # Interface Gradio
23
  interface = gr.Interface(
24
  fn=answer_question,
25
- inputs=[gr.Image(type="pil"), gr.Textbox(label="Question", value="What is the result of the math expression?")],
26
- outputs=gr.Text(label="Answer"),
27
- title="Moondream Solver (Stable Version)"
 
 
 
 
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__":