Download src/app.py from Enternal1/docker: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/spaces/Enternal1/docker/resolve/main/src/app.py
- Command line
-
hf download hf://spaces/Enternal1/docker/src/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Enternal1/docker/resolve/main/src/app.py
3.88 kB
| 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 "" | |
| 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 | |
| def health(): | |
| return {"status": "ok", "catalog_items": catalog_items, "embedding_model": "all-MiniLM-L6-v2"} | |
| 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) | |
| 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) | |