Update app.py
Browse files
app.py
CHANGED
|
@@ -33,9 +33,16 @@ if not hf_token:
|
|
| 33 |
logger.error("HF_TOKEN environment variable not set")
|
| 34 |
raise HTTPException(status_code=500, detail="HF_TOKEN environment variable not set")
|
| 35 |
|
| 36 |
-
# Initialize Hugging Face Inference Client
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# In-memory caches (1-hour TTL)
|
| 41 |
raw_text_cache = cachetools.TTLCache(maxsize=100, ttl=3600)
|
|
@@ -90,7 +97,7 @@ async def process_pdf_page(img, page_idx):
|
|
| 90 |
return ""
|
| 91 |
|
| 92 |
async def process_with_qwen(filename: str, raw_text: str):
|
| 93 |
-
"""Process raw text with Qwen2-7B-Instruct via Hugging Face Inference API."""
|
| 94 |
start_time = time.time()
|
| 95 |
logger.info(f"Starting Qwen API processing for {filename}, {log_memory_usage()}")
|
| 96 |
|
|
@@ -107,7 +114,7 @@ async def process_with_qwen(filename: str, raw_text: str):
|
|
| 107 |
|
| 108 |
try:
|
| 109 |
prompt = f"""
|
| 110 |
-
Extract key invoice fields as JSON from the raw text. Support English. Detect currency (e.g., USD, INR). Output only
|
| 111 |
Raw text: {raw_text}
|
| 112 |
Output JSON:
|
| 113 |
{{
|
|
@@ -133,7 +140,7 @@ async def process_with_qwen(filename: str, raw_text: str):
|
|
| 133 |
json_start = llm_output.find("{")
|
| 134 |
json_end = llm_output.rfind("}") + 1
|
| 135 |
if json_start == -1 or json_end == -1:
|
| 136 |
-
raise ValueError("No valid JSON found in
|
| 137 |
json_str = llm_output[json_start:json_end]
|
| 138 |
structured_data = json.loads(json_str)
|
| 139 |
structured_data_cache[text_hash] = structured_data
|
|
|
|
| 33 |
logger.error("HF_TOKEN environment variable not set")
|
| 34 |
raise HTTPException(status_code=500, detail="HF_TOKEN environment variable not set")
|
| 35 |
|
| 36 |
+
# Initialize Hugging Face Inference Client with primary and fallback models
|
| 37 |
+
primary_model = "Qwen/Qwen2-7B-Instruct"
|
| 38 |
+
fallback_model = "mistral/Mixtral-8x7B-Instruct-v0.1"
|
| 39 |
+
try:
|
| 40 |
+
client = InferenceClient(model=primary_model, token=hf_token, provider="auto")
|
| 41 |
+
logger.info(f"Hugging Face Inference Client initialized for {primary_model} with provider='auto'")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.warning(f"Failed to initialize client for {primary_model}: {str(e)}. Falling back to {fallback_model}")
|
| 44 |
+
client = InferenceClient(model=fallback_model, token=hf_token, provider="auto")
|
| 45 |
+
logger.info(f"Hugging Face Inference Client initialized for {fallback_model} with provider='auto'")
|
| 46 |
|
| 47 |
# In-memory caches (1-hour TTL)
|
| 48 |
raw_text_cache = cachetools.TTLCache(maxsize=100, ttl=3600)
|
|
|
|
| 97 |
return ""
|
| 98 |
|
| 99 |
async def process_with_qwen(filename: str, raw_text: str):
|
| 100 |
+
"""Process raw text with Qwen2-7B-Instruct or fallback via Hugging Face Inference API."""
|
| 101 |
start_time = time.time()
|
| 102 |
logger.info(f"Starting Qwen API processing for {filename}, {log_memory_usage()}")
|
| 103 |
|
|
|
|
| 114 |
|
| 115 |
try:
|
| 116 |
prompt = f"""
|
| 117 |
+
Extract key invoice fields as JSON from the raw text. Support English. Detect currency (e.g., USD, INR). Output only valid JSON, with no additional text, comments, or markdown.
|
| 118 |
Raw text: {raw_text}
|
| 119 |
Output JSON:
|
| 120 |
{{
|
|
|
|
| 140 |
json_start = llm_output.find("{")
|
| 141 |
json_end = llm_output.rfind("}") + 1
|
| 142 |
if json_start == -1 or json_end == -1:
|
| 143 |
+
raise ValueError("No valid JSON found in API output")
|
| 144 |
json_str = llm_output[json_start:json_end]
|
| 145 |
structured_data = json.loads(json_str)
|
| 146 |
structured_data_cache[text_hash] = structured_data
|