Mangesh223 commited on
Commit
82a80f9
·
verified ·
1 Parent(s): 7c4e758

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -56
app.py CHANGED
@@ -1,55 +1,15 @@
1
  import gradio as gr
2
- from transformers import pipeline, BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer
3
  import PyPDF2
4
  import io
5
  import re
6
  import json
7
  import os
8
- import gc
9
- import torch
10
  from huggingface_hub import login
11
  from dotenv import load_dotenv
12
 
13
  # --- Configuration --- #
14
  load_dotenv()
15
- login(token=os.getenv("HF_TOKEN"))
16
-
17
- # Quantization config (only used if CUDA is available)
18
- quant_config = BitsAndBytesConfig(
19
- load_in_4bit=True,
20
- bnb_4bit_compute_dtype=torch.float16,
21
- bnb_4bit_quant_type="nf4"
22
- )
23
-
24
- # Check if CUDA is available
25
- cuda_available = torch.cuda.is_available()
26
-
27
- # Load tokenizer and model
28
- tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
29
- if cuda_available:
30
- # Use quantization if CUDA is available
31
- model = AutoModelForCausalLM.from_pretrained(
32
- "mistralai/Mistral-7B-Instruct-v0.3",
33
- device_map="auto",
34
- quantization_config=quant_config,
35
- torch_dtype=torch.float16
36
- )
37
- else:
38
- # Fall back to full precision (no quantization) if no CUDA
39
- model = AutoModelForCausalLM.from_pretrained(
40
- "mistralai/Mistral-7B-Instruct-v0.3",
41
- device_map="cpu", # Explicitly set to CPU
42
- torch_dtype=torch.float16
43
- )
44
-
45
- # Initialize pipeline with preloaded model and tokenizer
46
- analyzer = pipeline(
47
- "text-generation",
48
- model=model,
49
- tokenizer=tokenizer,
50
- device_map="auto" if cuda_available else "cpu", # Match model device
51
- torch_dtype=torch.float16
52
- )
53
 
54
  # Skills set for faster lookups
55
  GENERAL_SKILLS = {
@@ -107,8 +67,8 @@ def calculate_scores(resume_text, job_desc=None):
107
 
108
  return scores, min(100, sum(scores.values()))
109
 
110
- def analyze_resume(pdf_file, job_desc=None):
111
- """Optimized analysis with memory management"""
112
  resume_text = extract_text_from_pdf(pdf_file)
113
  scores, total_score = calculate_scores(resume_text, job_desc)
114
 
@@ -119,12 +79,8 @@ def analyze_resume(pdf_file, job_desc=None):
119
  Return ONLY valid JSON without markdown:"""
120
 
121
  try:
122
- result = analyzer(
123
- prompt,
124
- max_new_tokens=300,
125
- do_sample=False
126
- )[0]["generated_text"]
127
-
128
  return {
129
  "score": {"total": total_score, "breakdown": scores},
130
  "analysis": json.loads(result),
@@ -134,11 +90,19 @@ def analyze_resume(pdf_file, job_desc=None):
134
  return {"error": str(e)}
135
 
136
  # --- Gradio Interface --- #
137
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
138
- gr.Markdown("""
139
- <h1 style='font-size: 18px'>🎯 Resume Analyzer</h1>
140
- <p><i>Optimized for memory efficiency</i></p>
141
- """)
 
 
 
 
 
 
 
 
142
 
143
  with gr.Row():
144
  with gr.Column(scale=1):
@@ -146,12 +110,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
146
  gr.File(label="PDF Resume", type="binary"),
147
  gr.Textbox(label="Job Description (Optional)", lines=3)
148
  ]
149
- # Removed examples that required sample.pdf
150
  with gr.Column(scale=2):
151
  output = gr.JSON(label="Analysis")
152
 
153
  inputs[0].upload(
154
- fn=analyze_resume,
155
  inputs=inputs,
156
  outputs=output,
157
  queue=True
 
1
  import gradio as gr
 
2
  import PyPDF2
3
  import io
4
  import re
5
  import json
6
  import os
 
 
7
  from huggingface_hub import login
8
  from dotenv import load_dotenv
9
 
10
  # --- Configuration --- #
11
  load_dotenv()
12
+ login(token=os.getenv("HF_TOKEN"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Skills set for faster lookups
15
  GENERAL_SKILLS = {
 
67
 
68
  return scores, min(100, sum(scores.values()))
69
 
70
+ def analyze_resume(pdf_file, job_desc=None, inference_fn=None):
71
+ """Analyze resume using Together AI inference"""
72
  resume_text = extract_text_from_pdf(pdf_file)
73
  scores, total_score = calculate_scores(resume_text, job_desc)
74
 
 
79
  Return ONLY valid JSON without markdown:"""
80
 
81
  try:
82
+ # Use Together AI inference function passed from gr.load
83
+ result = inference_fn(prompt)
 
 
 
 
84
  return {
85
  "score": {"total": total_score, "breakdown": scores},
86
  "analysis": json.loads(result),
 
90
  return {"error": str(e)}
91
 
92
  # --- Gradio Interface --- #
93
+ with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
94
+ with gr.Sidebar():
95
+ gr.Markdown("# Resume Analyzer with Mistral-7B")
96
+ gr.Markdown("Powered by mistralai/Mistral-7B-Instruct-v0.3 via Together AI API. Sign in to use.")
97
+ button = gr.LoginButton("Sign in")
98
+
99
+ # Load Mistral-7B from Together AI
100
+ inference = gr.load(
101
+ "models/mistralai/Mistral-7B-Instruct-v0.3",
102
+ accept_token=button,
103
+ provider="together",
104
+ _js="() => ({ max_new_tokens: 300, do_sample: false })" # Pass generation params
105
+ )
106
 
107
  with gr.Row():
108
  with gr.Column(scale=1):
 
110
  gr.File(label="PDF Resume", type="binary"),
111
  gr.Textbox(label="Job Description (Optional)", lines=3)
112
  ]
 
113
  with gr.Column(scale=2):
114
  output = gr.JSON(label="Analysis")
115
 
116
  inputs[0].upload(
117
+ fn=lambda pdf, job_desc: analyze_resume(pdf, job_desc, inference),
118
  inputs=inputs,
119
  outputs=output,
120
  queue=True