Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,90 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# NOTE: If the model loading fails here, the application will crash with a clear error
|
| 12 |
-
# in the logs, which is the correct behavior for a critical failure.
|
| 13 |
try:
|
| 14 |
-
print(f"Loading
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
except Exception as e:
|
| 22 |
-
print(f"FATAL ERROR: Failed to load
|
| 23 |
-
# Re-raise the exception to
|
| 24 |
raise
|
| 25 |
|
| 26 |
-
#
|
| 27 |
def match_cv_jd(cv_text: str, job_description_text: str):
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
return
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
|
| 57 |
-
return f"An error occurred during prediction: {e}. Please check the space logs."
|
| 58 |
|
| 59 |
-
#
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=match_cv_jd,
|
| 62 |
inputs=[
|
|
@@ -65,7 +93,7 @@ demo = gr.Interface(
|
|
| 65 |
],
|
| 66 |
outputs="markdown",
|
| 67 |
title="LlamaFactory CV-Job Matching Demo",
|
| 68 |
-
description=f"Interactive demo for the {
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from peft import PeftModel
|
| 5 |
import os
|
| 6 |
+
from accelerate import Accelerator
|
|
|
|
| 7 |
|
| 8 |
+
# --- Model Configuration ---
|
| 9 |
+
# The LoRA adapter model on the Hugging Face Hub
|
| 10 |
+
LORA_MODEL_ID = "LlamaFactoryAI/cv-job-description-matching"
|
| 11 |
+
# The base model it was fine-tuned from (found in the model card)
|
| 12 |
+
BASE_MODEL_ID = "akjindal53244/Llama-3.1-Storm-8B"
|
| 13 |
|
| 14 |
+
# --- Model Loading ---
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
+
print(f"Loading base model: {BASE_MODEL_ID}...")
|
| 17 |
|
| 18 |
+
# 1. Load the BASE model
|
| 19 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
BASE_MODEL_ID,
|
| 21 |
+
torch_dtype=torch.bfloat16, # Recommended dtype for Llama models
|
| 22 |
+
device_map="auto" # Use accelerate to manage device placement
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# 2. Load the base model's tokenizer
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 27 |
+
|
| 28 |
+
# 3. Load the LoRA adapter weights on top of the base model
|
| 29 |
+
model = PeftModel.from_pretrained(base_model, LORA_MODEL_ID)
|
| 30 |
+
|
| 31 |
+
# 4. Merge the LoRA weights into the base model (for faster inference)
|
| 32 |
+
model = model.merge_and_unload()
|
| 33 |
+
print("LoRA adapter merged and model ready successfully.")
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
+
print(f"FATAL ERROR: Failed to load LoRA model. Details: {e}")
|
| 37 |
+
# Re-raise the exception to crash the space if loading fails
|
| 38 |
raise
|
| 39 |
|
| 40 |
+
# 5. Define the Prediction/Matching Function
|
| 41 |
def match_cv_jd(cv_text: str, job_description_text: str):
|
| 42 |
"""
|
| 43 |
+
Constructs the chat prompt, tokenizes, runs the LLM, and extracts the JSON output.
|
| 44 |
"""
|
| 45 |
try:
|
| 46 |
+
# Define the exact prompt structure used for fine-tuning (from model card)
|
| 47 |
+
messages = [
|
| 48 |
+
{"role": "system", "content": """
|
| 49 |
+
You will receive a CV and a job description. Your task is to output a structured JSON format
|
| 50 |
+
that includes the following: 1. matching_analysis, 2. description, 3. score (0-100), 4. recommendation.
|
| 51 |
+
Your output must be in JSON format.
|
| 52 |
+
"""},
|
| 53 |
+
{"role": "user", "content": f"<CV> {cv_text} </CV>\n<job_description> {job_description_text} </job_description>"},
|
| 54 |
+
]
|
| 55 |
|
| 56 |
+
# Apply the chat template and generate tokens
|
| 57 |
+
inputs = tokenizer.apply_chat_template(
|
| 58 |
+
messages,
|
| 59 |
+
add_generation_prompt=True,
|
| 60 |
+
return_tensors="pt"
|
| 61 |
+
).to(model.device) # Move to the correct device
|
| 62 |
+
|
| 63 |
+
# Generate the response
|
| 64 |
+
outputs = model.generate(
|
| 65 |
+
inputs,
|
| 66 |
+
max_new_tokens=512,
|
| 67 |
+
do_sample=False,
|
| 68 |
+
pad_token_id=tokenizer.eos_token_id # Important for generation
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# Decode the generated text and clean up the prompt
|
| 72 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 73 |
|
| 74 |
+
# Extract the assistant's response (assuming the output starts after the user's prompt)
|
| 75 |
+
assistant_response_start = generated_text.rfind("assistant\n")
|
| 76 |
+
if assistant_response_start != -1:
|
| 77 |
+
raw_json_output = generated_text[assistant_response_start + len("assistant\n"):].strip()
|
| 78 |
+
else:
|
| 79 |
+
raw_json_output = generated_text
|
| 80 |
|
| 81 |
+
# For display, just return the raw text output
|
| 82 |
+
return f"### Model Output (JSON format expected):\n\n```json\n{raw_json_output}\n```"
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
return f"An error occurred during prediction: {e}. If the model is too large, consider upgrading hardware."
|
|
|
|
| 86 |
|
| 87 |
+
# 6. Create the Gradio Interface
|
| 88 |
demo = gr.Interface(
|
| 89 |
fn=match_cv_jd,
|
| 90 |
inputs=[
|
|
|
|
| 93 |
],
|
| 94 |
outputs="markdown",
|
| 95 |
title="LlamaFactory CV-Job Matching Demo",
|
| 96 |
+
description=f"Interactive demo for the {LORA_MODEL_ID} model (LoRA on {BASE_MODEL_ID})."
|
| 97 |
)
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|