Spaces:
Runtime error
Runtime error
Download agent/extractor_e.py from shensuming/gemini: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/shensuming/gemini/resolve/main/agent/extractor_e.py
- Command line
-
hf download hf://spaces/shensuming/gemini/agent/extractor_e.py
-
curl -L -o extractor_e.py https://huggingface.co/spaces/shensuming/gemini/resolve/main/agent/extractor_e.py
3.48 kB
| """ | |
| 信息提取器 | |
| 读 raw_pages.json,正则+DeepSeek提取人名/职位/邮箱 | |
| 输出 candidates.json | |
| """ | |
| import json | |
| import re | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from openai import OpenAI | |
| from agent.config_e import DEEPSEEK_KEY, MODEL_FAST, DATA_DIR | |
| client = OpenAI(api_key=DEEPSEEK_KEY, base_url="https://api.deepseek.com") | |
| RAW_PAGES_FILE = DATA_DIR / "raw_pages.json" | |
| CANDIDATES_FILE = DATA_DIR / "candidates.json" | |
| def quick_extract(text): | |
| """正则快速提取邮箱""" | |
| emails = re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text) | |
| return list(set(e.lower() for e in emails)) | |
| def deep_extract(html_snippet, ocr_text): | |
| """DeepSeek提取结构化的姓名+职位+邮箱""" | |
| content = html_snippet[:3000] + "\n---OCR---\n" + ocr_text[:1000] | |
| prompt = f"""从以下页面内容中提取所有人的姓名、职位、邮箱。 | |
| 内容:{content} | |
| 输出严格JSON数组:[{{"name":"姓名","title":"职位","email":"邮箱"}}] | |
| 没有提取到则输出[]。只输出JSON。""" | |
| try: | |
| resp = client.chat.completions.create( | |
| model=MODEL_FAST, | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.1, max_tokens=4000 | |
| ) | |
| raw = resp.choices[0].message.content.strip() | |
| if raw.startswith("```"): | |
| raw = raw.split("\n", 1)[1] | |
| if raw.endswith("```"): | |
| raw = raw[:-3] | |
| return json.loads(raw) | |
| except: | |
| return [] | |
| def extract_all(): | |
| with open(RAW_PAGES_FILE, "r", encoding="utf-8") as f: | |
| pages = json.load(f) | |
| all_candidates = [] | |
| seen_emails = set() | |
| for i, page in enumerate(pages): | |
| name = page.get("name", "") | |
| html = page.get("html", "") | |
| ocr = page.get("ocr_text", "") | |
| combined = html + "\n" + ocr | |
| print(f"[extractor {i+1}/{len(pages)}] {name}") | |
| # 第一遍:正则快速提取 | |
| quick_emails = quick_extract(combined) | |
| # 第二遍:DeepSeek结构化提取 | |
| deep_results = deep_extract(html, ocr) | |
| # 合并 | |
| for item in deep_results: | |
| email = item.get("email", "").strip().lower() | |
| if email and email not in seen_emails and "@" in email: | |
| seen_emails.add(email) | |
| all_candidates.append({ | |
| "name": item.get("name", "").strip(), | |
| "title": item.get("title", "").strip(), | |
| "email": email, | |
| "company": name, | |
| "source_url": page.get("url", ""), | |
| "category": page.get("category", "") | |
| }) | |
| # 正则提取的邮箱如果不在deep结果里,补上 | |
| for em in quick_emails: | |
| if em not in seen_emails and em not in seen_emails: | |
| seen_emails.add(em) | |
| all_candidates.append({ | |
| "name": "", | |
| "title": "", | |
| "email": em, | |
| "company": name, | |
| "source_url": page.get("url", ""), | |
| "category": page.get("category", "") | |
| }) | |
| with open(CANDIDATES_FILE, "w", encoding="utf-8") as f: | |
| json.dump(all_candidates, f, ensure_ascii=False, indent=2) | |
| print(f"[extractor] 完成,共{len(all_candidates)}个候选人 → {CANDIDATES_FILE}") | |
| if __name__ == "__main__": | |
| extract_all() |