Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,6 +6,8 @@ from fastapi.responses import JSONResponse
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
import aiofiles
|
| 8 |
import PyPDF2
|
|
|
|
|
|
|
| 9 |
|
| 10 |
UPLOAD_FOLDER = "uploads"
|
| 11 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
@@ -22,6 +24,28 @@ app.add_middleware(
|
|
| 22 |
)
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# ✅ Save uploaded file asynchronously
|
| 26 |
async def save_file(file: UploadFile) -> str:
|
| 27 |
filename = f"{uuid4()}_{file.filename}"
|
|
@@ -56,10 +80,13 @@ async def parse_resume(file: UploadFile = File(...)):
|
|
| 56 |
text = extract_text_from_pdf(path)
|
| 57 |
print("✅ Text extracted.")
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
os.remove(path)
|
| 60 |
print("🧹 File removed.")
|
| 61 |
|
| 62 |
-
return
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
import traceback
|
|
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
import aiofiles
|
| 8 |
import PyPDF2
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
+
from langchain.schema import HumanMessage
|
| 11 |
|
| 12 |
UPLOAD_FOLDER = "uploads"
|
| 13 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
+
llm = ChatOpenAI(
|
| 28 |
+
model_name="gpt-4o-mini", # Use a valid model name like "gpt-4o" or "gpt-4-turbo"
|
| 29 |
+
temperature=0,
|
| 30 |
+
openai_api_key=os.getenv("OPENAI_API_KEY")
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def parse_resume_text(text: str) -> dict:
|
| 34 |
+
prompt = f"""
|
| 35 |
+
Extract structured information from this resume text and return the result as a JSON object with the following keys:
|
| 36 |
+
- basics: {first_name, last_name, gender, emails, phone_numbers, address, total_experience_in_years, profession, summary, skills, has_driving_license}
|
| 37 |
+
- educations
|
| 38 |
+
- professional_experiences
|
| 39 |
+
- trainings_and_certifications
|
| 40 |
+
- languages
|
| 41 |
+
- awards
|
| 42 |
+
- references
|
| 43 |
+
Resume:
|
| 44 |
+
{text}
|
| 45 |
+
"""
|
| 46 |
+
result = llm([HumanMessage(content=prompt)])
|
| 47 |
+
return result.content
|
| 48 |
+
|
| 49 |
# ✅ Save uploaded file asynchronously
|
| 50 |
async def save_file(file: UploadFile) -> str:
|
| 51 |
filename = f"{uuid4()}_{file.filename}"
|
|
|
|
| 80 |
text = extract_text_from_pdf(path)
|
| 81 |
print("✅ Text extracted.")
|
| 82 |
|
| 83 |
+
json_result = parse_resume_text(text)
|
| 84 |
+
print("✅ JSON Created.")
|
| 85 |
+
|
| 86 |
os.remove(path)
|
| 87 |
print("🧹 File removed.")
|
| 88 |
|
| 89 |
+
return json_result
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
import traceback
|