Spaces:
Build error
Build error
Download app.py from maxhuang33/cv_analysis: direct link, hf CLI and curl.
- Browser
- Download file 4.82 kB
-
https://huggingface.co/spaces/maxhuang33/cv_analysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/maxhuang33/cv_analysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/maxhuang33/cv_analysis/resolve/main/app.py
4.82 kB
| import gradio as gr | |
| import chromadb | |
| from chromadb.config import Settings | |
| from openai import OpenAI | |
| import os | |
| from dotenv import load_dotenv | |
| # 加载 OpenAI Key | |
| load_dotenv() | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # 初始化 ChromaDB | |
| chroma_client = chromadb.Client(Settings( | |
| persist_directory="./getting-started" | |
| )) | |
| collection = chroma_client.get_or_create_collection("cv_analysis_knowledge") | |
| # 文档分块函数 | |
| def split_document(text, max_length=1000): | |
| lines = text.split('\n') | |
| chunks, current = [], "" | |
| for line in lines: | |
| if len(current) + len(line) > max_length and current: | |
| chunks.append(current.strip()) | |
| current = line | |
| else: | |
| current += ("\n" if current else "") + line | |
| if current.strip(): | |
| chunks.append(current.strip()) | |
| return chunks | |
| # 初始化知识库(只需运行一次) | |
| def init_knowledge_base(): | |
| file_path = "documennts/entry-level.txt" | |
| if not os.path.exists(file_path): | |
| return | |
| with open(file_path, "r", encoding="utf-8") as f: | |
| content = f.read() | |
| chunks = split_document(content) | |
| ids = [f"entry_level_{i+1}" for i in range(len(chunks))] | |
| metadatas = [{"source": "entry-level.txt", "chunk_index": i+1, "total_chunks": len(chunks)} for i in range(len(chunks))] | |
| # 检查是否已导入 | |
| if collection.count() < len(chunks): | |
| collection.add( | |
| ids=ids, | |
| documents=chunks, | |
| metadatas=metadatas | |
| ) | |
| # RAG 检索函数 | |
| def rag_query(query): | |
| results = collection.query( | |
| query_texts=[query], | |
| n_results=3 | |
| ) | |
| docs = results["documents"][0] | |
| return "\n\n".join(docs) | |
| # OpenAI 生成答案 | |
| def openai_answer(query, context): | |
| prompt = f""" | |
| 你是一名智能简历分析助手。以下是用户输入的问题或简历内容,以及相关的求职知识库信息。请结合这些内容,结构化地分析用户的职业背景与求职目标,并提供清晰、专业的建议。 | |
| 请按照以下四个维度进行分析: | |
| 1. 🎯 目标岗位分析: | |
| - 概述该岗位的核心职责和所需能力 | |
| - 分析用户的经历在多大程度上与岗位要求匹配 | |
| - 如果用户没有明确岗位目标,请根据其简历推测一个合适的岗位并进行分析 | |
| 2. ✅ 现有优势: | |
| - 概括用户简历中已有的技能、经验、项目亮点 | |
| - 特别指出与目标岗位最契合的部分 | |
| 3. 🧠 关键技能评分(1~5星): | |
| - 列出目标岗位常见的5个关键技能(如沟通能力、数据分析、Python 等) | |
| - 根据简历内容对每项技能打分,并简要解释理由 | |
| 4. 🧭 职业路径规划建议: | |
| - 根据用户当前背景,提出短期可行的提升路径(如学习课程、实践建议、补充经历) | |
| - 提出中长期的职业发展方向和建议的转型轨迹(如果适用) | |
| 请用清晰的小标题、要点式条理表达,回答内容以中文输出。 | |
| 知识库内容: | |
| {context} | |
| 用户问题或简历输入: | |
| {query} | |
| """ | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", # 或 gpt-4 | |
| messages=[ | |
| {"role": "system", "content": "你是一名专业的简历分析和职业咨询AI助手。"}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0.3, | |
| max_tokens=1024 | |
| ) | |
| return response.choices[0].message.content | |
| # Gradio 主函数 | |
| def agent(query): | |
| if not query.strip(): | |
| return "请输入您的问题或简历内容。" | |
| try: | |
| context = rag_query(query) | |
| answer = openai_answer(query, context) | |
| return answer | |
| except Exception as e: | |
| return f"抱歉,处理您的请求时出现错误:{str(e)}" | |
| # 初始化知识库 | |
| init_knowledge_base() | |
| # 创建 Gradio 界面 | |
| demo = gr.Interface( | |
| fn=agent, | |
| inputs=[ | |
| gr.Textbox( | |
| lines=6, | |
| label="📝 请输入您的简历内容或求职问题", | |
| placeholder="例如:我是一名计算机科学毕业生,有Python和机器学习经验,想找Web3相关的工作..." | |
| ) | |
| ], | |
| outputs=[ | |
| gr.Markdown( | |
| label="🤖 AI 智能分析与建议" | |
| ) | |
| ], | |
| title="🎯 CV 智能分析与职业规划助手", | |
| description="基于 RAG + OpenAI 的智能简历分析系统,为您提供个性化的职业发展建议", | |
| examples=[ | |
| ["我是一名计算机科学毕业生,有Python和机器学习经验,想找Web3相关的工作"], | |
| ["我有3年前端开发经验,熟悉React和Vue,想转行做产品经理"], | |
| ["我是应届毕业生,专业是金融,对区块链技术很感兴趣,想进入加密货币行业"] | |
| ], | |
| theme=gr.themes.Soft() | |
| ) | |
| # 启动应用 | |
| if __name__ == "__main__": | |
| demo.launch() |