Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install dependensi yang dibutuhkan oleh Hugging Face Spaces
|
| 2 |
+
#!pip install -q gradio google-generativeai pymupdf
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
+
import json
|
| 8 |
+
import os
|
| 9 |
+
import urllib.parse
|
| 10 |
+
|
| 11 |
+
# --- KONFIGURASI API KEY DARI HUGGING FACE SECRETS ---
|
| 12 |
+
API_CONFIGURED = False
|
| 13 |
+
try:
|
| 14 |
+
api_key = os.environ.get('GEMINI_API_KEY')
|
| 15 |
+
if api_key:
|
| 16 |
+
genai.configure(api_key=api_key)
|
| 17 |
+
model = genai.GenerativeModel('gemini-pro-latest')
|
| 18 |
+
API_CONFIGURED = True
|
| 19 |
+
print("β
Konfigurasi API dan model berhasil.")
|
| 20 |
+
else:
|
| 21 |
+
print("π Secret 'GEMINI_API_KEY' tidak ditemukan.")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"π Terjadi error saat inisialisasi: {e}")
|
| 24 |
+
|
| 25 |
+
# --- FUNGSI-FUNGSI UTAMA ---
|
| 26 |
+
|
| 27 |
+
def ekstrak_teks_dari_pdf(path_file_pdf):
|
| 28 |
+
try:
|
| 29 |
+
with fitz.open(path_file_pdf) as dokumen:
|
| 30 |
+
teks_lengkap = "".join(halaman.get_text() for halaman in dokumen)
|
| 31 |
+
return teks_lengkap
|
| 32 |
+
except Exception as e:
|
| 33 |
+
raise gr.Error(f"Gagal membaca file PDF: {e}")
|
| 34 |
+
|
| 35 |
+
def generate_search_links(keywords):
|
| 36 |
+
"""Membuat tautan pencarian dari kata kunci untuk berbagai platform."""
|
| 37 |
+
# Encode keywords agar aman digunakan di URL (misal: spasi menjadi %20)
|
| 38 |
+
keywords_encoded = urllib.parse.quote_plus(keywords)
|
| 39 |
+
|
| 40 |
+
# Format untuk situs yang menggunakan tanda hubung (-)
|
| 41 |
+
keywords_hyphenated = keywords.lower().replace(" ", "-")
|
| 42 |
+
|
| 43 |
+
links = {
|
| 44 |
+
"LinkedIn": f"https://www.linkedin.com/jobs/search/?keywords={keywords_encoded}&location=Indonesia",
|
| 45 |
+
"JobStreet": f"https://www.jobstreet.co.id/id/job-search/{keywords_hyphenated}-jobs/",
|
| 46 |
+
"Glints": f"https://glints.com/id/opportunities/jobs/explore?keyword={keywords_encoded}",
|
| 47 |
+
"Indeed": f"https://id.indeed.com/jobs?q={keywords_encoded}",
|
| 48 |
+
"Google Jobs": f"https://www.google.com/search?q={keywords_encoded}+jobs+in+Indonesia&ibp=htl;jobs"
|
| 49 |
+
}
|
| 50 |
+
return links
|
| 51 |
+
|
| 52 |
+
def format_output_markdown(keywords, links):
|
| 53 |
+
"""Menyusun hasil akhir menjadi string Markdown yang rapi."""
|
| 54 |
+
markdown_links = ""
|
| 55 |
+
for site, url in links.items():
|
| 56 |
+
markdown_links += f"- **[{site}]({url})**\n"
|
| 57 |
+
|
| 58 |
+
return f"""
|
| 59 |
+
### Buka Pintu Wawancara Berikutnya!
|
| 60 |
+
Berdasarkan analisis CV Anda, kami merekomendasikan Anda untuk mencari lowongan dengan kata kunci berikut:
|
| 61 |
+
|
| 62 |
+
**π Kata Kunci Rekomendasi:**
|
| 63 |
+
`{keywords}`
|
| 64 |
+
***
|
| 65 |
+
**π Klik untuk Mencari Lowongan Terbaru:**
|
| 66 |
+
{markdown_links}
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
def recommend_jobs(cv_file):
|
| 70 |
+
"""Fungsi utama yang dijalankan Gradio untuk merekomendasikan pekerjaan."""
|
| 71 |
+
if not API_CONFIGURED:
|
| 72 |
+
raise gr.Error("API Key Gemini belum terkonfigurasi. Periksa Logs aplikasi.")
|
| 73 |
+
if cv_file is None:
|
| 74 |
+
raise gr.Error("Mohon upload file CV (PDF) Anda.")
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
print("--- Memulai Proses Rekomendasi ---")
|
| 78 |
+
|
| 79 |
+
print("1. Mengekstrak teks dari PDF...")
|
| 80 |
+
teks_cv = ekstrak_teks_dari_pdf(cv_file.name)
|
| 81 |
+
if not teks_cv:
|
| 82 |
+
raise gr.Error("PDF kosong atau tidak dapat dibaca.")
|
| 83 |
+
print("β
Teks berhasil diekstrak.")
|
| 84 |
+
|
| 85 |
+
print("2. Mengirim permintaan ekstraksi keyword ke Gemini...")
|
| 86 |
+
prompt_rekomendasi = f"""
|
| 87 |
+
Anda adalah seorang Headhunter AI. Tugas Anda adalah membaca teks CV dan menentukan kata kunci pencarian pekerjaan yang paling ideal untuk kandidat ini.
|
| 88 |
+
|
| 89 |
+
Teks CV:
|
| 90 |
+
---
|
| 91 |
+
{teks_cv}
|
| 92 |
+
---
|
| 93 |
+
|
| 94 |
+
Analisis seluruh pengalaman, keterampilan, dan jabatan. Tentukan 1 (satu) frasa pencarian pekerjaan yang paling relevan dan spesifik.
|
| 95 |
+
Contoh output yang baik: "Senior Backend Developer Python", "Digital Marketing SEO Specialist", "Data Analyst SQL Tableau".
|
| 96 |
+
|
| 97 |
+
Berikan output hanya berupa frasa pencarian tersebut, tanpa teks atau format tambahan.
|
| 98 |
+
"""
|
| 99 |
+
response = model.generate_content(prompt_rekomendasi)
|
| 100 |
+
|
| 101 |
+
# Kata kunci adalah respons mentah dari model
|
| 102 |
+
keywords = response.text.strip()
|
| 103 |
+
print(f"β
Keyword berhasil diekstrak: {keywords}")
|
| 104 |
+
|
| 105 |
+
print("3. Membuat tautan pencarian...")
|
| 106 |
+
links = generate_search_links(keywords)
|
| 107 |
+
|
| 108 |
+
print("4. Menyusun output akhir...")
|
| 109 |
+
final_output = format_output_markdown(keywords, links)
|
| 110 |
+
print("--- Proses Selesai ---")
|
| 111 |
+
return final_output
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"π ERROR DALAM FUNGSI REKOMENDASI: {e}")
|
| 115 |
+
raise gr.Error(f"Terjadi kesalahan: {e}")
|
| 116 |
+
|
| 117 |
+
# --- MEMBUAT INTERFACE GRADIO ---
|
| 118 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 119 |
+
gr.Markdown("# π Aplikasi Rekomendasi Pekerjaan dari CV")
|
| 120 |
+
gr.Markdown("Upload CV Anda, dan biarkan AI menemukan kata kunci pekerjaan yang paling tepat beserta link pencariannya!")
|
| 121 |
+
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column(scale=1):
|
| 124 |
+
cv_pdf = gr.File(label="Upload CV Anda (PDF)", file_types=[".pdf"])
|
| 125 |
+
recommend_button = gr.Button("π Temukan Pekerjaan Saya", variant="primary")
|
| 126 |
+
|
| 127 |
+
with gr.Column(scale=2):
|
| 128 |
+
output_recommendation = gr.Markdown(label="Rekomendasi untuk Anda")
|
| 129 |
+
|
| 130 |
+
recommend_button.click(
|
| 131 |
+
fn=recommend_jobs,
|
| 132 |
+
inputs=[cv_pdf],
|
| 133 |
+
outputs=[output_recommendation],
|
| 134 |
+
show_progress='full'
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
demo.launch()
|