Update main.py
Browse files
main.py
CHANGED
|
@@ -16,7 +16,7 @@ from flask import Flask, request, jsonify
|
|
| 16 |
from flask_cors import CORS
|
| 17 |
import firebase_admin
|
| 18 |
from firebase_admin import credentials, db, auth
|
| 19 |
-
from google import genai
|
| 20 |
|
| 21 |
# --- Basic Configuration ---
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -46,27 +46,26 @@ try:
|
|
| 46 |
gemini_api_key = os.environ.get("Gemini")
|
| 47 |
if not gemini_api_key: raise ValueError("The 'Gemini' environment variable for the API key is not set.")
|
| 48 |
|
| 49 |
-
# ---
|
| 50 |
-
# Instantiate the client object, which is the modern, recommended approach.
|
| 51 |
client = genai.Client(api_key=gemini_api_key)
|
| 52 |
-
#
|
| 53 |
-
gemini_model = client.
|
| 54 |
-
# --- END OF
|
| 55 |
|
| 56 |
-
logger.info("Google GenAI Client initialized successfully using the
|
| 57 |
|
| 58 |
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
|
| 59 |
if not ELEVENLABS_API_KEY: raise ValueError("The 'ELEVENLABS_API_KEY' environment variable is not set.")
|
| 60 |
logger.info("ElevenLabs API Key loaded.")
|
| 61 |
except Exception as e:
|
| 62 |
logger.critical(f"FATAL: Error initializing AI Clients: {e}")
|
|
|
|
| 63 |
exit(1)
|
| 64 |
|
| 65 |
|
| 66 |
# -----------------------------------------------------------------------------
|
| 67 |
# 2. CORE HELPER FUNCTIONS
|
| 68 |
# -----------------------------------------------------------------------------
|
| 69 |
-
# ... [No changes in this section] ...
|
| 70 |
def verify_token(auth_header):
|
| 71 |
if not auth_header or not auth_header.startswith('Bearer '): return None
|
| 72 |
token = auth_header.split('Bearer ')[1]
|
|
@@ -102,14 +101,11 @@ def extract_text_from_input(file, text):
|
|
| 102 |
else:
|
| 103 |
raise ValueError("No input provided. Please supply either a file or text.")
|
| 104 |
|
|
|
|
| 105 |
# -----------------------------------------------------------------------------
|
| 106 |
# 3. AI LOGIC FUNCTIONS
|
| 107 |
# -----------------------------------------------------------------------------
|
| 108 |
-
# The AI functions now use the `gemini_model` object created during initialization.
|
| 109 |
-
# No code changes are needed here as the `generate_content` method is the same.
|
| 110 |
-
|
| 111 |
def detect_use_case_with_gemini(text):
|
| 112 |
-
# ... [Implementation is identical, it uses the globally defined `gemini_model`] ...
|
| 113 |
logger.info("Starting use case detection with Gemini.")
|
| 114 |
prompt = f"""
|
| 115 |
Analyze the following text. Your task is to classify it into one of three categories: 'Job Interview', 'Investor Pitch', or 'Academic Presentation'.
|
|
@@ -132,7 +128,6 @@ def detect_use_case_with_gemini(text):
|
|
| 132 |
raise
|
| 133 |
|
| 134 |
def _get_context_specific_instructions(use_case):
|
| 135 |
-
# ... [Implementation is identical] ...
|
| 136 |
if use_case == 'Job Interview':
|
| 137 |
return "Pay close attention to the user's ability to align their skills with the role requirements mentioned in the briefing. Note any use of the STAR (Situation, Task, Action, Result) method in their answers."
|
| 138 |
elif use_case == 'Investor Pitch':
|
|
@@ -143,7 +138,6 @@ def _get_context_specific_instructions(use_case):
|
|
| 143 |
return ""
|
| 144 |
|
| 145 |
def analyze_transcript_with_gemini(uid, project_id, transcript, duration_seconds):
|
| 146 |
-
# ... [Implementation is identical] ...
|
| 147 |
logger.info(f"Starting transcript analysis for project {project_id}.")
|
| 148 |
try:
|
| 149 |
project_ref = db_ref.child(f'projects/{uid}/{project_id}')
|
|
@@ -199,7 +193,6 @@ def analyze_transcript_with_gemini(uid, project_id, transcript, duration_seconds
|
|
| 199 |
raise
|
| 200 |
|
| 201 |
def generate_agent_briefing(uid, project_id):
|
| 202 |
-
# ... [Implementation is identical] ...
|
| 203 |
logger.info(f"Generating agent briefing for project {project_id}.")
|
| 204 |
project_ref = db_ref.child(f'projects/{uid}/{project_id}')
|
| 205 |
project_data = project_ref.get()
|
|
@@ -239,7 +232,6 @@ def generate_agent_briefing(uid, project_id):
|
|
| 239 |
# -----------------------------------------------------------------------------
|
| 240 |
# 4. USER & AUTHENTICATION ENDPOINTS
|
| 241 |
# -----------------------------------------------------------------------------
|
| 242 |
-
# ... [No changes in this section] ...
|
| 243 |
@app.route('/api/auth/signup', methods=['POST'])
|
| 244 |
def signup():
|
| 245 |
try:
|
|
@@ -294,7 +286,7 @@ def get_user_profile():
|
|
| 294 |
# -----------------------------------------------------------------------------
|
| 295 |
# 5. CORE APPLICATION ENDPOINTS (FULL CRUD & CREDIT CHECKS)
|
| 296 |
# -----------------------------------------------------------------------------
|
| 297 |
-
|
| 298 |
@app.route('/api/projects', methods=['POST'])
|
| 299 |
def create_project():
|
| 300 |
uid = verify_token(request.headers.get('Authorization'))
|
|
@@ -448,7 +440,7 @@ def get_session_details(project_id, session_id):
|
|
| 448 |
# -----------------------------------------------------------------------------
|
| 449 |
# 6. CREDIT & ADMIN ENDPOINTS
|
| 450 |
# -----------------------------------------------------------------------------
|
| 451 |
-
|
| 452 |
@app.route('/api/user/request-credits', methods=['POST'])
|
| 453 |
def request_credits():
|
| 454 |
uid = verify_token(request.headers.get('Authorization'))
|
|
@@ -512,7 +504,7 @@ def admin_update_credits(uid):
|
|
| 512 |
# -----------------------------------------------------------------------------
|
| 513 |
# 7. DEBUGGING ENDPOINT
|
| 514 |
# -----------------------------------------------------------------------------
|
| 515 |
-
|
| 516 |
@app.route('/api/debug/agent-check', methods=['GET'])
|
| 517 |
def debug_agent_check():
|
| 518 |
try:
|
|
|
|
| 16 |
from flask_cors import CORS
|
| 17 |
import firebase_admin
|
| 18 |
from firebase_admin import credentials, db, auth
|
| 19 |
+
from google import genai
|
| 20 |
|
| 21 |
# --- Basic Configuration ---
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 46 |
gemini_api_key = os.environ.get("Gemini")
|
| 47 |
if not gemini_api_key: raise ValueError("The 'Gemini' environment variable for the API key is not set.")
|
| 48 |
|
| 49 |
+
# --- CORRECTED SDK PATTERN, AS PER OFFICIAL DOCUMENTATION ---
|
|
|
|
| 50 |
client = genai.Client(api_key=gemini_api_key)
|
| 51 |
+
# This is the correct method to get a model object for generation.
|
| 52 |
+
gemini_model = client.generative_model(model_name='gemini-2.0-flash')
|
| 53 |
+
# --- END OF CORRECTION ---
|
| 54 |
|
| 55 |
+
logger.info("Google GenAI Client initialized successfully using the correct SDK pattern.")
|
| 56 |
|
| 57 |
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
|
| 58 |
if not ELEVENLABS_API_KEY: raise ValueError("The 'ELEVENLABS_API_KEY' environment variable is not set.")
|
| 59 |
logger.info("ElevenLabs API Key loaded.")
|
| 60 |
except Exception as e:
|
| 61 |
logger.critical(f"FATAL: Error initializing AI Clients: {e}")
|
| 62 |
+
logger.critical(traceback.format_exc()) # Log the full traceback for debugging
|
| 63 |
exit(1)
|
| 64 |
|
| 65 |
|
| 66 |
# -----------------------------------------------------------------------------
|
| 67 |
# 2. CORE HELPER FUNCTIONS
|
| 68 |
# -----------------------------------------------------------------------------
|
|
|
|
| 69 |
def verify_token(auth_header):
|
| 70 |
if not auth_header or not auth_header.startswith('Bearer '): return None
|
| 71 |
token = auth_header.split('Bearer ')[1]
|
|
|
|
| 101 |
else:
|
| 102 |
raise ValueError("No input provided. Please supply either a file or text.")
|
| 103 |
|
| 104 |
+
|
| 105 |
# -----------------------------------------------------------------------------
|
| 106 |
# 3. AI LOGIC FUNCTIONS
|
| 107 |
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
| 108 |
def detect_use_case_with_gemini(text):
|
|
|
|
| 109 |
logger.info("Starting use case detection with Gemini.")
|
| 110 |
prompt = f"""
|
| 111 |
Analyze the following text. Your task is to classify it into one of three categories: 'Job Interview', 'Investor Pitch', or 'Academic Presentation'.
|
|
|
|
| 128 |
raise
|
| 129 |
|
| 130 |
def _get_context_specific_instructions(use_case):
|
|
|
|
| 131 |
if use_case == 'Job Interview':
|
| 132 |
return "Pay close attention to the user's ability to align their skills with the role requirements mentioned in the briefing. Note any use of the STAR (Situation, Task, Action, Result) method in their answers."
|
| 133 |
elif use_case == 'Investor Pitch':
|
|
|
|
| 138 |
return ""
|
| 139 |
|
| 140 |
def analyze_transcript_with_gemini(uid, project_id, transcript, duration_seconds):
|
|
|
|
| 141 |
logger.info(f"Starting transcript analysis for project {project_id}.")
|
| 142 |
try:
|
| 143 |
project_ref = db_ref.child(f'projects/{uid}/{project_id}')
|
|
|
|
| 193 |
raise
|
| 194 |
|
| 195 |
def generate_agent_briefing(uid, project_id):
|
|
|
|
| 196 |
logger.info(f"Generating agent briefing for project {project_id}.")
|
| 197 |
project_ref = db_ref.child(f'projects/{uid}/{project_id}')
|
| 198 |
project_data = project_ref.get()
|
|
|
|
| 232 |
# -----------------------------------------------------------------------------
|
| 233 |
# 4. USER & AUTHENTICATION ENDPOINTS
|
| 234 |
# -----------------------------------------------------------------------------
|
|
|
|
| 235 |
@app.route('/api/auth/signup', methods=['POST'])
|
| 236 |
def signup():
|
| 237 |
try:
|
|
|
|
| 286 |
# -----------------------------------------------------------------------------
|
| 287 |
# 5. CORE APPLICATION ENDPOINTS (FULL CRUD & CREDIT CHECKS)
|
| 288 |
# -----------------------------------------------------------------------------
|
| 289 |
+
|
| 290 |
@app.route('/api/projects', methods=['POST'])
|
| 291 |
def create_project():
|
| 292 |
uid = verify_token(request.headers.get('Authorization'))
|
|
|
|
| 440 |
# -----------------------------------------------------------------------------
|
| 441 |
# 6. CREDIT & ADMIN ENDPOINTS
|
| 442 |
# -----------------------------------------------------------------------------
|
| 443 |
+
|
| 444 |
@app.route('/api/user/request-credits', methods=['POST'])
|
| 445 |
def request_credits():
|
| 446 |
uid = verify_token(request.headers.get('Authorization'))
|
|
|
|
| 504 |
# -----------------------------------------------------------------------------
|
| 505 |
# 7. DEBUGGING ENDPOINT
|
| 506 |
# -----------------------------------------------------------------------------
|
| 507 |
+
|
| 508 |
@app.route('/api/debug/agent-check', methods=['GET'])
|
| 509 |
def debug_agent_check():
|
| 510 |
try:
|