Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 1. Define the Model ID
|
| 7 |
+
MODEL_ID = "LlamaFactoryAI/cv-job-description-matching"
|
| 8 |
+
|
| 9 |
+
# 2. Load the Model and Tokenizer
|
| 10 |
+
# NOTE: Check the model's Hugging Face page for the exact class to use (e.g., AutoModel, AutoModelForSequenceClassification, etc.)
|
| 11 |
+
# If the model is very large, consider using quantization or the 'device_map="auto"' argument.
|
| 12 |
+
try:
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 14 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
# Handle potential loading errors gracefully
|
| 17 |
+
print(f"Error loading model: {e}")
|
| 18 |
+
# You might set model to None or use a placeholder here
|
| 19 |
+
|
| 20 |
+
# 3. Define the Prediction Function
|
| 21 |
+
def match_cv_jd(cv_text: str, job_description_text: str):
|
| 22 |
+
"""
|
| 23 |
+
Tokenizes the inputs, runs the model, and returns the prediction/score.
|
| 24 |
+
"""
|
| 25 |
+
if not model or not tokenizer:
|
| 26 |
+
return "Model loading failed. Check logs for details."
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Prepare the input for the matching task
|
| 30 |
+
inputs = tokenizer(
|
| 31 |
+
cv_text,
|
| 32 |
+
job_description_text,
|
| 33 |
+
return_tensors="pt",
|
| 34 |
+
padding=True,
|
| 35 |
+
truncation=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Get model output (logits)
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = model(**inputs)
|
| 41 |
+
|
| 42 |
+
# Process the output: This part depends heavily on the model's exact task
|
| 43 |
+
# Assuming it's a classification model that outputs a score/probability for a match
|
| 44 |
+
probabilities = torch.softmax(outputs.logits, dim=1).tolist()[0]
|
| 45 |
+
|
| 46 |
+
# Placeholder logic: Find the index of the highest probability
|
| 47 |
+
# The labels (e.g., match, no_match) would be in model.config.id2label
|
| 48 |
+
predicted_index = torch.argmax(outputs.logits, dim=1).item()
|
| 49 |
+
prediction = model.config.id2label[predicted_index]
|
| 50 |
+
score = max(probabilities) * 100
|
| 51 |
+
|
| 52 |
+
return f"Prediction: **{prediction}**\nConfidence Score: **{score:.2f}%**"
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"An error occurred during prediction: {e}"
|
| 56 |
+
|
| 57 |
+
# 4. Create the Gradio Interface
|
| 58 |
+
demo = gr.Interface(
|
| 59 |
+
fn=match_cv_jd,
|
| 60 |
+
inputs=[
|
| 61 |
+
gr.Textbox(label="Paste CV Text", lines=10),
|
| 62 |
+
gr.Textbox(label="Paste Job Description Text", lines=10)
|
| 63 |
+
],
|
| 64 |
+
outputs="markdown",
|
| 65 |
+
title="LlamaFactory CV-Job Matching Demo",
|
| 66 |
+
description=f"Interactive demo for the {MODEL_ID} model to assess CV/JD compatibility."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
# Note: On Hugging Face Spaces, `demo.launch()` should not include `share=True` or `server_name/port` arguments.
|
| 71 |
+
demo.launch()
|