credent007 commited on
Commit
0dd9ca4
·
verified ·
1 Parent(s): 7edd25b

Update llm.py

Browse files
Files changed (1) hide show
  1. llm.py +8 -19
llm.py CHANGED
@@ -34,26 +34,13 @@ if torch.cuda.is_available():
34
  def execute_llm(model, processor, image, prompt: str):
35
 
36
  if not prompt:
37
- prompt = """
38
- Extract all text from image.
39
- Return ONLY valid JSON.
40
- """
41
-
42
- messages = [{
43
- "role": "user",
44
- "content": [
45
- {"type": "image", "image": image},
46
- {"type": "text", "text": prompt}
47
- ]
48
- }]
49
 
50
  with torch.inference_mode():
51
 
52
- inputs = processor.apply_chat_template(
53
- messages,
54
- add_generation_prompt=True,
55
- tokenize=True,
56
- return_dict=True,
57
  return_tensors="pt"
58
  )
59
 
@@ -65,11 +52,13 @@ def execute_llm(model, processor, image, prompt: str):
65
  do_sample=False
66
  )
67
 
68
- return processor.decode(
69
- outputs[0][inputs["input_ids"].shape[-1]:],
70
  skip_special_tokens=True
71
  )
72
 
 
 
73
  async def call_llm(image, prompt: str = ""):
74
  print("call llm")
75
 
 
34
  def execute_llm(model, processor, image, prompt: str):
35
 
36
  if not prompt:
37
+ prompt = "Extract all text from the given image and return ONLY valid JSON."
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  with torch.inference_mode():
40
 
41
+ inputs = processor(
42
+ text=prompt,
43
+ images=image,
 
 
44
  return_tensors="pt"
45
  )
46
 
 
52
  do_sample=False
53
  )
54
 
55
+ result = processor.decode(
56
+ outputs[0],
57
  skip_special_tokens=True
58
  )
59
 
60
+ return result
61
+
62
  async def call_llm(image, prompt: str = ""):
63
  print("call llm")
64