rairo commited on
Commit
d173e03
·
verified ·
1 Parent(s): a38e707

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +296 -539
main.py CHANGED
@@ -1,589 +1,346 @@
1
- # -----------------------------------------------------------------------------
2
- # 1. IMPORTS & INITIALIZATION
3
- # -----------------------------------------------------------------------------
 
 
 
4
  import os
5
- import io
6
- import uuid
7
  import json
8
- import traceback
9
  import math
 
 
 
10
  import logging
 
11
  from datetime import datetime
12
 
13
- import requests
14
- import fitz # PyMuPDF
15
  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)
23
  logger = logging.getLogger(__name__)
24
 
25
- # --- Initialize Flask App & CORS ---
 
 
26
  app = Flask(__name__)
27
  CORS(app)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # --- Firebase Initialization ---
30
- try:
31
- credentials_json_string = os.environ.get("FIREBASE")
32
- if not credentials_json_string: raise ValueError("The 'FIREBASE' environment variable is not set.")
33
- credentials_json = json.loads(credentials_json_string)
34
- firebase_db_url = os.environ.get("Firebase_DB")
35
- if not firebase_db_url: raise ValueError("The 'Firebase_DB' environment variable must be set.")
36
- cred = credentials.Certificate(credentials_json)
37
- firebase_admin.initialize_app(cred, {'databaseURL': firebase_db_url})
38
- db_ref = db.reference()
39
- logger.info("Firebase Admin SDK initialized successfully.")
40
- except Exception as e:
41
- logger.critical(f"FATAL: Error initializing Firebase: {e}")
42
- exit(1)
43
-
44
- # --- AI Client Initialization ---
45
- 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
- client = genai.Client(api_key=gemini_api_key)
49
- MODEL_NAME = 'gemini-2.0-flash'
50
- logger.info(f"Google GenAI Client initialized successfully for model {MODEL_NAME}.")
51
- ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
52
- if not ELEVENLABS_API_KEY: raise ValueError("The 'ELEVENLABS_API_KEY' environment variable is not set.")
53
- logger.info("ElevenLabs API Key loaded.")
54
- except Exception as e:
55
- logger.critical(f"FATAL: Error initializing AI Clients: {e}")
56
- logger.critical(traceback.format_exc())
57
- exit(1)
58
-
59
-
60
- # -----------------------------------------------------------------------------
61
- # 2. CORE HELPER FUNCTIONS
62
- # -----------------------------------------------------------------------------
63
  def verify_token(auth_header):
64
- if not auth_header or not auth_header.startswith('Bearer '): return None
65
- token = auth_header.split('Bearer ')[1]
66
  try:
67
- return auth.verify_id_token(token)['uid']
68
- except Exception as e:
69
- logger.warning(f"Token verification failed: {e}")
70
  return None
71
 
72
- def verify_admin(auth_header):
73
- uid = verify_token(auth_header)
74
- if not uid: raise PermissionError('Invalid or missing user token')
75
- user_data = db_ref.child(f'users/{uid}').get()
76
- if not user_data or not user_data.get('is_admin', False):
77
- raise PermissionError('Admin access required')
78
  return uid
79
 
80
- def extract_text_from_input(file, text):
81
- if file:
82
- if file.mimetype == 'application/pdf':
83
- try:
84
- pdf_document = fitz.open(stream=file.read(), filetype="pdf")
85
- full_text = "".join(page.get_text() for page in pdf_document)
86
- pdf_document.close()
87
- return full_text
88
- except Exception as e:
89
- logger.error(f"Error processing PDF file: {e}")
90
- raise ValueError("Could not read the provided PDF file.")
91
- else:
92
- raise ValueError("Unsupported file type. Please upload a PDF.")
93
- elif text:
94
- return text
95
- else:
96
- raise ValueError("No input provided. Please supply either a file or text.")
97
-
98
-
99
- # -----------------------------------------------------------------------------
100
- # 3. AI LOGIC FUNCTIONS
101
- # -----------------------------------------------------------------------------
102
-
103
- def summarize_and_extract_context_with_gemini(text):
104
- logger.info("Starting intelligent context extraction with Gemini.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  prompt = f"""
106
- You are an expert document analyst. Analyze the following document text and perform two tasks:
107
- 1. Generate a concise, one-sentence "short_description" of the document's overall purpose.
108
- 2. Extract the "key_points" that are most critical for a mock interview or pitch scenario. This should be a dense paragraph or a few bullet points.
109
-
110
- Your entire response MUST be a single, valid JSON object with the keys "short_description" and "key_points". Do not include any text before or after the JSON.
111
-
112
- Document Text:
113
- "{text}"
114
- """
 
 
 
 
 
 
 
 
 
 
 
 
115
  try:
116
- response = client.models.generate_content(model=MODEL_NAME, contents=prompt)
117
- json_text = response.text.strip().lstrip("```json").rstrip("```")
118
- data = json.loads(json_text)
119
- logger.info("Successfully extracted intelligent context.")
120
- return data
121
  except Exception as e:
122
- logger.error(f"Error during intelligent context extraction: {e}")
123
  return {
124
- "short_description": "User-provided project document.",
125
- "key_points": text[:1000]
 
126
  }
127
 
128
- def detect_use_case_with_gemini(text):
129
- logger.info("Starting use case detection with Gemini.")
130
- prompt = f"""
131
- Analyze the following text. Your task is to classify it into one of three categories: 'Job Interview', 'Investor Pitch', or 'Academic Presentation'.
132
- Respond with ONLY the category name and nothing else.
133
-
134
- Text: "{text[:4000]}"
135
- """
136
- try:
137
- response = client.models.generate_content(model=MODEL_NAME, contents=prompt)
138
- category = response.text.strip().replace("'", "").replace('"', '')
139
- valid_categories = ['Job Interview', 'Investor Pitch', 'Academic Presentation']
140
- if category in valid_categories:
141
- logger.info(f"Gemini detected use case: {category}")
142
- return category
143
- else:
144
- logger.warning(f"Gemini returned an invalid category: '{category}'. Defaulting to 'Job Interview'.")
145
- return 'Job Interview'
146
- except Exception as e:
147
- logger.error(f"Error during Gemini use case detection: {e}")
148
- raise
149
-
150
- def _get_context_specific_instructions(use_case):
151
- if use_case == 'Job Interview':
152
- 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."
153
- elif use_case == 'Investor Pitch':
154
- return "Focus on the strength of the storytelling, the clarity of the business model, market logic, and how well they defended financial assumptions when challenged."
155
- elif use_case == 'Academic Presentation':
156
- return "Critique the methodological rigor, the clarity of the research findings, and the user's composure when handling critiques or questions about their research's validity."
157
- else:
158
- return ""
159
-
160
- def analyze_transcript_with_gemini(uid, project_id, transcript, duration_seconds):
161
- logger.info(f"Starting transcript analysis for project {project_id}.")
162
  try:
163
- # --- NEW: Fetch user's name for explicit identification ---
164
- user_ref = db_ref.child(f'users/{uid}')
165
- user_data = user_ref.get()
166
- user_name = user_data.get('displayName', 'the user') if user_data else 'the user'
167
- # --- END NEW ---
168
-
169
- project_ref = db_ref.child(f'projects/{uid}/{project_id}')
170
- project_data = project_ref.get()
171
- if not project_data: raise ValueError("Project not found for analysis.")
172
- use_case = project_data.get('detectedUseCase', 'General')
173
- context_text = project_data.get('key_points', project_data.get('originalBriefingText', ''))
174
-
175
- # --- NEW, SURGICALLY PRECISE PROMPT ---
176
- prompt = f"""
177
- You are an expert performance coach and communication analyst. Your task is to analyze the following transcript of a mock '{use_case}' with fairness and precision.
178
-
179
- **CRITICAL CONTEXT:**
180
- - You are analyzing a session for a user named **'{user_name}'**.
181
- - In the transcript, the speaker labeled 'User' is **'{user_name}'**.
182
- - Any other names mentioned by the user (e.g., Eric, Daniel, Rachel) are part of the role-play scenario and should NOT be confused with the user you are evaluating.
183
- - The user's session was based on a document with these key points: "{context_text}"
184
-
185
- Your analysis must be structured as a valid JSON object.
186
-
187
- **Step 1: Assess Conversation Substance**
188
- First, evaluate if the transcript contains a "substantive answer." A substantive answer is defined as a user's response to a direct question from the AI. An introductory statement or greeting from the user does NOT count as a substantive answer.
189
-
190
- **Step 2: Detailed Performance Evaluation with a Strict Scoring Rubric**
191
- Evaluate the user's performance on the four core criteria. You MUST follow this rubric:
192
-
193
- * **Crucial Scoring Rule:** To award a score above 40 in **any** category, the user must have provided at least one complete, on-topic, "substantive answer" as defined in Step 1.
194
- * **0-40 (Needs Significant Work):** Use this range if the session does not contain a substantive answer. Even if the user's introduction is excellent, if they do not answer a question, their scores must remain in this range.
195
- * **41-70 (Developing):** Use this for users who provide at least one substantive answer but struggle with clarity, depth, or confidence.
196
- * **71-100 (Proficient to Excellent):** Use this for users who provide clear, confident, and well-supported answers.
197
-
198
- **Core Criteria:**
199
- 1. **Communication Skills:** Clarity and confidence.
200
- 2. **Content Mastery:** Relevance and support for claims. Cannot be high without a substantive answer.
201
- 3. **Engagement & Delivery:** Tone and pacing.
202
- 4. **Resilience Under Pressure:** Handling follow-up questions. Cannot be scored high if no questions were answered.
203
-
204
- **Final Instruction:** Your qualitative feedback should reflect the score. If scores are low because the session was too short, you can praise the introduction in the "Strengths" section (e.g., "Provides a clear, confident opening statement.") but you MUST state in the "Areas for Improvement" section that a full analysis is impossible without answering questions.
205
-
206
- The JSON structure MUST be:
207
- {{
208
- "communicationScore": <integer>,
209
- "contentMasteryScore": <integer>,
210
- "engagementDeliveryScore": <integer>,
211
- "resilienceScore": <integer>,
212
- "qualitativeStrengths": "<string>",
213
- "qualitativeImprovements": "<string>",
214
- "contextSpecificFeedback": "<string>"
215
- }}
216
-
217
- Transcript to analyze:
218
- "{transcript}"
219
- """
220
- # --- END OF NEW PROMPT ---
221
-
222
- response = client.models.generate_content(model=MODEL_NAME, contents=prompt)
223
- feedback_json_text = response.text.strip().lstrip("```json").rstrip("```")
224
- feedback_data = json.loads(feedback_json_text)
225
- session_id = str(uuid.uuid4())
226
- session_ref = project_ref.child(f'practiceSessions/{session_id}')
227
- session_data = {
228
- "sessionId": session_id, "createdAt": datetime.utcnow().isoformat() + "Z",
229
- "durationSeconds": duration_seconds, "transcript": transcript, "feedback": feedback_data
230
- }
231
- session_ref.set(session_data)
232
- logger.info(f"Successfully saved feedback for session {session_id}.")
233
- user_ref = db_ref.child(f'users/{uid}')
234
- user_data = user_ref.get()
235
- current_credits = user_data.get('credits', 0)
236
- cost = math.ceil(duration_seconds / 60) * 3
237
- new_credits = max(0, current_credits - cost)
238
- user_ref.update({'credits': new_credits})
239
- logger.info(f"Credits deducted for user {uid}. Cost: {cost}, Remaining: {new_credits}")
240
- return {"cost": cost, "remaining": new_credits, "sessionId": session_id}
241
- except Exception as e:
242
- logger.error(f"An error occurred during transcript analysis for project {project_id}: {e}")
243
- logger.error(traceback.format_exc())
244
- db_ref.child(f'projects/{uid}/{project_id}/sessions').push().set({"error": str(e), "transcript": transcript})
245
- raise
246
-
247
- def generate_agent_briefing(uid, project_id):
248
- logger.info(f"Generating agent briefing for project {project_id}.")
249
- project_ref = db_ref.child(f'projects/{uid}/{project_id}')
250
- project_data = project_ref.get()
251
- if not project_data: raise ValueError("Project not found.")
252
- use_case = project_data.get('detectedUseCase', 'General')
253
- key_points = project_data.get('key_points', 'No specific context was extracted.')
254
- base_briefing = f"This is a mock '{use_case}'. The user's context is based on a document with these key points: '{key_points}'. Your goal is to act as a realistic {use_case.split(' ')[0]} interviewer/panelist and ask relevant questions."
255
- sessions = project_data.get('practiceSessions', {})
256
- if not sessions: return f"{base_briefing} This is the user's first practice session for this project. Start with some introductory questions."
257
- try:
258
- past_feedback_summary = []
259
- for session in sessions.values():
260
- feedback = session.get('feedback', {})
261
- if feedback:
262
- past_feedback_summary.append({
263
- "improvements": feedback.get('qualitativeImprovements'),
264
- "scores": {"communication": feedback.get('communicationScore'), "content": feedback.get('contentMasteryScore'), "resilience": feedback.get('resilienceScore')}
265
- })
266
- if not past_feedback_summary: return f"{base_briefing} The user has practiced before, but their feedback is unavailable. Conduct a standard session."
267
- summary_prompt = f"""
268
- You are an assistant preparing a briefing for a conversational AI agent. Analyze the user's past performance feedback and provide a short, 1-2 sentence directive for the agent. Focus on the most consistent area of weakness.
269
- Past Feedback: {json.dumps(past_feedback_summary)}
270
- Example directives:
271
- - "The user consistently scores low on Resilience. Challenge their financial assumptions more aggressively this time."
272
- - "The user struggles with concise communication. Ask multi-part questions to test their ability to stay on track."
273
- Your directive for the agent:
274
- """
275
- response = client.models.generate_content(model=MODEL_NAME, contents=summary_prompt)
276
- dynamic_directive = response.text.strip()
277
- logger.info(f"Generated dynamic directive for agent: {dynamic_directive}")
278
- return f"{base_briefing} {dynamic_directive}"
279
- except Exception as e:
280
- logger.error(f"Could not generate dynamic briefing for project {project_id}: {e}")
281
- return base_briefing
282
 
 
 
283
 
284
- # -----------------------------------------------------------------------------
285
- # 4. USER & AUTHENTICATION ENDPOINTS
286
- # -----------------------------------------------------------------------------
287
- @app.route('/api/auth/signup', methods=['POST'])
288
- def signup():
289
- try:
290
- data = request.get_json()
291
- email, password, display_name = data.get('email'), data.get('password'), data.get('displayName')
292
- if not email or not password: return jsonify({'error': 'Email and password are required'}), 400
293
- user = auth.create_user(email=email, password=password, display_name=display_name)
294
- user_ref = db_ref.child(f'users/{user.uid}')
295
- user_data = {
296
- 'email': email, 'displayName': display_name, 'credits': 30, 'is_admin': False,
297
- 'createdAt': datetime.utcnow().isoformat() + "Z"
298
- }
299
- user_ref.set(user_data)
300
- logger.info(f"New user signed up: {user.uid}, Name: {display_name}")
301
- return jsonify({'success': True, 'uid': user.uid, **user_data}), 201
302
- except Exception as e:
303
- logger.error(f"Signup failed: {e}")
304
- if 'EMAIL_EXISTS' in str(e): return jsonify({'error': 'An account with this email already exists.'}), 409
305
- return jsonify({'error': str(e)}), 400
306
-
307
- @app.route('/api/auth/social-signin', methods=['POST'])
308
- def social_signin():
309
- uid = verify_token(request.headers.get('Authorization'))
310
- if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
311
- user_ref, user_data = db_ref.child(f'users/{uid}'), db_ref.child(f'users/{uid}').get()
312
- if user_data:
313
- logger.info(f"Existing social user signed in: {uid}")
314
- return jsonify({'uid': uid, **user_data}), 200
315
- else:
316
- logger.info(f"New social user detected: {uid}. Creating database profile.")
317
- try:
318
- firebase_user = auth.get_user(uid)
319
- new_user_data = {
320
- 'email': firebase_user.email, 'displayName': firebase_user.display_name, 'credits': 30,
321
- 'is_admin': False, 'createdAt': datetime.utcnow().isoformat() + "Z"
322
  }
323
- user_ref.set(new_user_data)
324
- logger.info(f"Successfully created profile for new social user: {uid}")
325
- return jsonify({'success': True, 'uid': uid, **new_user_data}), 201
326
- except Exception as e:
327
- logger.error(f"Error creating profile for new social user {uid}: {e}")
328
- return jsonify({'error': f'Failed to create user profile: {str(e)}'}), 500
329
-
330
- @app.route('/api/user/profile', methods=['GET'])
331
- def get_user_profile():
332
- uid = verify_token(request.headers.get('Authorization'))
333
- if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
334
- user_data = db_ref.child(f'users/{uid}').get()
335
- if not user_data: return jsonify({'error': 'User not found'}), 404
336
- return jsonify({'uid': uid, **user_data})
337
-
338
- # -----------------------------------------------------------------------------
339
- # 5. CORE APPLICATION ENDPOINTS (FULL CRUD & CREDIT CHECKS)
340
- # -----------------------------------------------------------------------------
341
-
342
- @app.route('/api/projects', methods=['POST'])
343
- def create_project():
344
- uid = verify_token(request.headers.get('Authorization'))
345
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
346
- user_ref = db_ref.child(f'users/{uid}')
347
- user_data = user_ref.get()
348
- if not user_data or user_data.get('credits', 0) < 1:
349
- return jsonify({'error': 'Insufficient credits to create a project.'}), 402
350
- try:
351
- briefing_text = extract_text_from_input(request.files.get('file'), request.form.get('text'))
352
- context_data = summarize_and_extract_context_with_gemini(briefing_text)
353
- detected_use_case = detect_use_case_with_gemini(briefing_text)
354
- project_id = str(uuid.uuid4())
355
- project_ref = db_ref.child(f'projects/{uid}/{project_id}')
356
- project_data = {
357
- "projectId": project_id, "userId": uid,
358
- "title": context_data.get('short_description', 'New Project'),
359
- "detectedUseCase": detected_use_case,
360
- "originalBriefingText": briefing_text,
361
- "key_points": context_data.get('key_points'),
362
- "short_description": context_data.get('short_description'),
363
- "createdAt": datetime.utcnow().isoformat() + "Z", "practiceSessions": {}
364
- }
365
- project_ref.set(project_data)
366
- user_ref.update({'credits': user_data.get('credits', 0) - 1})
367
- logger.info(f"Created new project {project_id} for user {uid}. Cost: 1 credit.")
368
- return jsonify(project_data), 201
369
- except ValueError as e: return jsonify({'error': str(e)}), 400
370
- except Exception as e:
371
- logger.error(f"Project creation failed for user {uid}: {e}")
372
- return jsonify({'error': 'An internal server error occurred.'}), 500
373
 
374
- @app.route('/api/projects', methods=['GET'])
375
- def list_projects():
376
- uid = verify_token(request.headers.get('Authorization'))
377
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
378
- try:
379
- projects_data = db_ref.child(f'projects/{uid}').get()
380
- return jsonify(list(projects_data.values()) if projects_data else []), 200
381
  except Exception as e:
382
- logger.error(f"Failed to list projects for user {uid}: {e}")
383
- return jsonify({'error': 'Could not retrieve projects.'}), 500
384
 
385
- @app.route('/api/projects/<string:project_id>', methods=['GET'])
386
- def get_project(project_id):
387
- uid = verify_token(request.headers.get('Authorization'))
388
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
389
- try:
390
- project_data = db_ref.child(f'projects/{uid}/{project_id}').get()
391
- if not project_data: return jsonify({'error': 'Project not found or access denied'}), 404
392
- return jsonify(project_data), 200
393
- except Exception as e:
394
- logger.error(f"Failed to get project {project_id} for user {uid}: {e}")
395
- return jsonify({'error': 'Could not retrieve project details.'}), 500
396
-
397
- @app.route('/api/projects/<string:project_id>', methods=['PUT'])
398
- def update_project(project_id):
399
- uid = verify_token(request.headers.get('Authorization'))
400
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
401
- data = request.get_json()
402
- new_title = data.get('title')
403
- if not new_title or not isinstance(new_title, str) or len(new_title.strip()) == 0:
404
- return jsonify({'error': 'A valid title is required.'}), 400
405
  try:
406
- project_ref = db_ref.child(f'projects/{uid}/{project_id}')
407
- if not project_ref.get(): return jsonify({'error': 'Project not found or access denied'}), 404
408
- project_ref.update({'title': new_title.strip()})
409
- logger.info(f"User {uid} updated title for project {project_id}.")
410
- return jsonify({'success': True, 'message': 'Project updated successfully.'}), 200
411
- except Exception as e:
412
- logger.error(f"Failed to update project {project_id} for user {uid}: {e}")
413
- return jsonify({'error': 'Could not update the project.'}), 500
414
 
415
- @app.route('/api/projects/<string:project_id>', methods=['DELETE'])
416
- def delete_project(project_id):
417
- uid = verify_token(request.headers.get('Authorization'))
418
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
419
- try:
420
- project_ref = db_ref.child(f'projects/{uid}/{project_id}')
421
- if not project_ref.get(): return jsonify({'error': 'Project not found or access denied'}), 404
422
- project_ref.delete()
423
- logger.info(f"User {uid} deleted project {project_id}.")
424
- return jsonify({'success': True, 'message': 'Project deleted successfully.'}), 200
425
- except Exception as e:
426
- logger.error(f"Failed to delete project {project_id} for user {uid}: {e}")
427
- return jsonify({'error': 'Could not delete the project.'}), 500
 
 
 
 
 
 
 
 
 
428
 
429
- @app.route('/api/projects/<string:project_id>/briefing', methods=['GET'])
430
- def get_agent_briefing(project_id):
431
- uid = verify_token(request.headers.get('Authorization'))
432
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
433
- try:
434
- briefing = generate_agent_briefing(uid, project_id)
435
- return jsonify({"briefing": briefing})
436
- except ValueError as e: return jsonify({'error': str(e)}), 404
437
- except Exception as e:
438
- logger.error(f"Failed to generate briefing for project {project_id}: {e}")
439
- return jsonify({'error': 'Could not generate session briefing.'}), 500
440
-
441
- @app.route('/api/ai/get-agent-url', methods=['GET'])
442
- def get_agent_url():
443
- uid = verify_token(request.headers.get('Authorization'))
444
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
445
- user_data = db_ref.child(f'users/{uid}').get()
446
- if not user_data or user_data.get('credits', 0) < 3:
447
- return jsonify({'error': 'Insufficient credits to start a call. Minimum 3 required.'}), 402
448
- try:
449
- agent_id = os.environ.get("ELEVENLABS_AGENT_ID")
450
- if not agent_id: raise ValueError("ELEVENLABS_AGENT_ID is not configured on the server.")
451
- url, headers = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={agent_id}", {"xi-api-key": ELEVENLABS_API_KEY}
452
- response = requests.get(url, headers=headers)
453
- response.raise_for_status()
454
- logger.info(f"Successfully generated ElevenLabs signed URL for user {uid}.")
455
- return jsonify(response.json()), 200
456
- except requests.exceptions.RequestException as e:
457
- logger.error(f"ElevenLabs API error for user {uid}: {e}")
458
- return jsonify({'error': 'Failed to connect to the conversation service.'}), 502
459
- except Exception as e:
460
- logger.error(f"Error in get_agent_url for user {uid}: {e}")
461
- return jsonify({'error': 'An internal server error occurred.'}), 500
462
-
463
- @app.route('/api/projects/<string:project_id>/sessions/end', methods=['POST'])
464
- def end_session_and_analyze(project_id): # <-- CORRECTED: Added project_id parameter
465
- uid = verify_token(request.headers.get('Authorization'))
466
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
467
- data = request.get_json()
468
- duration, transcript = data.get('durationSeconds'), data.get('transcript')
469
- if not isinstance(duration, (int, float)) or not transcript:
470
- return jsonify({'error': 'durationSeconds and transcript are required.'}), 400
471
- try:
472
- result = analyze_transcript_with_gemini(uid, project_id, transcript, duration)
473
  return jsonify({
474
- "status": "success", "message": "Session logged and analysis complete.",
475
- "sessionId": result["sessionId"],
476
- "creditsDeducted": result["cost"], "remainingCredits": result["remaining"]
477
- }), 200
 
478
  except Exception as e:
479
- logger.error(f"Failed to process end of session for project {project_id}: {e}")
480
- return jsonify({'error': 'Failed to process session analysis.'}), 500
481
 
482
- @app.route('/api/projects/<string:project_id>/sessions/<string:session_id>', methods=['GET'])
483
- def get_session_details(project_id, session_id):
484
- uid = verify_token(request.headers.get('Authorization'))
485
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
486
  try:
487
- session_ref = db_ref.child(f'projects/{uid}/{project_id}/practiceSessions/{session_id}')
488
- session_data = session_ref.get()
489
- if not session_data:
490
- return jsonify({'error': 'Session not found or access denied.'}), 404
491
- return jsonify(session_data), 200
 
 
 
 
 
492
  except Exception as e:
493
- logger.error(f"Failed to retrieve session {session_id} for user {uid}: {e}")
494
- return jsonify({'error': 'An internal server error occurred.'}), 500
495
 
496
- # -----------------------------------------------------------------------------
497
- # 6. CREDIT & ADMIN ENDPOINTS
498
- # -----------------------------------------------------------------------------
499
 
500
- @app.route('/api/user/request-credits', methods=['POST'])
501
- def request_credits():
502
- uid = verify_token(request.headers.get('Authorization'))
503
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
504
  try:
505
- data = request.get_json()
506
- if not data or 'requested_credits' not in data: return jsonify({'error': 'requested_credits is required'}), 400
507
- request_ref = db_ref.child('credit_requests').push()
508
- request_ref.set({
509
- 'requestId': request_ref.key, 'userId': uid,
510
- 'requested_credits': data['requested_credits'], 'status': 'pending',
511
- 'requestedAt': datetime.utcnow().isoformat() + "Z"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
512
  })
513
- return jsonify({'success': True, 'requestId': request_ref.key})
514
- except Exception as e: return jsonify({'error': str(e)}), 500
515
 
516
- @app.route('/api/admin/credit_requests', methods=['GET'])
517
- def list_credit_requests():
518
- try:
519
- verify_admin(request.headers.get('Authorization'))
520
- requests_data = db_ref.child('credit_requests').get() or {}
521
- return jsonify(list(requests_data.values()))
522
- except PermissionError as e: return jsonify({'error': str(e)}), 403
523
- except Exception as e: return jsonify({'error': str(e)}), 500
524
-
525
- @app.route('/api/admin/credit_requests/<string:request_id>', methods=['PUT'])
526
- def process_credit_request(request_id):
527
- try:
528
- admin_uid = verify_admin(request.headers.get('Authorization'))
529
- req_ref = db_ref.child(f'credit_requests/{request_id}')
530
- req_data = req_ref.get()
531
- if not req_data: return jsonify({'error': 'Credit request not found'}), 404
532
- decision = request.json.get('decision')
533
- if decision not in ['approved', 'declined']: return jsonify({'error': 'Decision must be "approved" or "declined"'}), 400
534
- if decision == 'approved':
535
- user_ref = db_ref.child(f'users/{req_data["userId"]}')
536
- user_data = user_ref.get()
537
- if user_data:
538
- new_total = user_data.get('credits', 0) + int(req_data.get('requested_credits', 0))
539
- user_ref.update({'credits': new_total})
540
- req_ref.update({'status': decision, 'processedBy': admin_uid, 'processedAt': datetime.utcnow().isoformat() + "Z"})
541
- return jsonify({'success': True, 'message': f'Request {decision}.'})
542
- except PermissionError as e: return jsonify({'error': str(e)}), 403
543
- except Exception as e: return jsonify({'error': str(e)}), 500
544
-
545
- @app.route('/api/admin/users/<string:uid>/credits', methods=['PUT'])
546
- def admin_update_credits(uid):
547
- try:
548
- verify_admin(request.headers.get('Authorization'))
549
- add_credits = request.json.get('add_credits')
550
- if add_credits is None: return jsonify({'error': 'add_credits is required'}), 400
551
- user_ref = db_ref.child(f'users/{uid}')
552
- user_data = user_ref.get()
553
- if not user_data: return jsonify({'error': 'User not found'}), 404
554
- new_total = user_data.get('credits', 0) + int(add_credits)
555
- user_ref.update({'credits': new_total})
556
- return jsonify({'success': True, 'new_total_credits': new_total})
557
- except PermissionError as e: return jsonify({'error': str(e)}), 403
558
- except Exception as e: return jsonify({'error': str(e)}), 500
559
-
560
- # -----------------------------------------------------------------------------
561
- # 7. DEBUGGING ENDPOINT
562
- # -----------------------------------------------------------------------------
563
-
564
- @app.route('/api/debug/agent-check', methods=['GET'])
565
- def debug_agent_check():
566
- try:
567
- agent_id, api_key = os.environ.get("ELEVENLABS_AGENT_ID"), ELEVENLABS_API_KEY
568
- if not agent_id or not api_key:
569
- return jsonify({'error': 'ELEVENLABS_AGENT_ID or ELEVENLABS_API_KEY not set on server'}), 500
570
- url, headers = f"https://api.elevenlabs.io/v1/agents/{agent_id}", {"xi-api-key": api_key}
571
- response = requests.get(url, headers=headers)
572
- if response.ok:
573
- return jsonify({
574
- 'status': 'success', 'message': 'Agent found and API key is valid.',
575
- 'agent_id': agent_id, 'agent_name': response.json().get('name')
576
- })
577
- else:
578
- return jsonify({
579
- 'status': 'failure', 'message': 'Could not retrieve agent. Check Agent ID and API Key.',
580
- 'agent_id': agent_id, 'statusCode': response.status_code, 'response': response.text
581
- }), 404
582
- except Exception as e: return jsonify({'error': str(e)}), 500
583
-
584
- # -----------------------------------------------------------------------------
585
- # 8. MAIN EXECUTION
586
- # -----------------------------------------------------------------------------
587
- if __name__ == '__main__':
588
- port = int(os.environ.get("PORT", 7860))
589
- app.run(debug=False, host="0.0.0.0", port=port)
 
1
+ # ============================================================
2
+ # main.py AI Partner Cultural Simulator Backend
3
+ # REST + WebSocket | Gemini + Firebase + Azure Pronunciation
4
+ # HuggingFace-compatible (Port 7860)
5
+ # ============================================================
6
+
7
  import os
 
 
8
  import json
9
+ import uuid
10
  import math
11
+ import base64
12
+ import tempfile
13
+ import subprocess
14
  import logging
15
+ import traceback
16
  from datetime import datetime
17
 
 
 
18
  from flask import Flask, request, jsonify
19
  from flask_cors import CORS
20
+ from flask_socketio import SocketIO, emit
21
+
22
  import firebase_admin
23
  from firebase_admin import credentials, db, auth
24
+
25
  from google import genai
26
+ import azure.cognitiveservices.speech as speechsdk
27
 
28
+ # ------------------------------
29
+ # Logging (HF captures stdout)
30
+ # ------------------------------
31
  logging.basicConfig(level=logging.INFO)
32
  logger = logging.getLogger(__name__)
33
 
34
+ # ------------------------------
35
+ # App Init
36
+ # ------------------------------
37
  app = Flask(__name__)
38
  CORS(app)
39
+ socketio = SocketIO(app, cors_allowed_origins="*", async_mode="eventlet")
40
+
41
+ # ============================================================
42
+ # ENV & CLIENT INITIALIZATION
43
+ # ============================================================
44
+
45
+ # --- Firebase ---
46
+ firebase_json = os.environ.get("FIREBASE")
47
+ firebase_db_url = os.environ.get("Firebase_DB")
48
+
49
+ cred = credentials.Certificate(json.loads(firebase_json))
50
+ firebase_admin.initialize_app(cred, {"databaseURL": firebase_db_url})
51
+ db_ref = db.reference()
52
+
53
+ # --- Gemini ---
54
+ GEMINI_API_KEY = os.environ.get("Gemini")
55
+ MODEL_NAME = "gemini-2.0-flash"
56
+ gemini_client = genai.Client(api_key=GEMINI_API_KEY)
57
+
58
+ # --- Azure Speech ---
59
+ AZURE_SPEECH_KEY = os.environ.get("AZURE_SPEECH_KEY")
60
+ AZURE_SPEECH_REGION = os.environ.get("AZURE_SPEECH_REGION")
61
+
62
+ # ============================================================
63
+ # LANGUAGE PACKS
64
+ # ============================================================
65
+
66
+ from korean import KOREAN_PACK
67
+ from english import ENGLISH_PACK
68
+ # from japanese import JAPANESE_PACK
69
+ # from german import GERMAN_PACK
70
+
71
+ LANGUAGE_PACKS = {
72
+ "ko-KR": KOREAN_PACK,
73
+ "en-US": ENGLISH_PACK,
74
+ # "ja-JP": JAPANESE_PACK,
75
+ # "de-DE": GERMAN_PACK,
76
+ }
77
+
78
+ # ============================================================
79
+ # AUTH HELPERS (reuse-friendly)
80
+ # ============================================================
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  def verify_token(auth_header):
83
+ if not auth_header or not auth_header.startswith("Bearer "):
84
+ return None
85
  try:
86
+ token = auth_header.split("Bearer ")[1]
87
+ return auth.verify_id_token(token)["uid"]
88
+ except Exception:
89
  return None
90
 
91
+ def require_user():
92
+ uid = verify_token(request.headers.get("Authorization"))
93
+ if not uid:
94
+ raise PermissionError("Unauthorized")
 
 
95
  return uid
96
 
97
+ # ============================================================
98
+ # CREDIT RULES (simple + explicit)
99
+ # ============================================================
100
+
101
+ START_SESSION_COST = 1
102
+ PRACTICE_ATTEMPT_COST = 1
103
+ PER_MINUTE_COST = 2
104
+
105
+ def charge(uid, amount):
106
+ user_ref = db_ref.child(f"users/{uid}")
107
+ user = user_ref.get()
108
+ if user.get("credits", 0) < amount:
109
+ raise ValueError("Insufficient credits")
110
+ user_ref.update({"credits": user["credits"] - amount})
111
+
112
+ # ============================================================
113
+ # SESSION HELPERS
114
+ # ============================================================
115
+
116
+ def create_session(uid, language, scenario_id, title):
117
+ session_id = str(uuid.uuid4())
118
+ session = {
119
+ "sessionId": session_id,
120
+ "language": language,
121
+ "scenarioId": scenario_id,
122
+ "title": title,
123
+ "meters": {"respect": 50, "influence": 50, "trust": 50},
124
+ "turns": [],
125
+ "createdAt": datetime.utcnow().isoformat() + "Z"
126
+ }
127
+ db_ref.child(f"sessions/{uid}/{session_id}").set(session)
128
+ return session
129
+
130
+ def get_session(uid, session_id):
131
+ return db_ref.child(f"sessions/{uid}/{session_id}").get()
132
+
133
+ def update_session(uid, session_id, data):
134
+ db_ref.child(f"sessions/{uid}/{session_id}").update(data)
135
+
136
+ # ============================================================
137
+ # GEMINI — CULTURAL EVALUATOR
138
+ # ============================================================
139
+
140
+ def evaluate_turn(language_pack, scenario, transcript_turn):
141
  prompt = f"""
142
+ You are a cultural authority evaluator.
143
+
144
+ LANGUAGE: {language_pack["language"]}
145
+ SCENARIO: {scenario["name"]}
146
+ EXPECTATIONS:
147
+ {json.dumps(scenario["rules"], indent=2)}
148
+
149
+ USER SAID:
150
+ "{transcript_turn}"
151
+
152
+ Return STRICT JSON:
153
+ {{
154
+ "meter_delta": {{
155
+ "respect": <int>,
156
+ "influence": <int>,
157
+ "trust": <int>
158
+ }},
159
+ "feedback": "<short cultural feedback>",
160
+ "checkpoint_required": <true|false>
161
+ }}
162
+ """
163
  try:
164
+ response = gemini_client.models.generate_content(
165
+ model=MODEL_NAME,
166
+ contents=prompt
167
+ )
168
+ return json.loads(response.text.strip().lstrip("```json").rstrip("```"))
169
  except Exception as e:
170
+ logger.error(f"Gemini eval failed: {e}")
171
  return {
172
+ "meter_delta": {"respect": 0, "influence": 0, "trust": 0},
173
+ "feedback": "Evaluation unavailable.",
174
+ "checkpoint_required": False
175
  }
176
 
177
+ # ============================================================
178
+ # AUDIO SANITIZER (Azure requirement)
179
+ # ============================================================
180
+
181
+ def sanitize_audio(raw_path):
182
+ clean_path = raw_path + "_clean.wav"
183
+ cmd = [
184
+ "ffmpeg", "-y", "-v", "error",
185
+ "-i", raw_path,
186
+ "-ac", "1",
187
+ "-ar", "16000",
188
+ "-acodec", "pcm_s16le",
189
+ clean_path
190
+ ]
191
+ subprocess.run(cmd, check=True)
192
+ return clean_path
193
+
194
+ # ============================================================
195
+ # REST ENDPOINTS
196
+ # ============================================================
197
+
198
+ @app.route("/api/session/start", methods=["POST"])
199
+ def start_session():
 
 
 
 
 
 
 
 
 
 
 
200
  try:
201
+ uid = require_user()
202
+ data = request.get_json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
+ language = data["language"]
205
+ scenario_id = data["scenarioId"]
206
 
207
+ pack = LANGUAGE_PACKS[language]
208
+ scenario = pack["scenarios"][scenario_id]
209
+ title = scenario["title"]
210
+
211
+ charge(uid, START_SESSION_COST)
212
+
213
+ session = create_session(uid, language, scenario_id, title)
214
+
215
+ return jsonify({
216
+ "session": session,
217
+ "dynamicVariables": {
218
+ "title": title,
219
+ "language": pack["language"],
220
+ "scenarioName": scenario["name"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  }
222
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
 
 
 
 
 
 
 
224
  except Exception as e:
225
+ return jsonify({"error": str(e)}), 400
 
226
 
227
+ @app.route("/api/session/turn", methods=["POST"])
228
+ def submit_turn():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  try:
230
+ uid = require_user()
231
+ data = request.get_json()
 
 
 
 
 
 
232
 
233
+ session_id = data["sessionId"]
234
+ transcript = data["transcript"]
235
+
236
+ session = get_session(uid, session_id)
237
+ pack = LANGUAGE_PACKS[session["language"]]
238
+ scenario = pack["scenarios"][session["scenarioId"]]
239
+
240
+ result = evaluate_turn(pack, scenario, transcript)
241
+
242
+ meters = session["meters"]
243
+ for k in meters:
244
+ meters[k] = max(0, min(100, meters[k] + result["meter_delta"][k]))
245
+
246
+ session["turns"].append({
247
+ "text": transcript,
248
+ "feedback": result["feedback"]
249
+ })
250
+
251
+ update_session(uid, session_id, {
252
+ "meters": meters,
253
+ "turns": session["turns"]
254
+ })
255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  return jsonify({
257
+ "meters": meters,
258
+ "feedback": result["feedback"],
259
+ "checkpointRequired": result["checkpoint_required"]
260
+ })
261
+
262
  except Exception as e:
263
+ return jsonify({"error": str(e)}), 400
 
264
 
265
+ @app.route("/api/session/end", methods=["POST"])
266
+ def end_session():
 
 
267
  try:
268
+ uid = require_user()
269
+ data = request.get_json()
270
+ session_id = data["sessionId"]
271
+ duration = data["durationSeconds"]
272
+
273
+ cost = math.ceil(duration / 60) * PER_MINUTE_COST
274
+ charge(uid, cost)
275
+
276
+ return jsonify({"status": "completed", "cost": cost})
277
+
278
  except Exception as e:
279
+ return jsonify({"error": str(e)}), 400
 
280
 
281
+ # ============================================================
282
+ # WEBSOCKET AZURE PRONUNCIATION
283
+ # ============================================================
284
 
285
+ @socketio.on("practice_pronunciation")
286
+ def practice_pronunciation(data):
 
 
287
  try:
288
+ ref_text = data["text"]
289
+ lang = data.get("lang", "en-US")
290
+
291
+ audio_b64 = data["audio"].split(",")[1]
292
+ audio_bytes = base64.b64decode(audio_b64)
293
+
294
+ with tempfile.NamedTemporaryFile(suffix=".webm", delete=False) as f:
295
+ f.write(audio_bytes)
296
+ raw_path = f.name
297
+
298
+ clean_path = sanitize_audio(raw_path)
299
+
300
+ speech_config = speechsdk.SpeechConfig(
301
+ subscription=AZURE_SPEECH_KEY,
302
+ region=AZURE_SPEECH_REGION
303
+ )
304
+ speech_config.speech_recognition_language = lang
305
+
306
+ audio_config = speechsdk.audio.AudioConfig(filename=clean_path)
307
+ recognizer = speechsdk.SpeechRecognizer(
308
+ speech_config=speech_config,
309
+ audio_config=audio_config
310
+ )
311
+
312
+ pa_config = speechsdk.PronunciationAssessmentConfig(
313
+ reference_text=ref_text,
314
+ grading_system=speechsdk.PronunciationAssessmentGradingSystem.HundredMark,
315
+ granularity=speechsdk.PronunciationAssessmentGranularity.Word,
316
+ enable_miscue=True
317
+ )
318
+ pa_config.apply_to(recognizer)
319
+
320
+ result = recognizer.recognize_once_async().get()
321
+ pa_result = speechsdk.PronunciationAssessmentResult(result)
322
+
323
+ words = [
324
+ {
325
+ "word": w.word,
326
+ "score": w.accuracy_score,
327
+ "error": w.error_type
328
+ }
329
+ for w in pa_result.words
330
+ ]
331
+
332
+ emit("pronunciation_result", {
333
+ "accuracy": pa_result.accuracy_score,
334
+ "fluency": pa_result.fluency_score,
335
+ "words": words
336
  })
 
 
337
 
338
+ except Exception as e:
339
+ emit("pronunciation_result", {"error": "Pronunciation failed"})
340
+
341
+ # ============================================================
342
+ # MAIN
343
+ # ============================================================
344
+
345
+ if __name__ == "__main__":
346
+ socketio.run(app, host="0.0.0.0", port=7860)