Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from fastapi import Request
|
5 |
+
import joblib
|
6 |
+
import PyPDF2
|
7 |
+
import re
|
8 |
+
import logging
|
9 |
+
import tempfile
|
10 |
+
import os
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
|
15 |
+
app.add_middleware(
|
16 |
+
CORSMiddleware,
|
17 |
+
allow_origins=["*"],
|
18 |
+
allow_credentials=True,
|
19 |
+
allow_methods=["*"],
|
20 |
+
allow_headers=["*"],
|
21 |
+
)
|
22 |
+
|
23 |
+
logging.basicConfig(level=logging.INFO)
|
24 |
+
|
25 |
+
try:
|
26 |
+
model = joblib.load("react_classifier.joblib")
|
27 |
+
except Exception as e:
|
28 |
+
logging.error(f"Erro ao carregar o modelo: {e}")
|
29 |
+
raise
|
30 |
+
|
31 |
+
def clean_text(text):
|
32 |
+
text = text.lower()
|
33 |
+
text = re.sub(r'[^\w\s]', ' ', text)
|
34 |
+
text = re.sub(r'\s+', ' ', text)
|
35 |
+
return text.strip()
|
36 |
+
|
37 |
+
def extract_text_from_pdf(file_path):
|
38 |
+
text = ""
|
39 |
+
try:
|
40 |
+
with open(file_path, 'rb') as f:
|
41 |
+
reader = PyPDF2.PdfReader(f)
|
42 |
+
for page in reader.pages:
|
43 |
+
page_text = page.extract_text()
|
44 |
+
if page_text:
|
45 |
+
text += page_text + "\n"
|
46 |
+
except Exception as e:
|
47 |
+
raise Exception(f"Erro ao extrair texto do PDF: {e}")
|
48 |
+
return text
|
49 |
+
|
50 |
+
def avaliar_candidato(
|
51 |
+
anosEstudoTecnologia: int,
|
52 |
+
tempoDesenvolvedor: int,
|
53 |
+
tempoExperiencia: int,
|
54 |
+
nivelIngles: int,
|
55 |
+
graduacao: int
|
56 |
+
) -> dict:
|
57 |
+
score_estudo = anosEstudoTecnologia / 6
|
58 |
+
score_desenvolv = tempoDesenvolvedor / 6
|
59 |
+
score_experiencia = tempoExperiencia / 6
|
60 |
+
score_ingles = nivelIngles / 4
|
61 |
+
score_graduacao = graduacao / 7
|
62 |
+
|
63 |
+
peso_estudo = 0.15
|
64 |
+
peso_desenvolv = 0.2
|
65 |
+
peso_experiencia = 0.3
|
66 |
+
peso_ingles = 0.25
|
67 |
+
peso_graduacao = 0.1
|
68 |
+
|
69 |
+
score_final = (
|
70 |
+
score_estudo * peso_estudo +
|
71 |
+
score_desenvolv * peso_desenvolv +
|
72 |
+
score_experiencia * peso_experiencia +
|
73 |
+
score_ingles * peso_ingles +
|
74 |
+
score_graduacao * peso_graduacao
|
75 |
+
)
|
76 |
+
|
77 |
+
score_percentual = round(score_final * 100, 2)
|
78 |
+
aprovado = score_percentual >= 70.0
|
79 |
+
|
80 |
+
return {
|
81 |
+
"score": score_percentual,
|
82 |
+
"aprovado": aprovado
|
83 |
+
}
|
84 |
+
|
85 |
+
|
86 |
+
@app.post("/classify")
|
87 |
+
async def classify_resume(file: UploadFile = File(...)):
|
88 |
+
try:
|
89 |
+
suffix = os.path.splitext(file.filename)[-1]
|
90 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp:
|
91 |
+
temp.write(await file.read())
|
92 |
+
temp_path = temp.name
|
93 |
+
|
94 |
+
raw_text = extract_text_from_pdf(temp_path)
|
95 |
+
cleaned = clean_text(raw_text)
|
96 |
+
|
97 |
+
proba = model.predict_proba([cleaned])[0]
|
98 |
+
prediction = model.predict([cleaned])[0]
|
99 |
+
|
100 |
+
result = {
|
101 |
+
"classificacao": "React Developer" if prediction == 1 else "Outra Área",
|
102 |
+
"probabilidade": f"{proba[1]*100:.2f}%",
|
103 |
+
"trecho_texto": cleaned[:500] + "..." if len(cleaned) > 500 else cleaned
|
104 |
+
}
|
105 |
+
|
106 |
+
return JSONResponse(content=result)
|
107 |
+
|
108 |
+
except Exception as e:
|
109 |
+
logging.error(f"Erro no processamento: {e}")
|
110 |
+
raise HTTPException(status_code=500, detail=str(e))
|
111 |
+
finally:
|
112 |
+
if 'temp_path' in locals() and os.path.exists(temp_path):
|
113 |
+
os.remove(temp_path)
|
114 |
+
|
115 |
+
@app.post("/avaliar")
|
116 |
+
async def avaliar_formulario(request: Request):
|
117 |
+
try:
|
118 |
+
data = await request.json()
|
119 |
+
|
120 |
+
resultado = avaliar_candidato(
|
121 |
+
anosEstudoTecnologia=int(data.get("anosEstudoTecnologia", 0)),
|
122 |
+
tempoDesenvolvedor=int(data.get("tempoDesenvolvedor", 0)),
|
123 |
+
tempoExperiencia=int(data.get("tempoExperiencia", 0)),
|
124 |
+
nivelIngles=int(data.get("nivelIngles", 0)),
|
125 |
+
graduacao=int(data.get("graduacao", 0))
|
126 |
+
)
|
127 |
+
|
128 |
+
return JSONResponse(
|
129 |
+
status_code=200,
|
130 |
+
content={
|
131 |
+
"avaliacao": "Aprovado" if resultado["aprovado"] else "Reprovado",
|
132 |
+
"score": resultado["score"]
|
133 |
+
}
|
134 |
+
)
|
135 |
+
except Exception as e:
|
136 |
+
logging.error(f"Erro na avaliação do formulário: {e}")
|
137 |
+
raise HTTPException(status_code=500, detail="Erro ao processar os dados.")
|