Mangesh223 commited on
Commit
908d88b
·
verified ·
1 Parent(s): aac2ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -147
app.py CHANGED
@@ -1,151 +1,9 @@
 
1
  import gradio as gr
2
- import PyPDF2
3
- import io
4
- import os
5
- from dotenv import load_dotenv
6
 
7
- def extract_text_from_pdf(pdf_file):
8
- """
9
- Robust PDF text extraction with comprehensive error handling
10
-
11
- Args:
12
- pdf_file (str/bytes): PDF file path or bytes
13
-
14
- Returns:
15
- str: Extracted text from PDF
16
- """
17
- if pdf_file is None:
18
- raise ValueError("No PDF file uploaded")
19
-
20
- try:
21
- # Handle different input types
22
- if isinstance(pdf_file, str):
23
- with open(pdf_file, 'rb') as f:
24
- file_bytes = f.read()
25
- elif isinstance(pdf_file, bytes):
26
- file_bytes = pdf_file
27
- else:
28
- raise TypeError(f"Unsupported file type: {type(pdf_file)}")
29
-
30
- # Advanced PDF text extraction
31
- pdf_reader = PyPDF2.PdfReader(io.BytesIO(file_bytes))
32
-
33
- # Extract text from all pages, handle potential encoding issues
34
- pages_text = []
35
- for page in pdf_reader.pages:
36
- try:
37
- page_text = page.extract_text() or ""
38
- pages_text.append(page_text.strip())
39
- except Exception as page_error:
40
- print(f"Error extracting page text: {page_error}")
41
-
42
- # Join pages, handle empty extraction
43
- full_text = "\n".join(pages_text)
44
-
45
- if not full_text.strip():
46
- raise ValueError("No text could be extracted from the PDF")
47
-
48
- # Limit text to prevent overwhelming AI
49
- return full_text[:15000] # Increased limit for more comprehensive analysis
50
-
51
- except Exception as e:
52
- raise ValueError(f"PDF Extraction Error: {str(e)}")
53
 
54
- def prepare_resume_prompt(resume_text, job_description=None):
55
- """
56
- Prepare a structured, clear prompt for AI analysis
57
-
58
- Args:
59
- resume_text (str): Extracted resume text
60
- job_description (str, optional): Job description for context
61
-
62
- Returns:
63
- str: Formatted prompt for AI analysis
64
- """
65
- prompt = f"""Professional Resume Analysis:
66
 
67
- Resume Content:
68
- {resume_text[:10000]}
69
-
70
- {'Job Description: ' + job_description if job_description else 'No specific job description provided'}
71
-
72
- Instructions for Analysis:
73
- 1. Perform a comprehensive assessment of the resume
74
- 2. Evaluate professional skills, experience, and potential
75
- 3. Provide a structured JSON response with:
76
- - Overall Score (0-100)
77
- - Skill Match Percentage
78
- - Key Strengths
79
- - Areas for Improvement
80
- - Potential Red Flags
81
- - Recommended Next Steps
82
-
83
- Output Format (JSON):
84
- {{
85
- "total_score": int,
86
- "skill_match_percentage": int,
87
- "strengths": [str],
88
- "improvements": [str],
89
- "red_flags": [str],
90
- "recommended_actions": [str]
91
- }}"""
92
-
93
- return prompt
94
-
95
- def analyze_resume(pdf_file, job_description=None):
96
- """
97
- Main resume analysis function
98
-
99
- Args:
100
- pdf_file (bytes): Uploaded PDF file
101
- job_description (str, optional): Job description for context
102
-
103
- Returns:
104
- tuple: Extracted text and AI analysis
105
- """
106
- try:
107
- # Extract text from PDF
108
- resume_text = extract_text_from_pdf(pdf_file)
109
-
110
- # Prepare prompt for AI
111
- ai_prompt = prepare_resume_prompt(resume_text, job_description)
112
-
113
- # Note: Replace this with actual Mistral-7B inference
114
- # This is a placeholder - you'll need to integrate your actual AI model
115
- print("AI Prompt Prepared. Replace this with actual model inference.")
116
-
117
- return resume_text, {
118
- "total_score": 75,
119
- "skill_match_percentage": 80,
120
- "strengths": ["Robust text extraction", "Structured prompt generation"],
121
- "improvements": ["Integrate actual AI model inference"],
122
- "red_flags": [],
123
- "recommended_actions": ["Connect Mistral-7B model"]
124
- }
125
-
126
- except Exception as e:
127
- return str(e), {
128
- "error": str(e),
129
- "total_score": 0,
130
- "skill_match_percentage": 0
131
- }
132
-
133
- # Gradio Interface
134
- with gr.Blocks() as demo:
135
- with gr.Row():
136
- with gr.Column(scale=1):
137
- pdf_input = gr.File(label="Upload Resume PDF", type="binary")
138
- job_desc_input = gr.Textbox(label="Job Description (Optional)", lines=3)
139
- analyze_btn = gr.Button("Analyze Resume")
140
-
141
- with gr.Column(scale=2):
142
- extracted_text = gr.Textbox(label="Extracted Text", lines=10)
143
- analysis_output = gr.JSON(label="AI Analysis")
144
-
145
- analyze_btn.click(
146
- fn=analyze_resume,
147
- inputs=[pdf_input, job_desc_input],
148
- outputs=[extracted_text, analysis_output]
149
- )
150
-
151
- demo.launch(share=True)
 
1
+ from transformers import pipeline
2
  import gradio as gr
 
 
 
 
3
 
4
+ pipe = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def analyze(text):
7
+ return pipe(f"Analyze this resume: {text}")[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
8
 
9
+ gr.Interface(fn=analyze, inputs="text", outputs="text").launch()