File size: 3,884 Bytes
dd55845 ab80e7a dd55845 ab80e7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import os
from typing import Dict, Any
import uvicorn
from fastapi import FastAPI, Body
from fastapi.responses import HTMLResponse
import requests
from bs4 import BeautifulSoup
from .shl.indexer import load_catalog
from .shl.recommender import Recommender
app = FastAPI()
catalog_items = 0
rec = None
def jd_from_url(u: str) -> str:
hdrs = {"User-Agent": "Mozilla/5.0"}
try:
r = requests.get(u, headers=hdrs, timeout=30)
soup = BeautifulSoup(r.text, "html.parser")
for s in soup(["script", "style", "noscript"]):
s.decompose()
txt = " ".join(soup.get_text(separator=" ").split())
return txt[:5000]
except Exception:
return ""
@app.on_event("startup")
def on_startup():
global catalog_items
catalog_items = len(load_catalog())
auto_build = os.getenv("AUTO_BUILD", "false").lower() == "true"
if auto_build and catalog_items == 0:
try:
from .shl.scraper import run as scrape_run
from .shl.indexer import index as index_run
scrape_run()
index_run()
catalog_items = len(load_catalog())
except Exception:
pass
@app.get("/health")
def health():
return {"status": "ok", "catalog_items": catalog_items, "embedding_model": "all-MiniLM-L6-v2"}
@app.get("/", response_class=HTMLResponse)
def index():
html = """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>SHL Recommendations</title>
</head>
<body>
<h1>SHL Recommendations</h1>
<div>
<label>Input Type</label>
<select id="input_type">
<option value="text">text</option>
<option value="jd_text">jd_text</option>
<option value="jd_url">jd_url</option>
</select>
</div>
<div>
<textarea id="query" rows="8" cols="80" placeholder="Enter query or JD text or URL"></textarea>
</div>
<button onclick="send()">Recommend</button>
<table border="1" id="tbl"><thead><tr><th>Name</th><th>URL</th><th>Type</th></tr></thead><tbody></tbody></table>
<script>
async function send(){
const input_type = document.getElementById('input_type').value;
const query = document.getElementById('query').value;
const r = await fetch('/recommend',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({input_type,query,top_k:10})});
const j = await r.json();
const tb = document.querySelector('#tbl tbody');
tb.innerHTML = '';
for(const it of j.items){
const tr = document.createElement('tr');
const td1 = document.createElement('td'); td1.textContent = it.name;
const td2 = document.createElement('td'); const a = document.createElement('a'); a.href=it.url; a.textContent=it.url; a.target='_blank'; td2.appendChild(a);
const td3 = document.createElement('td'); td3.textContent = it.type || '';
tr.appendChild(td1); tr.appendChild(td2); tr.appendChild(td3);
tb.appendChild(tr);
}
}
</script>
</body>
</html>
"""
return HTMLResponse(content=html)
@app.post("/recommend")
def recommend(payload: Dict[str, Any] = Body(...)):
global rec
if rec is None:
rec = Recommender()
input_type = payload.get("input_type", "text")
query = payload.get("query", "") or ""
top_k = int(payload.get("top_k", 10))
if input_type == "jd_url":
q = jd_from_url(query)
if q:
query = q
if input_type == "jd_text":
query = query
items = rec.recommend(query=query, k=max(5, min(10, top_k)))
out = [{"name": it["name"], "url": it["url"], "type": it.get("type", "")} for it in items]
return {"items": out}
if __name__ == "__main__":
port = int(os.getenv("PORT", "8000"))
uvicorn.run("src.app:app", host="0.0.0.0", port=port, reload=False)
|