Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Security
|
|
| 18 |
from fastapi.responses import JSONResponse
|
| 19 |
from fastapi.security.api_key import APIKeyHeader
|
| 20 |
|
| 21 |
-
import httpx
|
| 22 |
from sarvamai import SarvamAI
|
| 23 |
|
| 24 |
app = FastAPI(
|
|
@@ -319,7 +318,7 @@ def extract_text_with_sdk(file_bytes: bytes, filename: str) -> str:
|
|
| 319 |
pass
|
| 320 |
|
| 321 |
# ---------------------------------------------------------------------------
|
| 322 |
-
# Step 2: Extract structured JSON via
|
| 323 |
# ---------------------------------------------------------------------------
|
| 324 |
|
| 325 |
def strip_think_tags(text: str) -> str:
|
|
@@ -336,6 +335,9 @@ def strip_think_tags(text: str) -> str:
|
|
| 336 |
|
| 337 |
|
| 338 |
async def extract_json_via_chat(doc_type: str, raw_text: str, parameters: list[str]) -> dict:
|
|
|
|
|
|
|
|
|
|
| 339 |
description = SUPPORTED_TYPES[doc_type]
|
| 340 |
param_list = "\n".join(f" - {p}" for p in parameters)
|
| 341 |
|
|
@@ -350,25 +352,25 @@ Rules:
|
|
| 350 |
2. Use exactly the field names listed above as JSON keys.
|
| 351 |
3. If a field is not found or not legible, set its value to null.
|
| 352 |
4. Do not invent or infer values not explicitly present in the document.
|
| 353 |
-
5.
|
| 354 |
Respond with the JSON object only."""
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
}
|
| 366 |
)
|
|
|
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
|
| 373 |
# Remove <think> blocks — handles both complete and truncated closing tags
|
| 374 |
raw = strip_think_tags(raw)
|
|
|
|
| 18 |
from fastapi.responses import JSONResponse
|
| 19 |
from fastapi.security.api_key import APIKeyHeader
|
| 20 |
|
|
|
|
| 21 |
from sarvamai import SarvamAI
|
| 22 |
|
| 23 |
app = FastAPI(
|
|
|
|
| 318 |
pass
|
| 319 |
|
| 320 |
# ---------------------------------------------------------------------------
|
| 321 |
+
# Step 2: Extract structured JSON via SarvamAI SDK chat completions
|
| 322 |
# ---------------------------------------------------------------------------
|
| 323 |
|
| 324 |
def strip_think_tags(text: str) -> str:
|
|
|
|
| 335 |
|
| 336 |
|
| 337 |
async def extract_json_via_chat(doc_type: str, raw_text: str, parameters: list[str]) -> dict:
|
| 338 |
+
if not SARVAM_API_KEY:
|
| 339 |
+
raise HTTPException(status_code=500, detail="SARVAM_API_KEY is not configured on the server")
|
| 340 |
+
|
| 341 |
description = SUPPORTED_TYPES[doc_type]
|
| 342 |
param_list = "\n".join(f" - {p}" for p in parameters)
|
| 343 |
|
|
|
|
| 352 |
2. Use exactly the field names listed above as JSON keys.
|
| 353 |
3. If a field is not found or not legible, set its value to null.
|
| 354 |
4. Do not invent or infer values not explicitly present in the document.
|
| 355 |
+
5. Respond everything in english language only. Also convert date into proper DD/MM/YYYY format.
|
| 356 |
Respond with the JSON object only."""
|
| 357 |
|
| 358 |
+
# Run the synchronous SDK call in a thread so we don't block the event loop
|
| 359 |
+
def _call_sdk() -> str:
|
| 360 |
+
client = SarvamAI(api_subscription_key=SARVAM_API_KEY)
|
| 361 |
+
response = client.chat.completions(
|
| 362 |
+
model="sarvam-30b",
|
| 363 |
+
messages=[{"role": "user", "content": prompt}],
|
| 364 |
+
temperature=0,
|
| 365 |
+
top_p=1,
|
| 366 |
+
max_tokens=2048,
|
|
|
|
| 367 |
)
|
| 368 |
+
return response.choices[0].message.content.strip()
|
| 369 |
|
| 370 |
+
try:
|
| 371 |
+
raw = await asyncio.to_thread(_call_sdk)
|
| 372 |
+
except Exception as exc:
|
| 373 |
+
raise HTTPException(status_code=500, detail=f"Sarvam chat error: {exc}") from exc
|
| 374 |
|
| 375 |
# Remove <think> blocks — handles both complete and truncated closing tags
|
| 376 |
raw = strip_think_tags(raw)
|