Spaces:
Paused
Paused
Update llm.py
Browse files
llm.py
CHANGED
|
@@ -36,9 +36,9 @@ processor=AutoProcessor.from_pretrained("datalab-to/chandra-ocr-2")
|
|
| 36 |
model=AutoModelForImageTextToText.from_pretrained("datalab-to/chandra-ocr-2",quantization_config=quant_config)
|
| 37 |
|
| 38 |
def execute_llm(model, processor,image, prompt: str):
|
| 39 |
-
|
| 40 |
# Prompt (IMPORTANT)
|
| 41 |
-
prompt = "Extract all text from the image and return structured JSON
|
| 42 |
|
| 43 |
messages = [
|
| 44 |
{
|
|
@@ -58,13 +58,13 @@ def execute_llm(model, processor,image, prompt: str):
|
|
| 58 |
return_dict=True,
|
| 59 |
return_tensors="pt"
|
| 60 |
).to(model.device)
|
| 61 |
-
|
| 62 |
# Generate
|
| 63 |
outputs = model.generate(
|
| 64 |
**inputs,
|
| 65 |
max_new_tokens=1000
|
| 66 |
)
|
| 67 |
-
|
| 68 |
# Decode only new tokens
|
| 69 |
result = processor.decode(
|
| 70 |
outputs[0][inputs["input_ids"].shape[-1]:],
|
|
@@ -72,11 +72,13 @@ def execute_llm(model, processor,image, prompt: str):
|
|
| 72 |
)
|
| 73 |
|
| 74 |
print(result)
|
|
|
|
| 75 |
# ✅ SYNC function (important)
|
| 76 |
|
| 77 |
|
| 78 |
# ✅ ASYNC wrapper
|
| 79 |
async def call_llm(image,prompt: str):
|
|
|
|
| 80 |
loop = asyncio.get_event_loop()
|
| 81 |
|
| 82 |
func = partial(execute_llm, model, processor,image, prompt)
|
|
|
|
| 36 |
model=AutoModelForImageTextToText.from_pretrained("datalab-to/chandra-ocr-2",quantization_config=quant_config)
|
| 37 |
|
| 38 |
def execute_llm(model, processor,image, prompt: str):
|
| 39 |
+
print('execulte llm')
|
| 40 |
# Prompt (IMPORTANT)
|
| 41 |
+
prompt = "Extract all text from the image and return structured JSON "
|
| 42 |
|
| 43 |
messages = [
|
| 44 |
{
|
|
|
|
| 58 |
return_dict=True,
|
| 59 |
return_tensors="pt"
|
| 60 |
).to(model.device)
|
| 61 |
+
print('input generated')
|
| 62 |
# Generate
|
| 63 |
outputs = model.generate(
|
| 64 |
**inputs,
|
| 65 |
max_new_tokens=1000
|
| 66 |
)
|
| 67 |
+
print('output bhi')
|
| 68 |
# Decode only new tokens
|
| 69 |
result = processor.decode(
|
| 70 |
outputs[0][inputs["input_ids"].shape[-1]:],
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
print(result)
|
| 75 |
+
return result
|
| 76 |
# ✅ SYNC function (important)
|
| 77 |
|
| 78 |
|
| 79 |
# ✅ ASYNC wrapper
|
| 80 |
async def call_llm(image,prompt: str):
|
| 81 |
+
print('call llm')
|
| 82 |
loop = asyncio.get_event_loop()
|
| 83 |
|
| 84 |
func = partial(execute_llm, model, processor,image, prompt)
|