Spaces:
Sleeping
Sleeping
MakPr016 commited on
Commit ·
d08e16b
1
Parent(s): a3f9e7d
RFQ Parser
Browse files- .gitignore +3 -0
- main.py +62 -4
- requirements.txt +4 -1
- rfq_parser.py +84 -31
.gitignore
CHANGED
|
@@ -4,6 +4,8 @@ __pycache__/
|
|
| 4 |
*.pyd
|
| 5 |
.venv/
|
| 6 |
venv/
|
|
|
|
|
|
|
| 7 |
.env
|
| 8 |
.env.*
|
| 9 |
.python-version
|
|
@@ -16,3 +18,4 @@ build/
|
|
| 16 |
dist/
|
| 17 |
*.egg-info/
|
| 18 |
response.json
|
|
|
|
|
|
| 4 |
*.pyd
|
| 5 |
.venv/
|
| 6 |
venv/
|
| 7 |
+
.parser/
|
| 8 |
+
parser/
|
| 9 |
.env
|
| 10 |
.env.*
|
| 11 |
.python-version
|
|
|
|
| 18 |
dist/
|
| 19 |
*.egg-info/
|
| 20 |
response.json
|
| 21 |
+
response2.json
|
main.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import uvicorn
|
| 6 |
-
from rfq_parser import parse_rfq_pdf
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
|
@@ -20,21 +21,78 @@ app.add_middleware(
|
|
| 20 |
@app.post("/parse-rfq")
|
| 21 |
async def parse_rfq(
|
| 22 |
file: UploadFile = File(...),
|
| 23 |
-
|
| 24 |
):
|
| 25 |
if not file.filename.endswith(".pdf"):
|
| 26 |
raise HTTPException(status_code=400, detail="Only PDF files are supported")
|
| 27 |
|
| 28 |
-
if
|
|
|
|
|
|
|
|
|
|
| 29 |
raise HTTPException(status_code=500, detail="GOOGLE_API_KEY not configured")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
contents = await file.read()
|
| 32 |
try:
|
| 33 |
-
result = parse_rfq_pdf(contents,
|
| 34 |
return result
|
| 35 |
except Exception as e:
|
| 36 |
raise HTTPException(status_code=500, detail=str(e))
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
@app.get("/health")
|
| 39 |
def health():
|
| 40 |
return {"status": "ok"}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import httpx
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import uvicorn
|
| 7 |
+
from rfq_parser import parse_rfq_pdf, LLAMA_API_BASE_DEFAULT, LLAMA_MODEL_DEFAULT
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 21 |
@app.post("/parse-rfq")
|
| 22 |
async def parse_rfq(
|
| 23 |
file: UploadFile = File(...),
|
| 24 |
+
provider: str = Form("gemini"),
|
| 25 |
):
|
| 26 |
if not file.filename.endswith(".pdf"):
|
| 27 |
raise HTTPException(status_code=400, detail="Only PDF files are supported")
|
| 28 |
|
| 29 |
+
if provider not in ("gemini", "ollama", "llama", "none"):
|
| 30 |
+
raise HTTPException(status_code=400, detail="provider must be 'gemini', 'ollama', 'llama', or 'none'")
|
| 31 |
+
|
| 32 |
+
if provider == "gemini" and not os.getenv("GOOGLE_API_KEY"):
|
| 33 |
raise HTTPException(status_code=500, detail="GOOGLE_API_KEY not configured")
|
| 34 |
|
| 35 |
+
if provider == "ollama" and not os.getenv("OLLAMA_MODEL"):
|
| 36 |
+
raise HTTPException(status_code=500, detail="OLLAMA_MODEL not configured")
|
| 37 |
+
|
| 38 |
+
if provider == "llama":
|
| 39 |
+
base_url = os.getenv("LLAMA_API_BASE", LLAMA_API_BASE_DEFAULT)
|
| 40 |
+
try:
|
| 41 |
+
resp = httpx.get(f"{base_url}/models", timeout=5)
|
| 42 |
+
resp.raise_for_status()
|
| 43 |
+
except Exception:
|
| 44 |
+
raise HTTPException(status_code=503, detail=f"llama.cpp server not reachable at {base_url}")
|
| 45 |
+
|
| 46 |
contents = await file.read()
|
| 47 |
try:
|
| 48 |
+
result = parse_rfq_pdf(contents, provider=provider)
|
| 49 |
return result
|
| 50 |
except Exception as e:
|
| 51 |
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
|
| 53 |
+
@app.get("/providers")
|
| 54 |
+
def list_providers():
|
| 55 |
+
llama_base = os.getenv("LLAMA_API_BASE", LLAMA_API_BASE_DEFAULT)
|
| 56 |
+
llama_model = os.getenv("LLAMA_MODEL", LLAMA_MODEL_DEFAULT)
|
| 57 |
+
llama_available = False
|
| 58 |
+
try:
|
| 59 |
+
resp = httpx.get(f"{llama_base}/models", timeout=3)
|
| 60 |
+
llama_available = resp.status_code == 200
|
| 61 |
+
except Exception:
|
| 62 |
+
pass
|
| 63 |
+
|
| 64 |
+
return {
|
| 65 |
+
"providers": [
|
| 66 |
+
{
|
| 67 |
+
"id": "gemini",
|
| 68 |
+
"available": bool(os.getenv("GOOGLE_API_KEY")),
|
| 69 |
+
"config": {"model": "gemini-2.5-pro"},
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"id": "ollama",
|
| 73 |
+
"available": bool(os.getenv("OLLAMA_MODEL")),
|
| 74 |
+
"config": {
|
| 75 |
+
"model": os.getenv("OLLAMA_MODEL", ""),
|
| 76 |
+
"host": os.getenv("OLLAMA_HOST", "http://localhost:11434"),
|
| 77 |
+
},
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"id": "llama",
|
| 81 |
+
"available": llama_available,
|
| 82 |
+
"config": {
|
| 83 |
+
"base_url": llama_base,
|
| 84 |
+
"model": llama_model,
|
| 85 |
+
},
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"id": "none",
|
| 89 |
+
"available": True,
|
| 90 |
+
"config": {},
|
| 91 |
+
},
|
| 92 |
+
]
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
|
| 96 |
@app.get("/health")
|
| 97 |
def health():
|
| 98 |
return {"status": "ok"}
|
requirements.txt
CHANGED
|
@@ -3,4 +3,7 @@ uvicorn
|
|
| 3 |
python-multipart
|
| 4 |
pdfplumber
|
| 5 |
python-dotenv
|
| 6 |
-
google-genai
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
python-multipart
|
| 4 |
pdfplumber
|
| 5 |
python-dotenv
|
| 6 |
+
google-genai
|
| 7 |
+
ollama
|
| 8 |
+
openai
|
| 9 |
+
httpx
|
rfq_parser.py
CHANGED
|
@@ -5,10 +5,15 @@ import io
|
|
| 5 |
import re
|
| 6 |
from google import genai
|
| 7 |
from google.genai import types
|
|
|
|
|
|
|
| 8 |
|
| 9 |
GEMINI_MODEL = "gemini-2.5-pro"
|
|
|
|
|
|
|
| 10 |
|
| 11 |
_client = None
|
|
|
|
| 12 |
|
| 13 |
PLACEHOLDER_PATTERNS = ["click or tap", "click here", "enter text", "type here"]
|
| 14 |
SKIP_DESCS = {
|
|
@@ -223,6 +228,44 @@ def _get_genai_client():
|
|
| 223 |
return _client
|
| 224 |
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
def _clean(cell):
|
| 227 |
return str(cell).replace("\n", " ").strip() if cell else ""
|
| 228 |
|
|
@@ -468,8 +511,8 @@ def extract_line_items(pdf_bytes):
|
|
| 468 |
return items
|
| 469 |
|
| 470 |
|
| 471 |
-
def _extract_line_items_from_llm(full_text,
|
| 472 |
-
if
|
| 473 |
return []
|
| 474 |
|
| 475 |
system_prompt = (
|
|
@@ -482,20 +525,25 @@ def _extract_line_items_from_llm(full_text, use_gemini: bool = True):
|
|
| 482 |
"If no line items are found, return []. RETURN JSON ARRAY ONLY, no markdown, no preamble."
|
| 483 |
)
|
| 484 |
try:
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
if isinstance(result, list):
|
| 497 |
-
# Apply local rule-based categorisation as a safety net in case
|
| 498 |
-
# the LLM returns an empty or generic category string.
|
| 499 |
for item in result:
|
| 500 |
if not item.get("category") or item["category"] in ("string", ""):
|
| 501 |
item["category"] = determine_item_category(
|
|
@@ -670,7 +718,7 @@ def _extract_structure_rule_based(full_text: str) -> dict:
|
|
| 670 |
}
|
| 671 |
|
| 672 |
|
| 673 |
-
def parse_rfq_pdf(pdf_bytes,
|
| 674 |
full_text = ""
|
| 675 |
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
|
| 676 |
total_pages = len(pdf.pages)
|
|
@@ -683,7 +731,7 @@ def parse_rfq_pdf(pdf_bytes, use_gemini: bool = True):
|
|
| 683 |
full_text += f"\n--- Page {p_idx + 1} ---\n{text}"
|
| 684 |
|
| 685 |
# --- Main document structure extraction ---
|
| 686 |
-
if
|
| 687 |
system_prompt = """You are an expert RFQ Parser. Extract data from the RFQ text into the exact JSON structure below.
|
| 688 |
|
| 689 |
JSON OUTPUT STRUCTURE:
|
|
@@ -714,17 +762,23 @@ def parse_rfq_pdf(pdf_bytes, use_gemini: bool = True):
|
|
| 714 |
}
|
| 715 |
"""
|
| 716 |
try:
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
except Exception:
|
| 729 |
llm_data = {"title": "Error Parsing", "description": "", "sections": [], "fields": []}
|
| 730 |
else:
|
|
@@ -739,8 +793,7 @@ def parse_rfq_pdf(pdf_bytes, use_gemini: bool = True):
|
|
| 739 |
]
|
| 740 |
|
| 741 |
if not valid_items:
|
| 742 |
-
|
| 743 |
-
valid_items = _extract_line_items_from_llm(full_text, use_gemini=use_gemini)
|
| 744 |
|
| 745 |
return {
|
| 746 |
"title": llm_data.get("title", "RFQ Document"),
|
|
@@ -748,5 +801,5 @@ def parse_rfq_pdf(pdf_bytes, use_gemini: bool = True):
|
|
| 748 |
"sections": llm_data.get("sections", []),
|
| 749 |
"line_items": valid_items,
|
| 750 |
"fields": llm_data.get("fields", []),
|
| 751 |
-
"
|
| 752 |
}
|
|
|
|
| 5 |
import re
|
| 6 |
from google import genai
|
| 7 |
from google.genai import types
|
| 8 |
+
import ollama as ollama_sdk
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
GEMINI_MODEL = "gemini-2.5-pro"
|
| 12 |
+
LLAMA_API_BASE_DEFAULT = "http://localhost:8000/v1"
|
| 13 |
+
LLAMA_MODEL_DEFAULT = "Moonlight-16B-A3B-Instruct-Q4_K_M.gguf"
|
| 14 |
|
| 15 |
_client = None
|
| 16 |
+
_llama_client = None
|
| 17 |
|
| 18 |
PLACEHOLDER_PATTERNS = ["click or tap", "click here", "enter text", "type here"]
|
| 19 |
SKIP_DESCS = {
|
|
|
|
| 228 |
return _client
|
| 229 |
|
| 230 |
|
| 231 |
+
def _get_llama_client():
|
| 232 |
+
global _llama_client
|
| 233 |
+
if _llama_client is None:
|
| 234 |
+
base_url = os.environ.get("LLAMA_API_BASE", LLAMA_API_BASE_DEFAULT)
|
| 235 |
+
_llama_client = OpenAI(base_url=base_url, api_key="not-needed")
|
| 236 |
+
return _llama_client
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def _call_llama(system_prompt: str, user_content: str) -> str:
|
| 240 |
+
client = _get_llama_client()
|
| 241 |
+
model = os.environ.get("LLAMA_MODEL", LLAMA_MODEL_DEFAULT)
|
| 242 |
+
response = client.chat.completions.create(
|
| 243 |
+
model=model,
|
| 244 |
+
messages=[
|
| 245 |
+
{"role": "system", "content": system_prompt},
|
| 246 |
+
{"role": "user", "content": user_content},
|
| 247 |
+
],
|
| 248 |
+
temperature=0,
|
| 249 |
+
)
|
| 250 |
+
return response.choices[0].message.content
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def _call_ollama(system_prompt: str, user_content: str) -> str:
|
| 254 |
+
model = os.environ.get("OLLAMA_MODEL", "llama3")
|
| 255 |
+
host = os.environ.get("OLLAMA_HOST", "http://localhost:11434")
|
| 256 |
+
client = ollama_sdk.Client(host=host)
|
| 257 |
+
response = client.chat(
|
| 258 |
+
model=model,
|
| 259 |
+
messages=[
|
| 260 |
+
{"role": "system", "content": system_prompt},
|
| 261 |
+
{"role": "user", "content": user_content},
|
| 262 |
+
],
|
| 263 |
+
format="json",
|
| 264 |
+
options={"temperature": 0},
|
| 265 |
+
)
|
| 266 |
+
return response.message.content
|
| 267 |
+
|
| 268 |
+
|
| 269 |
def _clean(cell):
|
| 270 |
return str(cell).replace("\n", " ").strip() if cell else ""
|
| 271 |
|
|
|
|
| 511 |
return items
|
| 512 |
|
| 513 |
|
| 514 |
+
def _extract_line_items_from_llm(full_text, provider: str = "gemini"):
|
| 515 |
+
if provider == "none":
|
| 516 |
return []
|
| 517 |
|
| 518 |
system_prompt = (
|
|
|
|
| 525 |
"If no line items are found, return []. RETURN JSON ARRAY ONLY, no markdown, no preamble."
|
| 526 |
)
|
| 527 |
try:
|
| 528 |
+
if provider == "ollama":
|
| 529 |
+
raw = _call_ollama(system_prompt, full_text[:30000])
|
| 530 |
+
elif provider == "llama":
|
| 531 |
+
raw = _call_llama(system_prompt, full_text[:30000])
|
| 532 |
+
else:
|
| 533 |
+
client = _get_genai_client()
|
| 534 |
+
response = client.models.generate_content(
|
| 535 |
+
model=GEMINI_MODEL,
|
| 536 |
+
contents=full_text[:30000],
|
| 537 |
+
config=types.GenerateContentConfig(
|
| 538 |
+
system_instruction=system_prompt,
|
| 539 |
+
response_mime_type="application/json",
|
| 540 |
+
temperature=0,
|
| 541 |
+
),
|
| 542 |
+
)
|
| 543 |
+
raw = response.text
|
| 544 |
+
|
| 545 |
+
result = json.loads(raw)
|
| 546 |
if isinstance(result, list):
|
|
|
|
|
|
|
| 547 |
for item in result:
|
| 548 |
if not item.get("category") or item["category"] in ("string", ""):
|
| 549 |
item["category"] = determine_item_category(
|
|
|
|
| 718 |
}
|
| 719 |
|
| 720 |
|
| 721 |
+
def parse_rfq_pdf(pdf_bytes, provider: str = "gemini"):
|
| 722 |
full_text = ""
|
| 723 |
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
|
| 724 |
total_pages = len(pdf.pages)
|
|
|
|
| 731 |
full_text += f"\n--- Page {p_idx + 1} ---\n{text}"
|
| 732 |
|
| 733 |
# --- Main document structure extraction ---
|
| 734 |
+
if provider in ("gemini", "ollama", "llama"):
|
| 735 |
system_prompt = """You are an expert RFQ Parser. Extract data from the RFQ text into the exact JSON structure below.
|
| 736 |
|
| 737 |
JSON OUTPUT STRUCTURE:
|
|
|
|
| 762 |
}
|
| 763 |
"""
|
| 764 |
try:
|
| 765 |
+
if provider == "ollama":
|
| 766 |
+
raw = _call_ollama(system_prompt + "\nRETURN JSON ONLY.", full_text[:30000])
|
| 767 |
+
elif provider == "llama":
|
| 768 |
+
raw = _call_llama(system_prompt + "\nRETURN JSON ONLY.", full_text[:30000])
|
| 769 |
+
else:
|
| 770 |
+
client = _get_genai_client()
|
| 771 |
+
response = client.models.generate_content(
|
| 772 |
+
model=GEMINI_MODEL,
|
| 773 |
+
contents=full_text[:30000],
|
| 774 |
+
config=types.GenerateContentConfig(
|
| 775 |
+
system_instruction=system_prompt + "\nRETURN JSON ONLY.",
|
| 776 |
+
response_mime_type="application/json",
|
| 777 |
+
temperature=0,
|
| 778 |
+
),
|
| 779 |
+
)
|
| 780 |
+
raw = response.text
|
| 781 |
+
llm_data = json.loads(raw)
|
| 782 |
except Exception:
|
| 783 |
llm_data = {"title": "Error Parsing", "description": "", "sections": [], "fields": []}
|
| 784 |
else:
|
|
|
|
| 793 |
]
|
| 794 |
|
| 795 |
if not valid_items:
|
| 796 |
+
valid_items = _extract_line_items_from_llm(full_text, provider=provider)
|
|
|
|
| 797 |
|
| 798 |
return {
|
| 799 |
"title": llm_data.get("title", "RFQ Document"),
|
|
|
|
| 801 |
"sections": llm_data.get("sections", []),
|
| 802 |
"line_items": valid_items,
|
| 803 |
"fields": llm_data.get("fields", []),
|
| 804 |
+
"provider": provider,
|
| 805 |
}
|