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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -39
app.py CHANGED
@@ -1,59 +1,30 @@
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 Tokenizer
11
- tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
12
-
13
- # 2. Load Config dengan trust_remote_code
14
- config = AutoConfig.from_pretrained(model_id, trust_remote_code=True, revision=revision)
15
-
16
- # 3. INTERVENSI KONFIGURASI (Penting!)
17
- if hasattr(config, "text_config"):
18
- # Fix untuk pad_token_id
19
- config.text_config.pad_token_id = tokenizer.eos_token_id
20
-
21
- # Fix untuk KeyError: 'type' pada rope_scaling
22
- # Kita paksa formatnya agar sesuai dengan apa yang diminta modeling_phi.py
23
- if hasattr(config.text_config, "rope_scaling") and config.text_config.rope_scaling is not None:
24
- if isinstance(config.text_config.rope_scaling, dict) and "type" not in config.text_config.rope_scaling:
25
- # Jika ada tapi tidak ada key 'type', kita beri default 'linear' atau hapus
26
- config.text_config.rope_scaling = None
27
- else:
28
- config.text_config.rope_scaling = None
29
-
30
- # 4. Load Model
31
- model = AutoModelForCausalLM.from_pretrained(
32
- model_id,
33
- config=config,
34
- trust_remote_code=True,
35
- revision=revision,
36
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
37
- ).to(device)
38
 
39
  def answer_question(image, question):
40
  if image is None:
41
  return "No image provided"
42
 
 
43
  image = image.convert("RGB")
44
 
45
- with torch.no_grad():
46
- enc_image = model.encode_image(image)
47
- answer = model.answer_question(enc_image, question, tokenizer)
48
 
49
  return answer
50
 
51
  # Interface Gradio
52
  interface = gr.Interface(
53
  fn=answer_question,
54
- inputs=[gr.Image(type="pil"), gr.Textbox(label="Question")],
55
  outputs=gr.Text(label="Answer"),
56
- title="Moondream Captcha Solver"
57
  )
58
 
59
  if __name__ == "__main__":
 
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__":