Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
from transformers import pipeline
|
4 |
+
from werkzeug.utils import secure_filename
|
5 |
+
from pdf2image import convert_from_path
|
6 |
+
import pytesseract
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Initialize Flask app
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
# Set upload folder
|
13 |
+
UPLOAD_FOLDER = 'uploads'
|
14 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
15 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
16 |
+
|
17 |
+
# Load AI Pipelines
|
18 |
+
ocr_pipeline = pipeline("image-to-text", model="microsoft/trocr-small-printed") # OCR Model
|
19 |
+
text_gen_pipeline = pipeline("text-generation", model="distilbert/distilgpt2") # Text Generation Model
|
20 |
+
|
21 |
+
# Function to extract text from a PDF resume
|
22 |
+
def extract_text_from_pdf(pdf_path):
|
23 |
+
images = convert_from_path(pdf_path)
|
24 |
+
extracted_text = ""
|
25 |
+
|
26 |
+
for img in images:
|
27 |
+
text = pytesseract.image_to_string(img) # OCR extraction
|
28 |
+
extracted_text += text + "\n"
|
29 |
+
|
30 |
+
return extracted_text.strip()
|
31 |
+
|
32 |
+
# Route: Upload Resume & Generate Report
|
33 |
+
@app.route('/upload', methods=['POST'])
|
34 |
+
def upload_resume():
|
35 |
+
if 'file' not in request.files:
|
36 |
+
return jsonify({"error": "No file uploaded"}), 400
|
37 |
+
|
38 |
+
file = request.files['file']
|
39 |
+
if file.filename == '':
|
40 |
+
return jsonify({"error": "No file selected"}), 400
|
41 |
+
|
42 |
+
# Save uploaded file
|
43 |
+
filename = secure_filename(file.filename)
|
44 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
45 |
+
file.save(file_path)
|
46 |
+
|
47 |
+
# Extract text from PDF
|
48 |
+
extracted_text = extract_text_from_pdf(file_path)
|
49 |
+
|
50 |
+
# Generate AI evaluation
|
51 |
+
prompt = f"Candidate Resume: {extracted_text}\n\nEvaluate the suitability of this candidate for a software engineering role at Google."
|
52 |
+
ai_evaluation = text_gen_pipeline(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
|
53 |
+
|
54 |
+
# Return response
|
55 |
+
response = {
|
56 |
+
"resume_text": extracted_text[:1000], # Limit to 1000 chars for display
|
57 |
+
"ai_evaluation": ai_evaluation
|
58 |
+
}
|
59 |
+
|
60 |
+
return jsonify(response)
|
61 |
+
|
62 |
+
# Run Flask App
|
63 |
+
if __name__ == '__main__':
|
64 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|