Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516171819201 unmodified line2223242526272829303132333435363738394041424344451 unmodified line4748495051525354555657585960616263642 unmodified lines6768697071727374757677783 unmodified lines828384858687# --- 変更1: Files の type を "filepath" にし、ハンドラをファイルパス対応へ ---with gr.Row():in_files = gr.Files(label="レジュメ類 (PDF/画像/Word/テキスト) 複数可",file_count="multiple",file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp", ".docx", ".txt"],type="filepath", # ← "file" から修正)candidate_id = gr.Textbox(label="候補者ID(任意。未入力なら自動生成)")# --- 変更2: out_skills は schema 周りの不具合回避のため JSON → Code に ---with gr.Tab("抽出スキル"):out_skills = gr.Code(label="スキル一覧(JSON)") # ← gr.JSON から修正# --- 変更3: ハンドラの files 取り扱いを「パス」前提に修正 ---def process_resumes(files, candidate_id: str, additional_notes: str = ""):if not files:raise gr.Error("少なくとも1ファイルをアップロードしてください。")1 unmodified linepartial_records = []raw_texts = []for path in files: # ← UploadedFile ではなく filepath の配列with open(path, "rb") as fh:raw_bytes = fh.read()fname = os.path.basename(path)filetype = detect_filetype(fname, raw_bytes)# 1) テキスト抽出if filetype in {"pdf", "image"}:text = extract_text_with_openai(raw_bytes, filename=fname, filetype=filetype)else:base_text = load_doc_text(filetype, raw_bytes)text = extract_text_with_openai(base_text.encode("utf-8"), filename=fname, filetype="txt")raw_texts.append({"filename": fname, "text": text})# 2) 構造化structured = structure_with_openai(text)normalized = normalize_resume({"work_experience": structured.get("work_experience_raw", ""),"education": structured.get("education_raw", ""),1 unmodified line"skills": ", ".join(structured.get("skills_list", [])),})partial_records.append({"source": fname,"text": text,"structured": structured,"normalized": normalized,})# ...(中略:ロジックはそのまま)...# 8) まとめresult_json = {"candidate_id": candidate_id or hashlib.sha256(merged_text.encode("utf-8")).hexdigest()[:16],"files": [os.path.basename(p) for p in files], # ← fname ���列へ"merged": merged,"skills": skills,"quality_score": score,2 unmodified lines"notes": additional_notes,}# ...(Datasets 保存処理はそのまま)...# 返り値:out_skills を Code にしたので JSON 文字列で返すreturn (json.dumps(result_json, ensure_ascii=False, indent=2),json.dumps(skills, ensure_ascii=False, indent=2), # ← 文字列化json.dumps(score, ensure_ascii=False, indent=2),summaries["300chars"],summaries["100chars"],3 unmodified lines)# --- 変更4: HF Spaces での起動を安定させるため launch の引数を明示 ---if __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=7860)123456789101112131415161718192021222324251 unmodified line2728293031323334353637383940414243444546474849501 unmodified line52535455565758596061626364656667686970717273747576777879808182838485868788892 unmodified lines92939495969798991001011021031041051061071081091101111121131141151163 unmodified lines120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import osimport ioimport jsonimport hashlibimport gradio as grfrom pipelines.openai_ingest import (extract_text_with_openai,structure_with_openai,summarize_with_openai,)from pipelines.parsing import normalize_resumefrom pipelines.merge import merge_normalized_recordsfrom pipelines.skills import extract_skillsfrom pipelines.anonymize import anonymize_text, render_anonymized_pdffrom pipelines.scoring import compute_quality_scorefrom pipelines.storage import persist_to_hffrom pipelines.utils import detect_filetype, load_doc_textAPP_TITLE = "候補者インテーク & レジュメ標準化(OpenAI版)"def process_resumes(files, candidate_id: str, additional_notes: str = ""):if not files:raise gr.Error("少なくとも1ファイルをアップロードしてください。")1 unmodified linepartial_records = []raw_texts = []for f in files:# gr.Files(type="binary") では .read() / .name が利用可能raw_bytes = f.read()filetype = detect_filetype(f.name, raw_bytes)# 1) テキスト抽出:画像/PDFはOpenAI Vision OCR、docx/txtは生文面+OpenAI整形if filetype in {"pdf", "image"}:text = extract_text_with_openai(raw_bytes, filename=f.name, filetype=filetype)else:base_text = load_doc_text(filetype, raw_bytes)# 生テキストをそのままOpenAIへ渡し、軽く整形した全文を返すtext = extract_text_with_openai(base_text.encode("utf-8"), filename=f.name, filetype="txt")raw_texts.append({"filename": f.name, "text": text})# 2) OpenAIでセクション構造化structured = structure_with_openai(text)# 念のためルールベース正規化も適用(期間抽出など補助)normalized = normalize_resume({"work_experience": structured.get("work_experience_raw", ""),"education": structured.get("education_raw", ""),1 unmodified line"skills": ", ".join(structured.get("skills_list", [])),})partial_records.append({"source": f.name,"text": text,"structured": structured,"normalized": normalized,})# 3) 統合(複数ファイル→1候補者)merged = merge_normalized_records([r["normalized"] for r in partial_records])# 4) スキル抽出(辞書/正規表現)merged_text = "\n\n".join([r["text"] for r in partial_records])skills = extract_skills(merged_text, {"work_experience": merged.get("raw_sections", {}).get("work_experience", ""),"education": merged.get("raw_sections", {}).get("education", ""),"certifications": merged.get("raw_sections", {}).get("certifications", ""),"skills": ", ".join(merged.get("skills", [])),})# 5) 匿名化anonymized_text, anon_map = anonymize_text(merged_text)anon_pdf_bytes = render_anonymized_pdf(anonymized_text)# 6) 品質スコアscore = compute_quality_score(merged_text, merged)# 7) 要約(300/100/1文)summaries = summarize_with_openai(merged_text)# 8) 構造化出力result_json = {"candidate_id": candidate_id or hashlib.sha256(merged_text.encode("utf-8")).hexdigest()[:16],"files": [f.name for f in files],"merged": merged,"skills": skills,"quality_score": score,2 unmodified lines"notes": additional_notes,}# 9) HF Datasets 保存dataset_repo = os.environ.get("DATASET_REPO")commit_info = Noneif dataset_repo:file_hash = result_json["candidate_id"]commit_info = persist_to_hf(dataset_repo=dataset_repo,record=result_json,anon_pdf_bytes=anon_pdf_bytes,parquet_path=f"candidates/{file_hash}.parquet",json_path=f"candidates/{file_hash}.json",pdf_path=f"candidates/{file_hash}.anon.pdf",)anon_pdf = (result_json["candidate_id"] + ".anon.pdf", anon_pdf_bytes)return (json.dumps(result_json, ensure_ascii=False, indent=2),skills,json.dumps(score, ensure_ascii=False, indent=2),summaries["300chars"],summaries["100chars"],3 unmodified lines)with gr.Blocks(title=APP_TITLE) as demo:gr.Markdown(f"# {APP_TITLE}\n複数ファイルを統合→OpenAIで読み込み/構造化/要約→匿名化→Datasets保存")with gr.Row():in_files = gr.Files(label="レジュメ類 (PDF/画像/Word/テキスト) 複数可",file_count="multiple",file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp", ".docx", ".txt"],type="binary", # Gradio 4.44系:'binary' or 'filepath')candidate_id = gr.Textbox(label="候補者ID(任意。未入力なら自動生成)")notes = gr.Textbox(label="補足メモ(任意)", lines=3)run_btn = gr.Button("実行")with gr.Tab("構造化JSON"):out_json = gr.Code(label="統合出力 (JSON)")with gr.Tab("抽出スキル"):out_skills = gr.JSON(label="スキル一覧")with gr.Tab("品質スコア"):out_score = gr.Code(label="品質評価")with gr.Tab("要約 (300/100/1文)"):out_sum_300 = gr.Textbox(label="300字要約")out_sum_100 = gr.Textbox(label="100字要約")out_sum_1 = gr.Textbox(label="1文要約")with gr.Tab("匿名PDF"):out_pdf = gr.File(label="匿名PDFダウンロード")with gr.Tab("Datasets 保存ログ"):out_commit = gr.Code(label="コミット情報")run_btn.click(process_resumes,inputs=[in_files, candidate_id, notes],outputs=[out_json, out_skills, out_score, out_sum_300, out_sum_100, out_sum_1, out_pdf, out_commit],)if __name__ == "__main__":# Hugging Face Spaces では share=True は不要/非推奨demo.launch()
app.py+115-35