mployd-engineering commited on
Commit
9c46a12
·
verified ·
1 Parent(s): 0bbef61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -51
app.py CHANGED
@@ -1,71 +1,33 @@
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()
 
1
+ # app.py
 
 
 
2
 
3
  # 1. Define the Model ID
4
  MODEL_ID = "LlamaFactoryAI/cv-job-description-matching"
5
 
6
  # 2. Load the Model and Tokenizer
7
+ # --- MAKE SURE THESE LINES ARE NOT COMMENTED OUT ---
8
+ # NOTE: Verify AutoModelForSequenceClassification is the correct class
9
  try:
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
11
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
12
  except Exception as e:
13
+ # If the model cannot load (e.g., OOM), this will print the error in the logs
14
+ print(f"FATAL ERROR: Failed to load model {MODEL_ID}. Details: {e}")
15
+ raise # Re-raise the exception to stop the space from running with a broken app
16
 
17
  # 3. Define the Prediction Function
18
  def match_cv_jd(cv_text: str, job_description_text: str):
19
  """
20
  Tokenizes the inputs, runs the model, and returns the prediction/score.
21
  """
22
+ # *** REMOVE THIS CHECK (if not model or not tokenizer) ***
23
+ # if not model or not tokenizer:
24
+ # return "Model loading failed. Check logs for details."
25
+
26
  try:
27
+ # ... the rest of the prediction logic ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
+ # Keep the try/except INSIDE the function to catch runtime errors
31
  return f"An error occurred during prediction: {e}"
32
 
33
+ # ... rest of the Gradio code ...