Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import urllib.parse
|
| 7 |
+
import base64 # Diperlukan untuk client-side API call
|
| 8 |
+
|
| 9 |
+
# --- KONFIGURASI API KEY (TETAP SAMA) ---
|
| 10 |
+
API_CONFIGURED = False
|
| 11 |
+
try:
|
| 12 |
+
api_key = os.environ.get('GEMINI_API_KEY')
|
| 13 |
+
if api_key:
|
| 14 |
+
genai.configure(api_key=api_key)
|
| 15 |
+
model = genai.GenerativeModel('gemini-pro-latest')
|
| 16 |
+
API_CONFIGURED = True
|
| 17 |
+
print("β
Konfigurasi API dan model berhasil.")
|
| 18 |
+
else:
|
| 19 |
+
print("π Secret 'GEMINI_API_KEY' tidak ditemukan.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"π Terjadi error saat inisialisasi: {e}")
|
| 22 |
+
|
| 23 |
+
# --- FUNGSI-FUNGSI UTAMA ---
|
| 24 |
+
|
| 25 |
+
def ekstrak_teks_dari_pdf(path_file_pdf):
|
| 26 |
+
try:
|
| 27 |
+
with fitz.open(path_file_pdf) as dokumen:
|
| 28 |
+
teks_lengkap = "".join(halaman.get_text() for halaman in dokumen)
|
| 29 |
+
return teks_lengkap
|
| 30 |
+
except Exception as e:
|
| 31 |
+
raise gr.Error(f"Gagal membaca file PDF: {e}")
|
| 32 |
+
|
| 33 |
+
def generate_search_links(keywords):
|
| 34 |
+
if not keywords:
|
| 35 |
+
return {}
|
| 36 |
+
keywords_encoded = urllib.parse.quote_plus(keywords)
|
| 37 |
+
keywords_hyphenated = keywords.lower().replace(" ", "-").replace("(", "").replace(")", "")
|
| 38 |
+
links = {
|
| 39 |
+
"LinkedIn": f"https://www.linkedin.com/jobs/search/?keywords={keywords_encoded}&location=Indonesia",
|
| 40 |
+
"JobStreet": f"https://www.jobstreet.co.id/id/job-search/{keywords_hyphenated}-jobs/",
|
| 41 |
+
"Glints": f"https://glints.com/id/opportunities/jobs/explore?keyword={keywords_encoded}",
|
| 42 |
+
"Indeed": f"https://id.indeed.com/jobs?q={keywords_encoded}",
|
| 43 |
+
"Google Jobs": f"https://www.google.com/search?q={keywords_encoded}+jobs+in+Indonesia&ibp=htl;jobs"
|
| 44 |
+
}
|
| 45 |
+
return links
|
| 46 |
+
|
| 47 |
+
def analyze_career_path(cv_file):
|
| 48 |
+
"""Fungsi utama pipeline: Analisis CV -> Buat Laporan JSON -> Buat Link -> Gabungkan."""
|
| 49 |
+
if not API_CONFIGURED:
|
| 50 |
+
raise gr.Error("API Key Gemini belum terkonfigurasi. Periksa Logs aplikasi.")
|
| 51 |
+
if cv_file is None:
|
| 52 |
+
raise gr.Error("Mohon upload file CV (PDF) Anda.")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
print("--- Memulai Proses Analisis Karir ---")
|
| 56 |
+
|
| 57 |
+
teks_cv = ekstrak_teks_dari_pdf(cv_file.name)
|
| 58 |
+
if not teks_cv:
|
| 59 |
+
raise gr.Error("PDF kosong atau tidak dapat dibaca.")
|
| 60 |
+
print("β
Teks berhasil diekstrak.")
|
| 61 |
+
|
| 62 |
+
print("2. Mengirim permintaan analisis karir ke Gemini...")
|
| 63 |
+
prompt_analisis_karir = f"""
|
| 64 |
+
Anda adalah seorang "Career Analyst AI". Baca teks CV dan buat laporan peluang karir dalam format JSON.
|
| 65 |
+
Teks CV: --- {teks_cv} ---
|
| 66 |
+
Struktur JSON yang diinginkan:
|
| 67 |
+
- "jabatan_ideal": Jabatan paling ideal untuk kandidat.
|
| 68 |
+
- "alasan_kecocokan": Array (list) berisi 3-4 poin MENGAPA kandidat cocok.
|
| 69 |
+
- "deskripsi_pekerjaan": Array (list) berisi 5 poin deskripsi pekerjaan umum.
|
| 70 |
+
- "potensi_karir": Array (list) berisi 3-4 jalur pengembangan karir.
|
| 71 |
+
- "kisaran_gaji": Objek JSON berisi estimasi gaji untuk level "junior", "mid_level", dan "senior".
|
| 72 |
+
- "kelebihan_tambahan": Array (list) berisi 1-2 poin saran atau kelebihan unik kandidat.
|
| 73 |
+
Pastikan output hanya berupa JSON saja.
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
generation_config = genai.types.GenerationConfig(response_mime_type="application/json")
|
| 77 |
+
response = model.generate_content(prompt_analisis_karir, generation_config=generation_config)
|
| 78 |
+
|
| 79 |
+
response_json = json.loads(response.text)
|
| 80 |
+
print("β
Laporan karir komprehensif berhasil diterima.")
|
| 81 |
+
|
| 82 |
+
print("3. Membuat tautan pencarian dari hasil analisis...")
|
| 83 |
+
keywords_from_analysis = response_json.get("jabatan_ideal", "")
|
| 84 |
+
search_links = generate_search_links(keywords_from_analysis)
|
| 85 |
+
|
| 86 |
+
# ==================================================================
|
| 87 |
+
# PERUBAHAN PENTING: Menambahkan link ke dalam JSON
|
| 88 |
+
# ==================================================================
|
| 89 |
+
response_json["tautan_pencarian"] = search_links
|
| 90 |
+
print("β
Tautan pencarian ditambahkan ke JSON.")
|
| 91 |
+
|
| 92 |
+
print("--- Proses Selesai ---")
|
| 93 |
+
# Mengembalikan dictionary/JSON mentah
|
| 94 |
+
return response_json
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"π ERROR DALAM FUNGSI ANALISIS: {e}")
|
| 98 |
+
raise gr.Error(f"Terjadi kesalahan: {e}")
|
| 99 |
+
|
| 100 |
+
# --- MEMBUAT INTERFACE GRADIO ---
|
| 101 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 102 |
+
gr.Markdown("# π API Analis Peluang Karir Personal")
|
| 103 |
+
gr.Markdown("Antarmuka ini dapat digunakan untuk pengujian. Endpoint API publik tersedia di `/run/predict` untuk integrasi ke website Anda.")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
cv_pdf = gr.File(label="Upload CV (PDF) untuk Uji Coba", file_types=[".pdf"])
|
| 108 |
+
analyze_button = gr.Button("π Analisis Karir Saya", variant="primary")
|
| 109 |
+
|
| 110 |
+
with gr.Column(scale=2):
|
| 111 |
+
# Menggunakan gr.JSON untuk menampilkan dan menghasilkan data JSON sebagai output API
|
| 112 |
+
output_analysis = gr.JSON(label="Output JSON dari API")
|
| 113 |
+
|
| 114 |
+
analyze_button.click(
|
| 115 |
+
fn=analyze_career_path,
|
| 116 |
+
inputs=[cv_pdf],
|
| 117 |
+
outputs=[output_analysis],
|
| 118 |
+
show_progress='full'
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|