Bapt120 commited on
Commit
12a0a22
·
verified ·
1 Parent(s): cdae040

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -10,6 +10,7 @@ import re
10
  import subprocess
11
  import sys
12
  import threading
 
13
  from collections import OrderedDict
14
  from io import BytesIO
15
 
@@ -29,6 +30,9 @@ from transformers import (
29
  VLLM_ENDPOINT_OCR = os.environ.get("VLLM_ENDPOINT_OCR")
30
  VLLM_ENDPOINT_BBOX = os.environ.get("VLLM_ENDPOINT_BBOX")
31
 
 
 
 
32
  # Model Registry with all supported models
33
  MODEL_REGISTRY = {
34
  "LightOnOCR-2-1B (Best OCR)": {
@@ -280,11 +284,16 @@ def extract_text_via_vllm(image, model_name, temperature=0.2, stream=False):
280
  )
281
 
282
  full_text = ""
 
283
  for chunk in response:
284
  if chunk.choices and chunk.choices[0].delta.content:
285
  full_text += chunk.choices[0].delta.content
286
- cleaned_text = clean_output_text(full_text)
287
- yield cleaned_text
 
 
 
 
288
  else:
289
  # Non-streaming response
290
  response = client.chat.completions.create(
@@ -387,13 +396,17 @@ def extract_text_from_image(image, model_name, temperature=0.2, stream=False):
387
 
388
  # Yield chunks as they arrive
389
  full_text = ""
 
390
  for new_text in streamer:
391
  full_text += new_text
392
- # Clean the accumulated text
393
- cleaned_text = clean_output_text(full_text)
394
- yield cleaned_text
 
395
 
396
  thread.join()
 
 
397
  else:
398
  # Non-streaming generation
399
  with torch.no_grad():
 
10
  import subprocess
11
  import sys
12
  import threading
13
+ import time
14
  from collections import OrderedDict
15
  from io import BytesIO
16
 
 
30
  VLLM_ENDPOINT_OCR = os.environ.get("VLLM_ENDPOINT_OCR")
31
  VLLM_ENDPOINT_BBOX = os.environ.get("VLLM_ENDPOINT_BBOX")
32
 
33
+ # Streaming configuration
34
+ STREAM_YIELD_INTERVAL = 0.5 # Yield every N seconds to reduce UI overhead
35
+
36
  # Model Registry with all supported models
37
  MODEL_REGISTRY = {
38
  "LightOnOCR-2-1B (Best OCR)": {
 
284
  )
285
 
286
  full_text = ""
287
+ last_yield_time = time.time()
288
  for chunk in response:
289
  if chunk.choices and chunk.choices[0].delta.content:
290
  full_text += chunk.choices[0].delta.content
291
+ # Batch yields to reduce UI overhead
292
+ if time.time() - last_yield_time > STREAM_YIELD_INTERVAL:
293
+ yield clean_output_text(full_text)
294
+ last_yield_time = time.time()
295
+ # Final yield with cleaned text
296
+ yield clean_output_text(full_text)
297
  else:
298
  # Non-streaming response
299
  response = client.chat.completions.create(
 
396
 
397
  # Yield chunks as they arrive
398
  full_text = ""
399
+ last_yield_time = time.time()
400
  for new_text in streamer:
401
  full_text += new_text
402
+ # Batch yields to reduce UI overhead
403
+ if time.time() - last_yield_time > STREAM_YIELD_INTERVAL:
404
+ yield clean_output_text(full_text)
405
+ last_yield_time = time.time()
406
 
407
  thread.join()
408
+ # Final yield with cleaned text
409
+ yield clean_output_text(full_text)
410
  else:
411
  # Non-streaming generation
412
  with torch.no_grad():