Spaces:
Sleeping
Sleeping
Update app_hug.py
Browse files- app_hug.py +0 -688
app_hug.py
CHANGED
|
@@ -1,691 +1,3 @@
|
|
| 1 |
-
# from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends, Header, status, Query
|
| 2 |
-
# from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
# from fastapi.security import APIKeyHeader
|
| 4 |
-
# from pydantic import BaseModel, EmailStr, Field
|
| 5 |
-
# from typing import Optional, Tuple
|
| 6 |
-
# from enum import Enum
|
| 7 |
-
# import os
|
| 8 |
-
# import base64
|
| 9 |
-
# import pickle
|
| 10 |
-
# import pandas as pd
|
| 11 |
-
# from dotenv import load_dotenv
|
| 12 |
-
# from langchain_openai import ChatOpenAI
|
| 13 |
-
# from langchain.schema import HumanMessage, SystemMessage
|
| 14 |
-
# from email.mime.text import MIMEText
|
| 15 |
-
# from google.auth.transport.requests import Request
|
| 16 |
-
# from google.oauth2.credentials import Credentials
|
| 17 |
-
# from google_auth_oauthlib.flow import InstalledAppFlow
|
| 18 |
-
# from googleapiclient.discovery import build
|
| 19 |
-
# from googleapiclient.errors import HttpError
|
| 20 |
-
# from datetime import datetime
|
| 21 |
-
# import json
|
| 22 |
-
# from langfuse import Langfuse
|
| 23 |
-
# from langfuse.langchain import CallbackHandler
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# load_dotenv()
|
| 27 |
-
# langfuse = Langfuse(
|
| 28 |
-
# secret_key=os.getenv("SECRET_KEY"),
|
| 29 |
-
# public_key=os.getenv("PUBLIC_KEY"),
|
| 30 |
-
# host=os.getenv("APP_HOST")
|
| 31 |
-
# )
|
| 32 |
-
# langfuse_handler = CallbackHandler()
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# # Load environment variables (not needed on Hugging Face, but harmless)
|
| 36 |
-
# # ------------------------------------------
|
| 37 |
-
# # Security Configuration
|
| 38 |
-
# # ------------------------------------------
|
| 39 |
-
# API_PASSWORD = os.getenv("API_PASSWORD") # Can be overridden by env var
|
| 40 |
-
# api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
| 41 |
-
|
| 42 |
-
# def verify_api_key(api_key: Optional[str] = Depends(api_key_header)) -> str:
|
| 43 |
-
# """
|
| 44 |
-
# Verify the API key/password provided in the request header.
|
| 45 |
-
# """
|
| 46 |
-
# if api_key is None:
|
| 47 |
-
# raise HTTPException(
|
| 48 |
-
# status_code=status.HTTP_401_UNAUTHORIZED,
|
| 49 |
-
# detail="API Key required. Please provide X-API-Key header.",
|
| 50 |
-
# headers={"WWW-Authenticate": "ApiKey"},
|
| 51 |
-
# )
|
| 52 |
-
|
| 53 |
-
# if api_key != API_PASSWORD:
|
| 54 |
-
# raise HTTPException(
|
| 55 |
-
# status_code=status.HTTP_401_UNAUTHORIZED,
|
| 56 |
-
# detail="Invalid API Key",
|
| 57 |
-
# headers={"WWW-Authenticate": "ApiKey"},
|
| 58 |
-
# )
|
| 59 |
-
|
| 60 |
-
# return api_key
|
| 61 |
-
|
| 62 |
-
# # ------------------------------------------
|
| 63 |
-
# # Helper: Write GOOGLE_CREDENTIALS_JSON to file if needed
|
| 64 |
-
# # ------------------------------------------
|
| 65 |
-
# def ensure_credentials_file():
|
| 66 |
-
# credentials_env = os.getenv("GOOGLE_CREDENTIALS_JSON")
|
| 67 |
-
# credentials_path = "credentials_SYNAPSE.json"
|
| 68 |
-
# if not os.path.exists(credentials_path):
|
| 69 |
-
# if not credentials_env:
|
| 70 |
-
# raise Exception("GOOGLE_CREDENTIALS_JSON not found in environment variables.")
|
| 71 |
-
# try:
|
| 72 |
-
# parsed_json = json.loads(credentials_env)
|
| 73 |
-
# except json.JSONDecodeError:
|
| 74 |
-
# raise Exception("Invalid JSON in GOOGLE_CREDENTIALS_JSON")
|
| 75 |
-
# with open(credentials_path, "w") as f:
|
| 76 |
-
# json.dump(parsed_json, f, indent=2)
|
| 77 |
-
# return credentials_path
|
| 78 |
-
|
| 79 |
-
# # ------------------------------------------
|
| 80 |
-
# # FastAPI app
|
| 81 |
-
# # ------------------------------------------
|
| 82 |
-
# app = FastAPI(title="Recruitment Message Generator API", version="1.0.0")
|
| 83 |
-
# app.add_middleware(
|
| 84 |
-
# CORSMiddleware,
|
| 85 |
-
# allow_origins=["*"],
|
| 86 |
-
# allow_credentials=True,
|
| 87 |
-
# allow_methods=["*"],
|
| 88 |
-
# allow_headers=["*"],
|
| 89 |
-
# )
|
| 90 |
-
|
| 91 |
-
# SCOPES = ["https://www.googleapis.com/auth/gmail.send"]
|
| 92 |
-
# openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 93 |
-
|
| 94 |
-
# # ------------------------------------------
|
| 95 |
-
# # Enums and Models
|
| 96 |
-
# # ------------------------------------------
|
| 97 |
-
# class MessageType(str, Enum):
|
| 98 |
-
# OUTREACH = "outreach"
|
| 99 |
-
# INTRODUCTORY = "introductory"
|
| 100 |
-
# FOLLOWUP = "followup"
|
| 101 |
-
|
| 102 |
-
# class GenerateMessageRequest(BaseModel):
|
| 103 |
-
# job_evaluation: Optional[str] = Field(None, description="(Optional) Recruiter's evaluation of candidate for the job")
|
| 104 |
-
# sender_email: EmailStr
|
| 105 |
-
# reply_to_email: Optional[EmailStr] = Field(None, description="Recruiter's email for reply-to header")
|
| 106 |
-
# recipient_email: EmailStr
|
| 107 |
-
# candidate_name: str
|
| 108 |
-
# current_role: str
|
| 109 |
-
# current_company: str
|
| 110 |
-
# company_name: str
|
| 111 |
-
# role: str
|
| 112 |
-
# recruiter_name: str
|
| 113 |
-
# organisation: str
|
| 114 |
-
# message_type: MessageType
|
| 115 |
-
# send_email: bool = False
|
| 116 |
-
# past_conversation: Optional[str] = Field(None, description="(Optional) Previous messages with candidate")
|
| 117 |
-
|
| 118 |
-
# class FeedbackRequest(BaseModel):
|
| 119 |
-
# message: str
|
| 120 |
-
# feedback: str
|
| 121 |
-
|
| 122 |
-
# class AuthenticateRequest(BaseModel):
|
| 123 |
-
# email: EmailStr
|
| 124 |
-
|
| 125 |
-
# class AuthenticateResponse(BaseModel):
|
| 126 |
-
# success: bool
|
| 127 |
-
# message: str
|
| 128 |
-
# error: Optional[str] = None
|
| 129 |
-
|
| 130 |
-
# class MessageResponse(BaseModel):
|
| 131 |
-
# success: bool
|
| 132 |
-
# message: str
|
| 133 |
-
# email_sent: bool = False
|
| 134 |
-
# email_subject: Optional[str] = None
|
| 135 |
-
# error: Optional[str] = None
|
| 136 |
-
|
| 137 |
-
# class SendMessageRequest(BaseModel):
|
| 138 |
-
# subject: str
|
| 139 |
-
# email_body: str
|
| 140 |
-
# sender_email: EmailStr
|
| 141 |
-
# recipient_email: EmailStr
|
| 142 |
-
# reply_to_email: Optional[EmailStr] = None
|
| 143 |
-
|
| 144 |
-
# # ------------------------------------------
|
| 145 |
-
# # Gmail Helper Functions
|
| 146 |
-
# # ------------------------------------------
|
| 147 |
-
# def get_token_file_path(email: str) -> str:
|
| 148 |
-
# tokens_dir = "gmail_tokens"
|
| 149 |
-
# if not os.path.exists(tokens_dir):
|
| 150 |
-
# os.makedirs(tokens_dir)
|
| 151 |
-
# safe_email = email.replace("@", "_at_").replace(".", "_dot_")
|
| 152 |
-
# return os.path.join(tokens_dir, f"token_{safe_email}.pickle")
|
| 153 |
-
|
| 154 |
-
# def check_user_token_exists(email: str) -> bool:
|
| 155 |
-
# token_file = get_token_file_path(email)
|
| 156 |
-
# return os.path.exists(token_file)
|
| 157 |
-
|
| 158 |
-
# def load_user_credentials(email: str):
|
| 159 |
-
# token_file = get_token_file_path(email)
|
| 160 |
-
# if os.path.exists(token_file):
|
| 161 |
-
# try:
|
| 162 |
-
# with open(token_file, 'rb') as token:
|
| 163 |
-
# creds = pickle.load(token)
|
| 164 |
-
# return creds
|
| 165 |
-
# except Exception:
|
| 166 |
-
# if os.path.exists(token_file):
|
| 167 |
-
# os.remove(token_file)
|
| 168 |
-
# return None
|
| 169 |
-
|
| 170 |
-
# def save_user_credentials(email: str, creds):
|
| 171 |
-
# token_file = get_token_file_path(email)
|
| 172 |
-
# with open(token_file, 'wb') as token:
|
| 173 |
-
# pickle.dump(creds, token)
|
| 174 |
-
|
| 175 |
-
# def create_new_credentials(email: str):
|
| 176 |
-
# credentials_path = ensure_credentials_file()
|
| 177 |
-
# flow = InstalledAppFlow.from_client_secrets_file(
|
| 178 |
-
# credentials_path, SCOPES
|
| 179 |
-
# )
|
| 180 |
-
# creds = flow.run_local_server(port=0)
|
| 181 |
-
# save_user_credentials(email, creds)
|
| 182 |
-
# return creds
|
| 183 |
-
|
| 184 |
-
# def authenticate_gmail(email: str, create_if_missing: bool = False):
|
| 185 |
-
# creds = load_user_credentials(email)
|
| 186 |
-
# if creds:
|
| 187 |
-
# if creds.expired and creds.refresh_token:
|
| 188 |
-
# try:
|
| 189 |
-
# creds.refresh(Request())
|
| 190 |
-
# save_user_credentials(email, creds)
|
| 191 |
-
# except Exception:
|
| 192 |
-
# if create_if_missing:
|
| 193 |
-
# try:
|
| 194 |
-
# creds = create_new_credentials(email)
|
| 195 |
-
# except:
|
| 196 |
-
# return None
|
| 197 |
-
# else:
|
| 198 |
-
# return None
|
| 199 |
-
# elif not creds.valid:
|
| 200 |
-
# creds = None
|
| 201 |
-
# if not creds:
|
| 202 |
-
# if create_if_missing:
|
| 203 |
-
# try:
|
| 204 |
-
# creds = create_new_credentials(email)
|
| 205 |
-
# except:
|
| 206 |
-
# return None
|
| 207 |
-
# else:
|
| 208 |
-
# return None
|
| 209 |
-
# try:
|
| 210 |
-
# service = build("gmail", "v1", credentials=creds)
|
| 211 |
-
# return service
|
| 212 |
-
# except Exception:
|
| 213 |
-
# return None
|
| 214 |
-
|
| 215 |
-
# from email.mime.text import MIMEText
|
| 216 |
-
|
| 217 |
-
# def create_email_message(sender: str, to: str, subject: str, message_text: str, reply_to: Optional[str] = None, is_html: bool = False):
|
| 218 |
-
# if is_html:
|
| 219 |
-
# message = MIMEText(message_text, "html")
|
| 220 |
-
# else:
|
| 221 |
-
# message = MIMEText(message_text, "plain")
|
| 222 |
-
# message["To"] = to
|
| 223 |
-
# message["From"] = sender
|
| 224 |
-
# message["Subject"] = subject
|
| 225 |
-
# if reply_to:
|
| 226 |
-
# message["Reply-To"] = reply_to
|
| 227 |
-
# raw_message = base64.urlsafe_b64encode(message.as_bytes()).decode()
|
| 228 |
-
# return {"raw": raw_message}
|
| 229 |
-
|
| 230 |
-
# def send_gmail_message(service, user_id: str, message: dict):
|
| 231 |
-
# try:
|
| 232 |
-
# result = service.users().messages().send(userId=user_id, body=message).execute()
|
| 233 |
-
# return result is not None
|
| 234 |
-
# except HttpError:
|
| 235 |
-
# return False
|
| 236 |
-
|
| 237 |
-
# # ------------------------------------------
|
| 238 |
-
# # LLM (OpenAI) Message Generation Helpers
|
| 239 |
-
# # ------------------------------------------
|
| 240 |
-
# def refine_message_with_feedback(
|
| 241 |
-
# original_message: str,
|
| 242 |
-
# feedback: str,
|
| 243 |
-
# ) -> Tuple[str, str]:
|
| 244 |
-
# api_key = os.getenv("OPENAI_API_KEY")
|
| 245 |
-
# llm = ChatOpenAI(
|
| 246 |
-
# model="gpt-4o-mini",
|
| 247 |
-
# temperature=0.7,
|
| 248 |
-
# max_tokens=600,
|
| 249 |
-
# openai_api_key=api_key
|
| 250 |
-
# )
|
| 251 |
-
# prompt = f"""
|
| 252 |
-
# Please refine the following recruitment message based on the provided feedback:
|
| 253 |
-
|
| 254 |
-
# ORIGINAL MESSAGE:
|
| 255 |
-
# {original_message}
|
| 256 |
-
|
| 257 |
-
# FEEDBACK:
|
| 258 |
-
# {feedback}
|
| 259 |
-
|
| 260 |
-
# Please provide your response in the following format:
|
| 261 |
-
# SUBJECT: [Your subject line here]
|
| 262 |
-
|
| 263 |
-
# BODY:
|
| 264 |
-
# [Your refined email body content here]
|
| 265 |
-
# Keep the same tone and intent as the original message, but incorporate the feedback to improve it.
|
| 266 |
-
# """
|
| 267 |
-
# try:
|
| 268 |
-
# messages = [
|
| 269 |
-
# SystemMessage(content="You are a professional recruitment message writer. Refine the given message based on feedback while maintaining professionalism and the original intent."),
|
| 270 |
-
# HumanMessage(content=prompt)
|
| 271 |
-
# ]
|
| 272 |
-
# response = llm.invoke(messages, config={"callbacks": [langfuse_handler]})
|
| 273 |
-
# content = response.content.strip()
|
| 274 |
-
# subject_line = ""
|
| 275 |
-
# body_content = ""
|
| 276 |
-
# lines = content.split('\n')
|
| 277 |
-
# body_found = False
|
| 278 |
-
# body_lines = []
|
| 279 |
-
# for line in lines:
|
| 280 |
-
# if line.strip().startswith('SUBJECT:'):
|
| 281 |
-
# subject_line = line.replace('SUBJECT:', '').strip()
|
| 282 |
-
# elif line.strip().startswith('BODY:'):
|
| 283 |
-
# body_found = True
|
| 284 |
-
# elif body_found and line.strip():
|
| 285 |
-
# body_lines.append(line)
|
| 286 |
-
# body_content = '\n'.join(body_lines).strip()
|
| 287 |
-
# if not subject_line:
|
| 288 |
-
# subject_line = "Recruitment Opportunity - Updated"
|
| 289 |
-
# if not body_content:
|
| 290 |
-
# body_content = content
|
| 291 |
-
# return subject_line, body_content
|
| 292 |
-
# except Exception as e:
|
| 293 |
-
# raise HTTPException(status_code=500, detail=f"Error refining message: {str(e)}")
|
| 294 |
-
|
| 295 |
-
# def generate_recruitment_message_with_subject(
|
| 296 |
-
# msg_type: str,
|
| 297 |
-
# company: str,
|
| 298 |
-
# role_title: str,
|
| 299 |
-
# recruiter: str,
|
| 300 |
-
# org: str,
|
| 301 |
-
# candidate: str,
|
| 302 |
-
# current_pos: str,
|
| 303 |
-
# evaluation: Optional[str] = None,
|
| 304 |
-
# feedback: Optional[str] = None,
|
| 305 |
-
# past_conversation: Optional[str] = None
|
| 306 |
-
# ) -> Tuple[str, str]:
|
| 307 |
-
# api_key = os.getenv("OPENAI_API_KEY")
|
| 308 |
-
# llm = ChatOpenAI(
|
| 309 |
-
# model="gpt-4o-mini",
|
| 310 |
-
# temperature=0.7,
|
| 311 |
-
# max_tokens=600,
|
| 312 |
-
# openai_api_key=api_key
|
| 313 |
-
# )
|
| 314 |
-
# # Outreach: Only request consent
|
| 315 |
-
# if msg_type == "outreach":
|
| 316 |
-
# prompt = f"""
|
| 317 |
-
# Generate a professional initial outreach message to a candidate.
|
| 318 |
-
# - Introduce yourself as {recruiter} from {org}
|
| 319 |
-
# - Clearly state you are reaching out about an open role ({role_title}) at {company}
|
| 320 |
-
# - Ask if they are open to learning more or interested in a quick chat.
|
| 321 |
-
# - Do NOT discuss any job evaluation or judgment.
|
| 322 |
-
# - Explicitly request their consent to share more details. E.g., 'Would you be open to hearing more about this opportunity?'
|
| 323 |
-
# - Keep it short and friendly.
|
| 324 |
-
# - No placeholders like [Candidate Name] or [Role Title] in the output.
|
| 325 |
-
# """
|
| 326 |
-
# else:
|
| 327 |
-
# base_prompt = f"""
|
| 328 |
-
# Generate a professional recruitment {msg_type} with the following details:
|
| 329 |
-
# - Company hiring: {company}
|
| 330 |
-
# - Role: {role_title}
|
| 331 |
-
# - Recruiter: {recruiter} from {org}
|
| 332 |
-
# - Candidate: {candidate}
|
| 333 |
-
# - Candidate's current position: {current_pos}
|
| 334 |
-
# """
|
| 335 |
-
# if evaluation:
|
| 336 |
-
# base_prompt += f"- Evaluation: {evaluation}\n"
|
| 337 |
-
# prompt = base_prompt
|
| 338 |
-
# if msg_type == "introductory":
|
| 339 |
-
# prompt += """
|
| 340 |
-
# Create an introductory message that:
|
| 341 |
-
# - Thanks the candidate for their initial response
|
| 342 |
-
# - Provides more details about the role and company
|
| 343 |
-
# - Explains why this opportunity aligns with their background
|
| 344 |
-
# - Suggests next steps (like a call or meeting)
|
| 345 |
-
# - Maintains a warm, professional tone
|
| 346 |
-
# - if in the Evaluation the verdict is rejected, Send a rejection message instead.
|
| 347 |
-
# """
|
| 348 |
-
# else: # followup
|
| 349 |
-
# prompt += """
|
| 350 |
-
# Create a follow-up message that:
|
| 351 |
-
# - References previous communication
|
| 352 |
-
# - Reiterates interest in the candidate
|
| 353 |
-
# - Addresses any potential concerns
|
| 354 |
-
# - Provides additional compelling reasons to consider the role
|
| 355 |
-
# - Includes a clear call to action
|
| 356 |
-
# """
|
| 357 |
-
|
| 358 |
-
# # Use feedback if provided
|
| 359 |
-
# if feedback:
|
| 360 |
-
# prompt += f"\n\nPlease modify the message based on this feedback: {feedback}"
|
| 361 |
-
|
| 362 |
-
# # Use past conversation if provided
|
| 363 |
-
# if past_conversation:
|
| 364 |
-
# prompt += f"""
|
| 365 |
-
# Use the following past conversation as context to inform your reply:
|
| 366 |
-
|
| 367 |
-
# PAST CONVERSATION:
|
| 368 |
-
# {past_conversation}
|
| 369 |
-
|
| 370 |
-
# Write a reply message to the candidate, maintaining professionalism and continuity.
|
| 371 |
-
# """
|
| 372 |
-
|
| 373 |
-
# prompt += """
|
| 374 |
-
|
| 375 |
-
# Please provide your response in the following format:
|
| 376 |
-
# SUBJECT: [Your subject line here]
|
| 377 |
-
|
| 378 |
-
# BODY:
|
| 379 |
-
# [Your email body content here]
|
| 380 |
-
# """
|
| 381 |
-
|
| 382 |
-
# try:
|
| 383 |
-
# messages = [
|
| 384 |
-
# SystemMessage(content="You are a professional recruitment message writer. Generate both an email subject line and body content. Follow the exact format requested."),
|
| 385 |
-
# HumanMessage(content=prompt)
|
| 386 |
-
# ]
|
| 387 |
-
# response = llm.invoke(messages, config={"callbacks": [langfuse_handler]})
|
| 388 |
-
# content = response.content.strip()
|
| 389 |
-
# subject_line = ""
|
| 390 |
-
# body_content = ""
|
| 391 |
-
# lines = content.split('\n')
|
| 392 |
-
# body_found = False
|
| 393 |
-
# body_lines = []
|
| 394 |
-
# for line in lines:
|
| 395 |
-
# if line.strip().startswith('SUBJECT:'):
|
| 396 |
-
# subject_line = line.replace('SUBJECT:', '').strip()
|
| 397 |
-
# elif line.strip().startswith('BODY:'):
|
| 398 |
-
# body_found = True
|
| 399 |
-
# elif body_found and line.strip():
|
| 400 |
-
# body_lines.append(line)
|
| 401 |
-
# body_content = '\n'.join(body_lines).strip()
|
| 402 |
-
# if not subject_line:
|
| 403 |
-
# subject_line = f"Opportunity at {company} - {role_title}"
|
| 404 |
-
# if not body_content:
|
| 405 |
-
# body_content = content
|
| 406 |
-
# return subject_line, body_content
|
| 407 |
-
# except Exception as e:
|
| 408 |
-
# raise HTTPException(status_code=500, detail=f"Error generating message: {str(e)}")
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
# def format_email_html(body: str, sender_name: Optional[str]=None, sender_org: Optional[str]=None):
|
| 412 |
-
# """Wrap plain text body in an HTML template for better appearance."""
|
| 413 |
-
# # Convert consecutive line breaks into HTML paragraphs
|
| 414 |
-
# import re
|
| 415 |
-
# # Smart paragraphing
|
| 416 |
-
# body = re.sub(r"\n\s*\n", "</p><p>", body.strip()) # Double newlines => new paragraph
|
| 417 |
-
# body = re.sub(r"\n", "<br>", body) # Single newlines => <br>
|
| 418 |
-
# # Optional signature
|
| 419 |
-
# signature = ""
|
| 420 |
-
# if sender_name or sender_org:
|
| 421 |
-
# signature = "<br><br>Best regards,<br>"
|
| 422 |
-
# if sender_name:
|
| 423 |
-
# signature += f"{sender_name}<br>"
|
| 424 |
-
# if sender_org:
|
| 425 |
-
# signature += f"{sender_org}"
|
| 426 |
-
# html = f"""
|
| 427 |
-
# <html>
|
| 428 |
-
# <body style="font-family: Arial, sans-serif; color: #222; line-height: 1.7; max-width: 540px;">
|
| 429 |
-
# <p>{body}</p>
|
| 430 |
-
# {signature}
|
| 431 |
-
# </body>
|
| 432 |
-
# </html>
|
| 433 |
-
# """
|
| 434 |
-
# return html
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
# # ------------------------------------------
|
| 438 |
-
# # FastAPI Endpoints
|
| 439 |
-
# # ------------------------------------------
|
| 440 |
-
# @app.get("/")
|
| 441 |
-
# async def root():
|
| 442 |
-
# """Root endpoint - public access"""
|
| 443 |
-
# return {
|
| 444 |
-
# "message": "Recruitment Message Generator API",
|
| 445 |
-
# "version": "1.0.0",
|
| 446 |
-
# "authentication": "Required for all endpoints except / and /health",
|
| 447 |
-
# "auth_header": "X-API-Key",
|
| 448 |
-
# "endpoints": [
|
| 449 |
-
# "/generate-message",
|
| 450 |
-
# "/refine-message",
|
| 451 |
-
# "/authenticate",
|
| 452 |
-
# "/send-message",
|
| 453 |
-
# "/docs"
|
| 454 |
-
# ]
|
| 455 |
-
# }
|
| 456 |
-
|
| 457 |
-
# @app.post("/send-message", response_model=MessageResponse, dependencies=[Depends(verify_api_key)])
|
| 458 |
-
# async def send_message(
|
| 459 |
-
# subject: str = Query(..., description="Email subject"),
|
| 460 |
-
# email_body: str = Query(..., description="Email body content"),
|
| 461 |
-
# sender_email: str = Query(..., description="Sender's email address"),
|
| 462 |
-
# recipient_email: str = Query(..., description="Recipient's email address"),
|
| 463 |
-
# reply_to_email: Optional[str] = Query(None, description="Reply-to email address")
|
| 464 |
-
# ):
|
| 465 |
-
# try:
|
| 466 |
-
# # Authenticate sender
|
| 467 |
-
# service = authenticate_gmail(sender_email)
|
| 468 |
-
# if not service:
|
| 469 |
-
# return MessageResponse(
|
| 470 |
-
# success=False,
|
| 471 |
-
# message="",
|
| 472 |
-
# error="Gmail authentication failed"
|
| 473 |
-
# )
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
# formatted_html = format_email_html(
|
| 477 |
-
# email_body
|
| 478 |
-
# )
|
| 479 |
-
# # Create the email
|
| 480 |
-
# email_message = create_email_message(
|
| 481 |
-
# sender=sender_email,
|
| 482 |
-
# to=recipient_email,
|
| 483 |
-
# subject=subject,
|
| 484 |
-
# message_text=formatted_html,
|
| 485 |
-
# reply_to=reply_to_email,
|
| 486 |
-
# is_html=True
|
| 487 |
-
# )
|
| 488 |
-
# # Send the email
|
| 489 |
-
# email_sent = send_gmail_message(service, "me", email_message)
|
| 490 |
-
# if email_sent:
|
| 491 |
-
# return MessageResponse(
|
| 492 |
-
# success=True,
|
| 493 |
-
# message="Email sent successfully.",
|
| 494 |
-
# email_sent=True,
|
| 495 |
-
# email_subject=subject
|
| 496 |
-
# )
|
| 497 |
-
# else:
|
| 498 |
-
# return MessageResponse(
|
| 499 |
-
# success=False,
|
| 500 |
-
# message="",
|
| 501 |
-
# email_sent=False,
|
| 502 |
-
# email_subject=subject,
|
| 503 |
-
# error="Failed to send via Gmail API"
|
| 504 |
-
# )
|
| 505 |
-
# except Exception as e:
|
| 506 |
-
# return MessageResponse(
|
| 507 |
-
# success=False,
|
| 508 |
-
# message="",
|
| 509 |
-
# error=str(e)
|
| 510 |
-
# )
|
| 511 |
-
|
| 512 |
-
# @app.post("/generate-message", response_model=MessageResponse, dependencies=[Depends(verify_api_key)])
|
| 513 |
-
# async def generate_message(
|
| 514 |
-
# background_tasks: BackgroundTasks,
|
| 515 |
-
# sender_email: str = Query(..., description="Sender's email address"),
|
| 516 |
-
# recipient_email: str = Query(..., description="Recipient's email address"),
|
| 517 |
-
# candidate_name: str = Query(..., description="Candidate's name"),
|
| 518 |
-
# current_role: str = Query(..., description="Candidate's current role"),
|
| 519 |
-
# current_company: str = Query(..., description="Candidate's current company"),
|
| 520 |
-
# company_name: str = Query(..., description="Company name for the job"),
|
| 521 |
-
# role: str = Query(..., description="Job role title"),
|
| 522 |
-
# recruiter_name: str = Query(..., description="Recruiter's name"),
|
| 523 |
-
# organisation: str = Query(..., description="Recruiting organisation"),
|
| 524 |
-
# message_type: MessageType = Query(..., description="Type of message"),
|
| 525 |
-
# job_evaluation: Optional[str] = Query(None, description="Recruiter's evaluation of candidate for the job"),
|
| 526 |
-
# reply_to_email: Optional[str] = Query(None, description="Recruiter's email for reply-to header"),
|
| 527 |
-
# send_email: bool = Query(False, description="Whether to send the email"),
|
| 528 |
-
# past_conversation: Optional[str] = Query(None, description="Previous messages with candidate")
|
| 529 |
-
# ):
|
| 530 |
-
# try:
|
| 531 |
-
# current_position = f"{current_role} at {current_company}"
|
| 532 |
-
# email_subject, generated_message = generate_recruitment_message_with_subject(
|
| 533 |
-
# msg_type=message_type.value.replace('followup', 'follow-up'),
|
| 534 |
-
# company=company_name,
|
| 535 |
-
# role_title=role,
|
| 536 |
-
# recruiter=recruiter_name,
|
| 537 |
-
# org=organisation,
|
| 538 |
-
# candidate=candidate_name,
|
| 539 |
-
# current_pos=current_position,
|
| 540 |
-
# evaluation=job_evaluation,
|
| 541 |
-
# past_conversation=past_conversation
|
| 542 |
-
# )
|
| 543 |
-
# email_sent = False
|
| 544 |
-
# if send_email:
|
| 545 |
-
# registered_users = []
|
| 546 |
-
# if os.path.exists("registered_users.csv"):
|
| 547 |
-
# df = pd.read_csv("registered_users.csv")
|
| 548 |
-
# registered_users = df['email'].tolist() if 'email' in df.columns else []
|
| 549 |
-
# if sender_email.lower() not in [user.lower() for user in registered_users]:
|
| 550 |
-
# return MessageResponse(
|
| 551 |
-
# success=True,
|
| 552 |
-
# message=generated_message,
|
| 553 |
-
# email_sent=False,
|
| 554 |
-
# email_subject=email_subject,
|
| 555 |
-
# error="Email not sent: Sender email is not registered"
|
| 556 |
-
# )
|
| 557 |
-
# service = authenticate_gmail(sender_email)
|
| 558 |
-
# if service:
|
| 559 |
-
# formatted_html = format_email_html(
|
| 560 |
-
# generated_message,
|
| 561 |
-
# sender_name=recruiter_name,
|
| 562 |
-
# sender_org=organisation
|
| 563 |
-
# )
|
| 564 |
-
# email_message = create_email_message(
|
| 565 |
-
# sender=sender_email,
|
| 566 |
-
# to=recipient_email,
|
| 567 |
-
# subject=email_subject,
|
| 568 |
-
# message_text=formatted_html,
|
| 569 |
-
# reply_to=reply_to_email,
|
| 570 |
-
# is_html=True
|
| 571 |
-
# )
|
| 572 |
-
# email_sent = send_gmail_message(service, "me", email_message)
|
| 573 |
-
# if not email_sent:
|
| 574 |
-
# return MessageResponse(
|
| 575 |
-
# success=True,
|
| 576 |
-
# message=generated_message,
|
| 577 |
-
# email_sent=False,
|
| 578 |
-
# email_subject=email_subject,
|
| 579 |
-
# error="Email not sent: Failed to send via Gmail API"
|
| 580 |
-
# )
|
| 581 |
-
# else:
|
| 582 |
-
# return MessageResponse(
|
| 583 |
-
# success=True,
|
| 584 |
-
# message=generated_message,
|
| 585 |
-
# email_sent=False,
|
| 586 |
-
# email_subject=email_subject,
|
| 587 |
-
# error="Email not sent: Gmail authentication failed"
|
| 588 |
-
# )
|
| 589 |
-
# return MessageResponse(
|
| 590 |
-
# success=True,
|
| 591 |
-
# message=generated_message,
|
| 592 |
-
# email_sent=email_sent,
|
| 593 |
-
# email_subject=email_subject
|
| 594 |
-
# )
|
| 595 |
-
# except Exception as e:
|
| 596 |
-
# return MessageResponse(
|
| 597 |
-
# success=False,
|
| 598 |
-
# message="",
|
| 599 |
-
# error=str(e)
|
| 600 |
-
# )
|
| 601 |
-
|
| 602 |
-
# @app.post("/refine-message", response_model=MessageResponse, dependencies=[Depends(verify_api_key)])
|
| 603 |
-
# async def refine_message(request: FeedbackRequest):
|
| 604 |
-
# try:
|
| 605 |
-
# email_subject, refined_message = refine_message_with_feedback(
|
| 606 |
-
# original_message=request.message,
|
| 607 |
-
# feedback=request.feedback
|
| 608 |
-
# )
|
| 609 |
-
# return MessageResponse(
|
| 610 |
-
# success=True,
|
| 611 |
-
# message=refined_message,
|
| 612 |
-
# email_sent=False,
|
| 613 |
-
# email_subject=email_subject
|
| 614 |
-
# )
|
| 615 |
-
# except Exception as e:
|
| 616 |
-
# return MessageResponse(
|
| 617 |
-
# success=False,
|
| 618 |
-
# message="",
|
| 619 |
-
# error=str(e)
|
| 620 |
-
# )
|
| 621 |
-
|
| 622 |
-
# @app.post("/authenticate", response_model=AuthenticateResponse, dependencies=[Depends(verify_api_key)])
|
| 623 |
-
# async def authenticate_user(request: AuthenticateRequest):
|
| 624 |
-
# try:
|
| 625 |
-
# if check_user_token_exists(request.email):
|
| 626 |
-
# service = authenticate_gmail(request.email, create_if_missing=False)
|
| 627 |
-
# if service:
|
| 628 |
-
# return AuthenticateResponse(
|
| 629 |
-
# success=True,
|
| 630 |
-
# message="User already authenticated and token is valid"
|
| 631 |
-
# )
|
| 632 |
-
# else:
|
| 633 |
-
# service = authenticate_gmail(request.email, create_if_missing=True)
|
| 634 |
-
# if service:
|
| 635 |
-
# return AuthenticateResponse(
|
| 636 |
-
# success=True,
|
| 637 |
-
# message="Token refreshed successfully"
|
| 638 |
-
# )
|
| 639 |
-
# else:
|
| 640 |
-
# return AuthenticateResponse(
|
| 641 |
-
# success=False,
|
| 642 |
-
# message="Failed to refresh token",
|
| 643 |
-
# error="Could not refresh existing token. Please check credentials.json"
|
| 644 |
-
# )
|
| 645 |
-
# else:
|
| 646 |
-
# try:
|
| 647 |
-
# creds = create_new_credentials(request.email)
|
| 648 |
-
# if creds:
|
| 649 |
-
# return AuthenticateResponse(
|
| 650 |
-
# success=True,
|
| 651 |
-
# message="Authentication successful. Token created and saved."
|
| 652 |
-
# )
|
| 653 |
-
# else:
|
| 654 |
-
# return AuthenticateResponse(
|
| 655 |
-
# success=False,
|
| 656 |
-
# message="Authentication failed",
|
| 657 |
-
# error="Failed to create credentials"
|
| 658 |
-
# )
|
| 659 |
-
# except Exception as e:
|
| 660 |
-
# return AuthenticateResponse(
|
| 661 |
-
# success=False,
|
| 662 |
-
# message="Authentication failed",
|
| 663 |
-
# error=str(e)
|
| 664 |
-
# )
|
| 665 |
-
# except Exception as e:
|
| 666 |
-
# return AuthenticateResponse(
|
| 667 |
-
# success=False,
|
| 668 |
-
# message="Authentication error",
|
| 669 |
-
# error=str(e)
|
| 670 |
-
# )
|
| 671 |
-
|
| 672 |
-
# @app.get("/health")
|
| 673 |
-
# async def health_check():
|
| 674 |
-
# """Health check endpoint - public access"""
|
| 675 |
-
# return {"status": "healthy", "timestamp": datetime.utcnow().isoformat()}
|
| 676 |
-
|
| 677 |
-
# # Protected documentation endpoint
|
| 678 |
-
# @app.get("/docs", dependencies=[Depends(verify_api_key)])
|
| 679 |
-
# async def get_documentation():
|
| 680 |
-
# """This will be handled by FastAPI's built-in docs"""
|
| 681 |
-
# pass
|
| 682 |
-
|
| 683 |
-
# if __name__ == "__main__":
|
| 684 |
-
# import uvicorn
|
| 685 |
-
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
from fastapi import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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends, Header, status, Query
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.security import APIKeyHeader
|