Update app.py
Browse files
app.py
CHANGED
|
@@ -31,20 +31,27 @@ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
|
| 31 |
# Set cache directory for Hugging Face
|
| 32 |
os.environ["HF_HOME"] = "/app/cache"
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 38 |
model_name,
|
| 39 |
torch_dtype=torch.float16,
|
| 40 |
device_map="auto",
|
| 41 |
-
low_cpu_mem_usage=True
|
|
|
|
| 42 |
)
|
| 43 |
-
processor = AutoProcessor.from_pretrained(model_name)
|
| 44 |
-
logger.info("Qwen2
|
| 45 |
except Exception as e:
|
| 46 |
-
logger.error(f"Failed to load Qwen2
|
| 47 |
-
raise HTTPException(status_code=500, detail="Failed to load Qwen2
|
| 48 |
|
| 49 |
# In-memory caches (1-hour TTL)
|
| 50 |
raw_text_cache = cachetools.TTLCache(maxsize=100, ttl=3600)
|
|
@@ -98,7 +105,7 @@ async def process_pdf_page(img, page_idx):
|
|
| 98 |
return ""
|
| 99 |
|
| 100 |
async def process_with_qwen(filename: str, raw_text: str):
|
| 101 |
-
"""Process raw text with Qwen2
|
| 102 |
start_time = time.time()
|
| 103 |
logger.info(f"Starting Qwen processing for {filename}, {log_memory_usage()}")
|
| 104 |
|
|
|
|
| 31 |
# Set cache directory for Hugging Face
|
| 32 |
os.environ["HF_HOME"] = "/app/cache"
|
| 33 |
|
| 34 |
+
# Get Hugging Face token from environment variable
|
| 35 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 36 |
+
if not hf_token:
|
| 37 |
+
logger.error("HF_TOKEN environment variable not set")
|
| 38 |
+
raise HTTPException(status_code=500, detail="HF_TOKEN environment variable not set")
|
| 39 |
+
|
| 40 |
+
# Load Qwen2-VL-2B-Instruct model on CPU
|
| 41 |
+
model_name = "Qwen/Qwen2-VL-2B-Instruct"
|
| 42 |
try:
|
| 43 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 44 |
model_name,
|
| 45 |
torch_dtype=torch.float16,
|
| 46 |
device_map="auto",
|
| 47 |
+
low_cpu_mem_usage=True,
|
| 48 |
+
token=hf_token
|
| 49 |
)
|
| 50 |
+
processor = AutoProcessor.from_pretrained(model_name, token=hf_token)
|
| 51 |
+
logger.info("Qwen2-VL-2B-Instruct model loaded successfully")
|
| 52 |
except Exception as e:
|
| 53 |
+
logger.error(f"Failed to load Qwen2-VL-2B-Instruct model: {str(e)}")
|
| 54 |
+
raise HTTPException(status_code=500, detail="Failed to load Qwen2-VL-2B-Instruct model")
|
| 55 |
|
| 56 |
# In-memory caches (1-hour TTL)
|
| 57 |
raw_text_cache = cachetools.TTLCache(maxsize=100, ttl=3600)
|
|
|
|
| 105 |
return ""
|
| 106 |
|
| 107 |
async def process_with_qwen(filename: str, raw_text: str):
|
| 108 |
+
"""Process raw text with Qwen2-VL-2B-Instruct to extract structured data."""
|
| 109 |
start_time = time.time()
|
| 110 |
logger.info(f"Starting Qwen processing for {filename}, {log_memory_usage()}")
|
| 111 |
|