Spaces:
Sleeping
Sleeping
Download KnowledgeService/interviews.py from dellweb3/workos-office: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/KnowledgeService/interviews.py
- Command line
-
hf download hf://spaces/dellweb3/workos-office/KnowledgeService/interviews.py
-
curl -L -o interviews.py https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/KnowledgeService/interviews.py
4.43 kB
| """Offline, immutable, sourced interview cards and labels for the demo tenant.""" | |
| import hashlib | |
| import json | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from sqlalchemy import select | |
| from .models import KnowledgeBase, Grant, LibraryConfig, DocumentInfo, Document, Chunk, DocumentSource, KnowledgeError | |
| from .parsing import extract | |
| BASE_ID='kb-interviews' | |
| def corpus(): | |
| value=json.loads(Path(__file__).with_name('interview-corpus.json').read_text(encoding='utf-8')) | |
| if len({x['id'] for x in value['records']})!=len(value['records']):raise ValueError('duplicate interview IDs') | |
| return value | |
| def seed_interviews(service): | |
| data=corpus() | |
| with service.Session.begin() as db: | |
| if db.get(KnowledgeBase,('demo-tenant',BASE_ID)) is None: | |
| db.add(KnowledgeBase(tenant='demo-tenant',id=BASE_ID,name='科技企业秋招面试经验库',visibility='shared')) | |
| db.add(LibraryConfig(tenant='demo-tenant',base_id=BASE_ID,description=data['notice'],category='秋招与求职',status='enabled',revision=1)) | |
| for uid in ('u-admin','u-lin','u-zhou'):db.add(Grant(tenant='demo-tenant',base_id=BASE_ID,user_id=uid)) | |
| with service.Session() as db: | |
| existing={d.document_id:(d.sha256,db.get(DocumentInfo,d.id)) for d in db.scalars(select(Document).where(Document.tenant=='demo-tenant',Document.base_id==BASE_ID,Document.version=='1'))} | |
| prepared=[] | |
| for r in data['records']: | |
| text='\n'.join(['# '+r['title'],'公司:'+r['company'],'岗位:'+r['role'],'时期:'+r['season'], | |
| '主题:'+'、'.join(r['topics']),'内容核验:'+r['verification'],'导读:'+r['summary'],'原文:'+r['source_url'], | |
| '注意:个人历史经验,不是当前官方招聘政策;自动标注需人工复核。']) | |
| known=existing.get(r['id']) | |
| tags=list(dict.fromkeys([r['company'],r['role'],r['verification'],*r['topics']])) | |
| if known and known[0]==hashlib.sha256(text.encode()).hexdigest() and known[1] is not None:continue | |
| provenance={k:r[k] for k in ('source_url','verification','annotation_status','season','company','role')} | |
| provenance.update(origin_kind='public_interview_reference',origin_label='公开面经原创导读/索引', | |
| author='原文作者(见来源) / Work OS 导读',license='原文保留原作者权利;不收录全文', | |
| retrieved_at=data['retrieved_at'],version='1',currency='historical_reference', | |
| notice=data['notice'],topics=r['topics'],source_content_sha256=r.get('source_content_sha256')) | |
| prepared.append((r,text.encode(),tags,provenance)) | |
| # Commit a validated bundle once, rather than two disk syncs for every card. | |
| with service.lock,service.Session.begin() as db: | |
| for r,content,tags,provenance in prepared: | |
| sha=hashlib.sha256(content).hexdigest() | |
| key='doc-'+hashlib.sha256(json.dumps(['demo-tenant',r['id'],'1']).encode()).hexdigest() | |
| doc=db.get(Document,key) | |
| if doc is not None and doc.sha256!=sha:raise KnowledgeError(409,'DOCUMENT_VERSION_CONFLICT','面试资料同一版本不可替换。') | |
| chunks=extract(r['id']+'.txt',content) | |
| if doc is None: | |
| doc=Document(id=key,tenant='demo-tenant',document_id=r['id'],version='1',base_id=BASE_ID,title=r['title'], | |
| owner=None,workspace=None,source_type='interview_reference',sha256=sha,status='ready', | |
| valid_from=None,valid_to=None,error_code=None,created_at=datetime.now(timezone.utc).isoformat()) | |
| db.add(doc);db.flush() | |
| for ordinal,(locator,text,terms) in enumerate(chunks,1): | |
| db.add(Chunk(id='cite-'+hashlib.sha256((key+':'+str(ordinal)).encode()).hexdigest(), | |
| document_key=key,ordinal=ordinal,locator=locator,text=text,terms=terms)) | |
| service._info(db,doc,r['id']+'.txt',content,chunks) | |
| info=db.get(DocumentInfo,key);info.tags=tags;info.category='秋招面试经验' | |
| source=db.get(DocumentSource,key) | |
| body={**provenance,'content_sha256':sha,'sha256':sha} | |
| if source is None:db.add(DocumentSource(document_key=key,body=body)) | |
| else:source.body=body | |
| def stats(): | |
| data=corpus() | |
| return {k:data[k] for k in ('total','article_read_count','index_only_count','retrieved_at','notice')} | |