cv / app.py
saherPervaiz's picture
Update app.py
9480219 verified
raw
history blame
2.52 kB
# app.py
import os
import gradio as gr
from text_extractor import extract_text_from_file
from embedder import get_embeddings
from vector_store import create_faiss_index, search_similar_cvs
from groq_api import summarize_match
# Global storage
cv_texts = []
cv_names = []
cv_vectors = []
faiss_index = None
def upload_cvs(files):
global cv_texts, cv_names, cv_vectors, faiss_index
try:
cv_texts = [extract_text_from_file(f.name) for f in files]
cv_names = [f.name for f in files]
cv_vectors = get_embeddings(cv_texts)
import numpy as np
if cv_vectors is None or np.array(cv_vectors).size == 0:
return "❌ No valid CVs extracted or embedded."
faiss_index = create_faiss_index(cv_vectors)
return f"βœ… Uploaded and indexed {len(files)} CVs."
except Exception as e:
return f"❌ Error during upload: {e}"
def match_jd(jd_text):
global faiss_index
try:
if not faiss_index:
return "❌ Please upload CVs first."
if not jd_text.strip():
return "❌ Job description is empty."
jd_vector = get_embeddings([jd_text])[0]
top_k_indices = search_similar_cvs(jd_vector, faiss_index, k=3)
# Get readable file names and snippets of CVs
matched_names = [os.path.basename(cv_names[i]) for i in top_k_indices]
matched_texts = [cv_texts[i][:500] for i in top_k_indices] # limit for Groq prompt
summary = summarize_match(jd_text, matched_names, matched_texts)
return f"βœ… Top Matches:\n{matched_names}\n\nπŸ“ Summary:\n{summary}"
except Exception as e:
return f"❌ Error during matching: {e}"
def clear_data():
global cv_texts, cv_names, cv_vectors, faiss_index
cv_texts, cv_names, cv_vectors, faiss_index = [], [], [], None
return "🧹 Data cleared."
# Gradio Interfaces
iface = gr.Interface(
fn=match_jd,
inputs=[gr.Textbox(lines=10, label="Paste Job Description")],
outputs="text",
title="CV Matcher with Groq",
description="Upload CVs, enter a Job Description, and get top matches and summary."
)
upload = gr.Interface(
fn=upload_cvs,
inputs=gr.File(file_types=[".pdf", ".docx"], file_count="multiple"),
outputs="text",
title="Upload CVs"
)
clear = gr.Interface(fn=clear_data, inputs=[], outputs="text", title="Reset Data")
app = gr.TabbedInterface([upload, iface, clear], ["Upload CVs", "Match JD", "Clear Data"])
if __name__ == "__main__":
app.launch()