Spaces:
Runtime error
Runtime error
Download services/db_service.py from utkarsh134/FastAPIExperience2: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/spaces/utkarsh134/FastAPIExperience2/resolve/main/services/db_service.py
- Command line
-
hf download hf://spaces/utkarsh134/FastAPIExperience2/services/db_service.py
-
curl -L -o db_service.py https://huggingface.co/spaces/utkarsh134/FastAPIExperience2/resolve/main/services/db_service.py
2.82 kB
| import os | |
| import uuid | |
| from datetime import datetime, date | |
| from typing import Dict, Any | |
| from supabase import create_client, Client | |
| _supabase_client = None | |
| def get_supabase_client() -> Client: | |
| global _supabase_client | |
| if _supabase_client is None: | |
| supabase_url = os.getenv("SUPABASE_URL") | |
| supabase_key = os.getenv("SUPABASE_KEY") | |
| if not supabase_url or not supabase_key: | |
| raise ValueError("SUPABASE_URL and SUPABASE_KEY environment variables must be set") | |
| _supabase_client = create_client(supabase_url, supabase_key) | |
| return _supabase_client | |
| def format_candidate_data(resume_data: Dict[str, Any]) -> Dict[str, Any]: | |
| db_record = { | |
| "id": str(uuid.uuid4()), | |
| "created_at": datetime.now().isoformat(), | |
| } | |
| direct_fields = [ | |
| "candidate_name", "gender", "email", "address", | |
| "pan_number", "aadhar_number", "highest_education", | |
| "university_name", "designation", "skills", "region", | |
| "relevant_experience", "experience_summary", "mobile_number" | |
| ] | |
| for field in direct_fields: | |
| db_record[field] = resume_data.get(field) | |
| dob = resume_data.get("date_of_birth") | |
| if dob: | |
| try: | |
| if '/' in dob: | |
| year, month, day = dob.split('/') | |
| db_record["date_of_birth"] = date(int(year), int(month), int(day)).isoformat() | |
| elif '-' in dob: | |
| db_record["date_of_birth"] = dob | |
| else: | |
| db_record["date_of_birth"] = None | |
| except (ValueError, TypeError): | |
| db_record["date_of_birth"] = None | |
| else: | |
| db_record["date_of_birth"] = None | |
| experience = resume_data.get("total_experience") | |
| if experience: | |
| try: | |
| db_record["total_experience"] = float(experience) | |
| except (ValueError, TypeError): | |
| db_record["total_experience"] = None | |
| else: | |
| db_record["total_experience"] = None | |
| file_date = resume_data.get("file_modified_date") | |
| if isinstance(file_date, datetime): | |
| db_record["file_modified_date"] = file_date.strftime("%Y-%m-%dT%H:%M:%S") | |
| else: | |
| db_record["file_modified_date"] = None | |
| return db_record | |
| async def store_candidate_data(resume_data: Dict[str, Any]) -> Dict[str, Any]: | |
| try: | |
| supabase = get_supabase_client() | |
| db_record = format_candidate_data(resume_data) | |
| response = supabase.table("candidates").insert(db_record).execute() | |
| if hasattr(response, 'error') and response.error: | |
| raise Exception(f"Database error: {response.error}") | |
| return db_record | |
| except Exception as e: | |
| print(f"Error storing candidate data: {str(e)}") | |
| raise |