Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
+
import openai
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
# --- 共用:清理履歷內容 ---
|
| 10 |
+
def clean_text(text):
|
| 11 |
+
text = re.sub(r'^\s*\d+\s*$', '', text, flags=re.MULTILINE)
|
| 12 |
+
text = re.sub(r'\n\s*\n+', '\n\n', text)
|
| 13 |
+
text = re.sub(r'[ \t]+', ' ', text)
|
| 14 |
+
return text.strip()
|
| 15 |
+
|
| 16 |
+
# --- 擷取 PDF 文字 ---
|
| 17 |
+
def extract_text_from_pdf(file):
|
| 18 |
+
doc = fitz.open(file.name)
|
| 19 |
+
text = ""
|
| 20 |
+
for page in doc:
|
| 21 |
+
text += page.get_text()
|
| 22 |
+
doc.close()
|
| 23 |
+
return clean_text(text)
|
| 24 |
+
|
| 25 |
+
# --- GPT:履歷分析(STAR 法則)---
|
| 26 |
+
def analyze_resume(resume_text):
|
| 27 |
+
prompt = f"""
|
| 28 |
+
你是一位專業的履歷健檢與職涯輔導顧問,請協助我以下幾件事:
|
| 29 |
+
|
| 30 |
+
1. 根據下方履歷內容,幫我整理出:
|
| 31 |
+
- 學歷背景
|
| 32 |
+
- 技能與工具
|
| 33 |
+
- 工作經歷與專案經歷
|
| 34 |
+
|
| 35 |
+
2. 對於每一段經歷,請用 STAR 法則整理:
|
| 36 |
+
- S(情境)
|
| 37 |
+
- T(任務)
|
| 38 |
+
- A(行動)
|
| 39 |
+
- R(成果)
|
| 40 |
+
|
| 41 |
+
3. 指出這份履歷的優勢,以及可以優化的建議
|
| 42 |
+
|
| 43 |
+
4. 推薦幾個可能適合的職缺方向(例如:前端工程師、資料分析師、產品實習生等)
|
| 44 |
+
|
| 45 |
+
這是履歷內容:
|
| 46 |
+
------------------------
|
| 47 |
+
{resume_text}
|
| 48 |
+
------------------------
|
| 49 |
+
|
| 50 |
+
請以條列式輸出,分段清楚。
|
| 51 |
+
"""
|
| 52 |
+
response = openai.ChatCompletion.create(
|
| 53 |
+
model="gpt-4",
|
| 54 |
+
messages=[
|
| 55 |
+
{"role": "system", "content": "你是一位履歷健檢顧問"},
|
| 56 |
+
{"role": "user", "content": prompt}
|
| 57 |
+
],
|
| 58 |
+
temperature=0.7,
|
| 59 |
+
max_tokens=1500
|
| 60 |
+
)
|
| 61 |
+
content = response['choices'][0]['message']['content']
|
| 62 |
+
return content.replace("**", "").replace("* ", "• ").replace("*", "").strip()
|
| 63 |
+
|
| 64 |
+
# --- GPT:撰寫個人自傳 ---
|
| 65 |
+
def generate_autobiography(resume_text):
|
| 66 |
+
prompt = f"""
|
| 67 |
+
請根據以下履歷內容,幫我撰寫一段約 300 字的個人自傳,口吻自然、自信,強調個人特質、學習歷程與職涯目標,可作為履歷中的「自我介紹」使用:
|
| 68 |
+
|
| 69 |
+
履歷內容:
|
| 70 |
+
------------------------
|
| 71 |
+
{resume_text}
|
| 72 |
+
------------------------
|
| 73 |
+
"""
|
| 74 |
+
response = openai.ChatCompletion.create(
|
| 75 |
+
model="gpt-4",
|
| 76 |
+
messages=[
|
| 77 |
+
{"role": "system", "content": "你是一位履歷與自傳輔導顧問"},
|
| 78 |
+
{"role": "user", "content": prompt}
|
| 79 |
+
],
|
| 80 |
+
temperature=0.7,
|
| 81 |
+
max_tokens=800
|
| 82 |
+
)
|
| 83 |
+
return response['choices'][0]['message']['content'].strip()
|
| 84 |
+
|
| 85 |
+
# --- Gradio UI ---
|
| 86 |
+
with gr.Blocks(title="求職小幫手 AI") as demo:
|
| 87 |
+
gr.Markdown("## 🧑💼 求職小幫手 AI\n請上傳你的履歷 PDF,我們將幫你分析履歷並生成個人自傳。")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
pdf_input = gr.File(label="📄 上傳履歷 PDF(限 PDF)", file_types=[".pdf"])
|
| 91 |
+
extract_btn = gr.Button("⬇️ 擷取與清理內容")
|
| 92 |
+
|
| 93 |
+
resume_textbox = gr.Textbox(label="📄 擷取後的履歷純文字", lines=20, interactive=False)
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
analyze_btn = gr.Button("🧠 分析履歷(STAR 法則)")
|
| 97 |
+
bio_btn = gr.Button("📝 撰寫個人自傳")
|
| 98 |
+
|
| 99 |
+
analysis_output = gr.Textbox(label="✨ GPT 履歷分析結果", lines=25)
|
| 100 |
+
bio_output = gr.Textbox(label="✍️ GPT 產生的個人自傳", lines=15)
|
| 101 |
+
|
| 102 |
+
extract_btn.click(fn=extract_text_from_pdf, inputs=pdf_input, outputs=resume_textbox)
|
| 103 |
+
analyze_btn.click(fn=analyze_resume, inputs=resume_textbox, outputs=analysis_output)
|
| 104 |
+
bio_btn.click(fn=generate_autobiography, inputs=resume_textbox, outputs=bio_output)
|
| 105 |
+
|
| 106 |
+
demo.launch()
|