Spaces:
Sleeping
Sleeping
File size: 56,730 Bytes
9337b76 14bcf8b 9337b76 db3b812 9337b76 6876ebd 9337b76 6ef20f2 9337b76 6ef20f2 9337b76 b73e009 9a9e395 9337b76 3ee3854 9337b76 6ef20f2 9337b76 131a7ca dfb773b 131a7ca 9d4f581 dfb773b 9d4f581 dfb773b e353eec 9d4f581 89f7839 dfb773b 89f7839 dfb773b 89f7839 dfb773b 89f7839 dfb773b 89f7839 dfb773b 89f7839 dfb773b 89f7839 e353eec dfb773b 89f7839 dfb773b 89f7839 dfb773b 89f7839 dfb773b 89f7839 e353eec dfb773b e353eec dfb773b e353eec dfb773b 89f7839 e353eec dfb773b 89f7839 dfb773b 89f7839 9d4f581 e353eec dfb773b e353eec 9d4f581 9337b76 5ef257e 9337b76 cdff30c 9337b76 bd7ea2f 9337b76 cdff30c 9337b76 bd7ea2f cdff30c bd7ea2f cdff30c b899d5b cdff30c 9337b76 cdff30c 9337b76 cdff30c 9337b76 cdff30c 9337b76 cdff30c b899d5b cdff30c bd7ea2f cdff30c b899d5b cdff30c bd7ea2f b899d5b 9a9e395 b899d5b cdff30c 9337b76 cdff30c b899d5b bd7ea2f cdff30c 3ff9df9 bd7ea2f 3ff9df9 bd7ea2f 3ff9df9 bd7ea2f b899d5b bd7ea2f 3ff9df9 cdff30c b899d5b cdff30c b899d5b bd7ea2f 9337b76 cdff30c 9337b76 b899d5b cdff30c b899d5b 9337b76 cdff30c 9337b76 cdff30c b899d5b cdff30c b899d5b bd7ea2f b899d5b cdff30c b899d5b cdff30c bd7ea2f cdff30c b899d5b cdff30c 367e4d8 b899d5b 367e4d8 9337b76 cdff30c bd7ea2f cdff30c 7408586 cdff30c 7408586 9337b76 cdff30c 7408586 9337b76 45f8ce2 0911208 1c5a346 45f8ce2 8f77489 1c5a346 7491ab4 8f77489 7491ab4 1c5a346 8f77489 1c5a346 45f8ce2 7491ab4 1c5a346 45f8ce2 8f77489 7491ab4 1c5a346 7491ab4 8f77489 1c5a346 7491ab4 1c5a346 7491ab4 1c5a346 7491ab4 8f77489 7491ab4 8f77489 1c5a346 8f77489 1c5a346 97cdc57 1c5a346 e5c8e12 7491ab4 e5c8e12 8f77489 7491ab4 8f77489 e5c8e12 97cdc57 8f77489 97cdc57 e5c8e12 7491ab4 e5c8e12 7491ab4 e5c8e12 8f77489 7491ab4 8f77489 e5c8e12 1c5a346 8f77489 1c5a346 8f77489 1c5a346 0911208 1c5a346 d160019 1c5a346 97cdc57 1c5a346 d160019 1c5a346 97cdc57 1c5a346 d160019 1c5a346 97cdc57 1c5a346 97cdc57 d160019 9337b76 97cdc57 1c5a346 97cdc57 1c5a346 97cdc57 1c5a346 97cdc57 d160019 97cdc57 d160019 97cdc57 1c5a346 97cdc57 d160019 97cdc57 d160019 97cdc57 1c5a346 d160019 97cdc57 d160019 1c5a346 0911208 1c5a346 0911208 9337b76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 | import os
import io
import uuid
import re
import time
import json
import traceback
import wave
from datetime import datetime, timedelta
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
import firebase_admin
from firebase_admin import credentials, db, storage, auth
from PIL import Image
from io import BytesIO
import requests
from elevenlabs import ElevenLabs
# Import and configure Google GenAI, matching the Streamlit app
from google import genai
from google.genai import types
# -----------------------------------------------------------------------------
# 1. CONFIGURATION & INITIALIZATION
# -----------------------------------------------------------------------------
# Initialize Flask app and CORS
app = Flask(__name__)
CORS(app)
# --- Firebase Initialization ---
try:
credentials_json_string = os.environ.get("FIREBASE")
if not credentials_json_string:
raise ValueError("The FIREBASE environment variable is not set.")
credentials_json = json.loads(credentials_json_string)
firebase_db_url = os.environ.get("Firebase_DB")
firebase_storage_bucket = os.environ.get("Firebase_Storage")
if not firebase_db_url or not firebase_storage_bucket:
raise ValueError("Firebase_DB and Firebase_Storage environment variables must be set.")
cred = credentials.Certificate(credentials_json)
firebase_admin.initialize_app(cred, {
'databaseURL': firebase_db_url,
'storageBucket': firebase_storage_bucket
})
print("Firebase Admin SDK initialized successfully.")
except Exception as e:
print(f"FATAL: Error initializing Firebase: {e}")
exit(1)
# Initialize Firebase services
bucket = storage.bucket()
db_ref = db.reference()
# --- Google GenAI Client Initialization (as per Streamlit app) ---
try:
api_key = os.environ.get("Gemini")
if not api_key:
raise ValueError("The 'Gemini' environment variable for the API key is not set.")
client = genai.Client(api_key=api_key)
print("Google GenAI Client initialized successfully.")
except Exception as e:
print(f"FATAL: Error initializing GenAI Client: {e}")
exit(1)
# --- Model Constants (as per Streamlit app) ---
CATEGORY_MODEL = "gemini-2.0-flash-exp"
GENERATION_MODEL = "gemini-2.0-flash-exp-image-generation"
#TTS_MODEL = "gemini-2.5-flash-preview-tts"
# -----------------------------------------------------------------------------
# 2. HELPER FUNCTIONS (Adapted directly from Streamlit App & Template)
# -----------------------------------------------------------------------------
def verify_token(auth_header):
"""Verifies the Firebase ID token from the Authorization header."""
if not auth_header or not auth_header.startswith('Bearer '):
return None
token = auth_header.split('Bearer ')[1]
try:
decoded_token = auth.verify_id_token(token)
return decoded_token['uid']
except Exception as e:
print(f"Token verification failed: {e}")
return None
def verify_admin(auth_header):
"""Verifies if the user is an admin."""
uid = verify_token(auth_header)
if not uid:
raise PermissionError('Invalid or missing user token')
user_ref = db_ref.child(f'users/{uid}')
user_data = user_ref.get()
if not user_data or not user_data.get('is_admin', False):
raise PermissionError('Admin access required')
return uid
def upload_to_storage(data_bytes, destination_blob_name, content_type):
"""Uploads a bytes object to Firebase Storage and returns its public URL."""
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(data_bytes, content_type=content_type)
blob.make_public()
return blob.public_url
def parse_numbered_steps(text):
"""Helper to parse numbered steps out of Gemini text."""
text = "\n" + text
steps_found = re.findall(r"\n\s*(\d+)\.\s*(.*)", text, re.MULTILINE)
return [{"stepNumber": int(num), "text": desc.strip()} for num, desc in steps_found]
def _convert_pcm_to_wav(pcm_data, sample_rate=24000, channels=1, sample_width=2):
"""Wraps raw PCM audio data in a WAV container in memory."""
audio_buffer = io.BytesIO()
with wave.open(audio_buffer, 'wb') as wf:
wf.setnchannels(channels)
wf.setsampwidth(sample_width)
wf.setframerate(sample_rate)
wf.writeframes(pcm_data)
audio_buffer.seek(0)
return audio_buffer.getvalue()
#Gemini tts implementation SOTA but slow
'''
def generate_tts_audio_and_upload(text_to_speak, uid, project_id, step_num):
"""Generates audio using the exact method from the Streamlit app and uploads it."""
try:
response = client.models.generate_content(
model=TTS_MODEL,
contents=f"""You are an articulate AI assistant — confident and precise like Jarvis.Rephrase the instruction naturally using simple expert language.
Speak with a brisk, clear British accent.
Avoid reading word for word — explain it like you know it.
No quips or acknowledging the prompt just narrate this step:
{text_to_speak}""",
config=types.GenerateContentConfig(
response_modalities=["AUDIO"],
speech_config=types.SpeechConfig(
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name='Sadaltager')
)
),
)
)
audio_part = response.candidates[0].content.parts[0]
audio_data = audio_part.inline_data.data
mime_type = audio_part.inline_data.mime_type
final_audio_bytes = _convert_pcm_to_wav(audio_data) if 'pcm' in mime_type else audio_data
audio_path = f"users/{uid}/projects/{project_id}/narrations/step_{step_num}.wav"
return upload_to_storage(final_audio_bytes, audio_path, 'audio/wav')
except Exception as e:
print(f"Error during TTS generation for step {step_num}: {e}")
return None
'''
# DeepGram faster and efficient
def generate_tts_audio_and_upload(text_to_speak, uid, project_id, step_num):
"""
Generates audio using the Deepgram TTS API and uploads it to Firebase Storage.
This is a drop-in replacement for the previous Google GenAI TTS function.
"""
try:
# --- Step 1: Get the Deepgram API Key from environment variables ---
api_key = os.environ.get("DEEPGRAM_API_KEY")
if not api_key:
print("FATAL: DEEPGRAM_API_KEY environment variable not set.")
return None
# --- Step 2: Define the API endpoint and headers ---
# The model 'aura-2-draco-en' is specified as a query parameter in the URL.
DEEPGRAM_URL = "https://api.deepgram.com/v1/speak?model=aura-2-draco-en"
headers = {
"Authorization": f"Token {api_key}",
"Content-Type": "text/plain" # As per Deepgram's requirement for this type of request
}
# --- Step 3: Make the API call to Deepgram ---
# Deepgram expects the raw text as the request body, not in a JSON object.
# We send the text directly in the 'data' parameter.
response = requests.post(DEEPGRAM_URL, headers=headers, data=text_to_speak.encode('utf-8'))
# Raise an exception for bad status codes (4xx or 5xx)
response.raise_for_status()
# The raw audio data is in the response content
audio_data = response.content
# --- Step 4: Upload the received audio to Firebase Storage ---
# The output format from this Deepgram model is MP3.
audio_path = f"users/{uid}/projects/{project_id}/narrations/step_{step_num}.mp3"
# The MIME type for MP3 is 'audio/mpeg'.
narration_url = upload_to_storage(audio_data, audio_path, 'audio/mpeg')
return narration_url
except requests.exceptions.RequestException as e:
print(f"Error during Deepgram API call for step {step_num}: {e}")
# Log the response body if available for more detailed error info
if e.response is not None:
print(f"Deepgram Error Response: {e.response.text}")
return None
except Exception as e:
print(f"An unexpected error occurred during TTS generation for step {step_num}: {e}")
return None
def send_text_request(model_name, prompt, image):
"""Helper to send requests that expect only a text response."""
try:
chat = client.chats.create(model=model_name)
response = chat.send_message([prompt, image])
response_text = "".join(part.text for part in response.candidates[0].content.parts if hasattr(part, 'text'))
return response_text.strip()
except Exception as e:
print(f"Error with model {model_name}: {e}")
return None
import logging
# Configure logging at the top of your file if not already done
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# =============================================================================
# OPEN IMAGE PROXY ENDPOINT (NO AUTHENTICATION)
# =============================================================================
@app.route('/api/image-proxy', methods=['GET'])
def image_proxy():
image_url = request.args.get('url')
logger.info(f"[IMAGE PROXY] Received URL: {image_url}")
if not image_url:
logger.error("[IMAGE PROXY] ERROR: URL parameter is missing")
return jsonify({'error': 'URL parameter is missing.'}), 400
try:
# Parse Firebase Storage URL
# Expected format: https://storage.googleapis.com/bucket-name/path/to/file.ext
if 'storage.googleapis.com' not in image_url:
logger.error(f"[IMAGE PROXY] ERROR: Invalid Firebase Storage URL: {image_url}")
return jsonify({'error': 'Invalid Firebase Storage URL.'}), 400
logger.info(f"[IMAGE PROXY] Parsing URL: {image_url}")
# Extract bucket name and blob path from the URL
url_parts = image_url.split('storage.googleapis.com/')[1]
logger.info(f"[IMAGE PROXY] URL parts after split: {url_parts}")
# Remove query parameters if present
url_parts = url_parts.split('?')[0]
logger.info(f"[IMAGE PROXY] URL parts after removing query params: {url_parts}")
# Split into bucket name and blob path
path_components = url_parts.split('/', 1)
logger.info(f"[IMAGE PROXY] Path components: {path_components}")
if len(path_components) < 2:
logger.error(f"[IMAGE PROXY] ERROR: Invalid URL format - path_components: {path_components}")
return jsonify({'error': 'Invalid URL format.'}), 400
url_bucket_name = path_components[0]
blob_path = path_components[1]
logger.info(f"[IMAGE PROXY] Extracted bucket name: {url_bucket_name}")
logger.info(f"[IMAGE PROXY] Extracted blob path: {blob_path}")
# Verify bucket name matches (optional security check)
expected_bucket_name = bucket.name
logger.info(f"[IMAGE PROXY] Expected bucket name: {expected_bucket_name}")
if url_bucket_name != expected_bucket_name:
logger.error(f"[IMAGE PROXY] ERROR: Bucket name mismatch - URL: {url_bucket_name}, Expected: {expected_bucket_name}")
return jsonify({'error': 'Bucket name mismatch.'}), 403
logger.info(f"[IMAGE PROXY] Creating blob object for path: {blob_path}")
# Get the blob
blob = bucket.blob(blob_path)
logger.info(f"[IMAGE PROXY] Checking if blob exists...")
if not blob.exists():
logger.error(f"[IMAGE PROXY] ERROR: Image not found at path: {blob_path}")
return jsonify({'error': 'Image not found.'}), 404
logger.info(f"[IMAGE PROXY] Downloading blob...")
# Download and return the image
image_bytes = blob.download_as_bytes()
content_type = blob.content_type or 'application/octet-stream'
logger.info(f"[IMAGE PROXY] Successfully downloaded {len(image_bytes)} bytes, content-type: {content_type}")
# Add cache headers for better performance
response = Response(image_bytes, content_type=content_type)
response.headers['Cache-Control'] = 'public, max-age=3600' # Cache for 1 hour
return response
except IndexError as e:
logger.error(f"[IMAGE PROXY] URL parsing IndexError: {e}")
logger.error(f"[IMAGE PROXY] URL was: {image_url}")
return jsonify({'error': 'Invalid URL format.'}), 400
except Exception as e:
# This will catch parsing errors or other unexpected issues.
logger.error(f"[IMAGE PROXY] Unexpected error: {e}")
logger.error(f"[IMAGE PROXY] Error type: {type(e).__name__}")
logger.error(f"[IMAGE PROXY] URL was: {image_url}")
import traceback
logger.error(f"[IMAGE PROXY] Full traceback: {traceback.format_exc()}")
return jsonify({'error': 'Internal server error processing the image request.'}), 500
# -----------------------------------------------------------------------------
# 3. AUTHENTICATION & USER MANAGEMENT
# -----------------------------------------------------------------------------
@app.route('/api/auth/signup', methods=['POST'])
def signup():
try:
data = request.get_json()
email, password = data.get('email'), data.get('password')
if not email or not password: return jsonify({'error': 'Email and password are required'}), 400
user = auth.create_user(email=email, password=password)
user_ref = db_ref.child(f'users/{user.uid}')
user_data = {'email': email, 'credits': 15, 'is_admin': False, 'createdAt': datetime.utcnow().isoformat()}
user_ref.set(user_data)
return jsonify({'success': True, 'uid': user.uid, **user_data}), 201
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/api/auth/social-signin', methods=['POST'])
def social_signin():
"""
Ensures a user record exists in the Realtime Database after a social login
(like Google Sign-In). The client should call this endpoint immediately after
a successful Firebase authentication on their side, sending the
Firebase ID Token. This creates the user's profile in our database if it's
their first time.
"""
uid = verify_token(request.headers.get('Authorization'))
if not uid:
return jsonify({'error': 'Invalid or expired token'}), 401
user_ref = db_ref.child(f'users/{uid}')
user_data = user_ref.get()
if user_data:
# User already has a profile in our DB, return it.
return jsonify({'uid': uid, **user_data}), 200
else:
# This is a new user (first social login), create their profile.
try:
# Get user details from Firebase Auth service. [8]
firebase_user = auth.get_user(uid)
# Create the user profile in our Realtime Database
new_user_data = {
'email': firebase_user.email,
'credits': 15, # Standard starting credits
'is_admin': False,
'createdAt': datetime.utcnow().isoformat()
}
user_ref.set(new_user_data)
# Return the newly created profile
return jsonify({'success': True, 'uid': uid, **new_user_data}), 201
except Exception as e:
print(f"Error creating profile for new social user {uid}: {e}")
return jsonify({'error': f'Failed to create user profile: {str(e)}'}), 500
@app.route('/api/user/profile', methods=['GET'])
def get_user_profile():
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
user_data = db_ref.child(f'users/{uid}').get()
if not user_data: return jsonify({'error': 'User not found'}), 404
return jsonify({'uid': uid, **user_data})
# -----------------------------------------------------------------------------
# 4. FEEDBACK AND CREDIT REQUESTS (USER-FACING)
# -----------------------------------------------------------------------------
@app.route('/api/feedback', methods=['POST'])
def submit_feedback():
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
try:
data = request.get_json()
if not data or not data.get('message'): return jsonify({'error': 'Message is required'}), 400
user_email = (db_ref.child(f'users/{uid}').get() or {}).get('email', 'unknown')
feedback_ref = db_ref.child('feedback').push()
feedback_record = {
"feedbackId": feedback_ref.key,
"userId": uid,
"userEmail": user_email,
"type": data.get('type', 'general'),
"message": data.get('message'),
"createdAt": datetime.utcnow().isoformat(),
"status": "open"
}
feedback_ref.set(feedback_record)
return jsonify({"success": True, "feedbackId": feedback_ref.key}), 201
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/user/request-credits', methods=['POST'])
def request_credits():
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
try:
data = request.get_json()
if not data or 'requested_credits' not in data: return jsonify({'error': 'requested_credits is required'}), 400
request_ref = db_ref.child('credit_requests').push()
request_ref.set({
'requestId': request_ref.key,
'userId': uid,
'requested_credits': data['requested_credits'],
'status': 'pending',
'requestedAt': datetime.utcnow().isoformat()
})
return jsonify({'success': True, 'requestId': request_ref.key})
except Exception as e:
return jsonify({'error': str(e)}), 500
# -----------------------------------------------------------------------------
# 5. ADMIN ENDPOINTS
# -----------------------------------------------------------------------------
@app.route('/api/admin/profile', methods=['GET'])
def get_admin_profile():
try:
admin_uid = verify_admin(request.headers.get('Authorization'))
# Fetch all necessary data from Firebase in one go
all_users = db_ref.child('users').get() or {}
all_projects = db_ref.child('projects').get() or {}
all_feedback = db_ref.child('feedback').get() or {}
all_credit_requests = db_ref.child('credit_requests').get() or {}
# --- User Statistics Calculation ---
total_users = len(all_users)
admin_count = 0
total_credits_in_system = 0
new_users_last_7_days = 0
seven_days_ago = datetime.utcnow() - timedelta(days=7)
for user_data in all_users.values():
if user_data.get('is_admin', False):
admin_count += 1
total_credits_in_system += user_data.get('credits', 0)
# Check for new users
try:
created_at_str = user_data.get('createdAt')
if created_at_str:
# Accommodate different possible ISO formats
user_created_at = datetime.fromisoformat(created_at_str.replace('Z', '+00:00'))
if user_created_at.replace(tzinfo=None) > seven_days_ago:
new_users_last_7_days += 1
except (ValueError, TypeError):
# Ignore if date format is invalid or missing
pass
# --- Project Statistics Calculation ---
total_projects = len(all_projects)
projects_by_status = {
"awaiting_approval": 0,
"awaiting_selection": 0,
"ready": 0,
"unknown": 0
}
projects_by_category = {}
for project_data in all_projects.values():
# Tally by status
status = project_data.get('status', 'unknown')
projects_by_status[status] = projects_by_status.get(status, 0) + 1
# Tally by category
category = project_data.get('category', 'N/A')
projects_by_category[category] = projects_by_category.get(category, 0) + 1
# --- System Health Calculation ---
open_feedback_count = sum(1 for fb in all_feedback.values() if fb.get('status') == 'open')
pending_requests_count = sum(1 for req in all_credit_requests.values() if req.get('status') == 'pending')
# Assemble the final response object
admin_personal_data = all_users.get(admin_uid, {})
response_data = {
'uid': admin_uid,
'email': admin_personal_data.get('email'),
'credits': admin_personal_data.get('credits'),
'is_admin': True,
'dashboardStats': {
'users': {
'total': total_users,
'admins': admin_count,
'regular': total_users - admin_count,
'newLast7Days': new_users_last_7_days,
'totalCreditsInSystem': total_credits_in_system
},
'projects': {
'total': total_projects,
'byStatus': projects_by_status,
'byCategory': projects_by_category
},
'system': {
'openFeedback': open_feedback_count,
'pendingCreditRequests': pending_requests_count
}
}
}
return jsonify(response_data), 200
except PermissionError as e:
return jsonify({'error': str(e)}), 403 # Use 403 Forbidden for permission issues
except Exception as e:
print(traceback.format_exc())
return jsonify({'error': f"An internal error occurred: {e}"}), 500
@app.route('/api/admin/credit_requests', methods=['GET'])
def list_credit_requests():
try:
verify_admin(request.headers.get('Authorization'))
requests_data = db_ref.child('credit_requests').get() or {}
return jsonify(list(requests_data.values()))
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/admin/credit_requests/<string:request_id>', methods=['PUT'])
def process_credit_request(request_id):
try:
admin_uid = verify_admin(request.headers.get('Authorization'))
req_ref = db_ref.child(f'credit_requests/{request_id}')
req_data = req_ref.get()
if not req_data: return jsonify({'error': 'Credit request not found'}), 404
decision = request.json.get('decision')
if decision not in ['approved', 'declined']: return jsonify({'error': 'Decision must be "approved" or "declined"'}), 400
if decision == 'approved':
user_ref = db_ref.child(f'users/{req_data["userId"]}')
user_data = user_ref.get()
if user_data:
new_total = user_data.get('credits', 0) + int(req_data.get('requested_credits', 0))
user_ref.update({'credits': new_total})
req_ref.update({'status': decision, 'processedBy': admin_uid, 'processedAt': datetime.utcnow().isoformat()})
return jsonify({'success': True, 'message': f'Request {decision}.'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/admin/feedback', methods=['GET'])
def admin_view_feedback():
try:
verify_admin(request.headers.get('Authorization'))
feedback_data = db_ref.child('feedback').get() or {}
return jsonify(list(feedback_data.values()))
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/admin/users', methods=['GET'])
def admin_list_users():
try:
verify_admin(request.headers.get('Authorization'))
all_users = db_ref.child('users').get() or {}
user_list = [{'uid': uid, **data} for uid, data in all_users.items()]
return jsonify(user_list)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/admin/users/<string:uid>/credits', methods=['PUT'])
def admin_update_credits(uid):
try:
verify_admin(request.headers.get('Authorization'))
add_credits = request.json.get('add_credits')
if add_credits is None: return jsonify({'error': 'add_credits is required'}), 400
user_ref = db_ref.child(f'users/{uid}')
user_data = user_ref.get()
if not user_data: return jsonify({'error': 'User not found'}), 404
new_total = user_data.get('credits', 0) + float(add_credits)
user_ref.update({'credits': new_total})
return jsonify({'success': True, 'new_total_credits': new_total})
except Exception as e:
return jsonify({'error': str(e)}), 500
# -----------------------------------------------------------------------------
# 6. DIY PROJECT ENDPOINTS (Core Logic)
# -----------------------------------------------------------------------------
# (The project endpoints from the previous answer go here, unchanged)
@app.route('/api/projects', methods=['POST'])
def create_project():
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Unauthorized'}), 401
user_ref = db_ref.child(f'users/{uid}')
user_data = user_ref.get()
if not user_data or user_data.get('credits', 0) < 1:
return jsonify({'error': 'Insufficient credits'}), 402
if 'image' not in request.files:
return jsonify({'error': 'Image file is required'}), 400
image_file = request.files['image']
context_text = request.form.get('contextText', '')
image_bytes = image_file.read()
pil_image = Image.open(io.BytesIO(image_bytes))
try:
category_prompt = (
"You are an expert DIY assistant. Analyze the user's image and context. "
f"Context: '{context_text}'. "
"Categorize the project into ONE of the following: "
"Home Appliance Repair, Automotive Maintenance, Gardening & Urban Farming, "
"Upcycling & Sustainable Crafts, or DIY Project Creation. "
"Reply with ONLY the category name."
)
category = send_text_request(CATEGORY_MODEL, category_prompt, pil_image)
if not category: return jsonify({'error': 'Failed to get project category from AI.'}), 500
plan_prompt = f"""
You are an expert DIY assistant in the category: {category}.
User Context: "{context_text if context_text else 'No context provided.'}"
Based on the image and context, perform the following:
1. **Title:** Create a short, clear title for this project.
2. **Description:** Write a brief, one-paragraph description of the goal.
3. **Initial Plan:**
- If 'Upcycling & Sustainable Crafts' AND no specific project is mentioned, propose three distinct project options as a numbered list under "UPCYCLING OPTIONS:".
- For all other cases, briefly outline the main stages of the proposed solution.
Structure your response EXACTLY like this:
TITLE: [Your title]
DESCRIPTION: [Your description]
INITIAL PLAN:
[Your plan or 3 options]
"""
plan_response = send_text_request(GENERATION_MODEL, plan_prompt, pil_image)
if not plan_response: return jsonify({'error': 'Failed to generate project plan from AI.'}), 500
title = re.search(r"TITLE:\s*(.*)", plan_response).group(1).strip()
description = re.search(r"DESCRIPTION:\s*(.*)", plan_response, re.DOTALL).group(1).strip()
initial_plan_text = re.search(r"INITIAL PLAN:\s*(.*)", plan_response, re.DOTALL).group(1).strip()
upcycling_options = re.findall(r"^\s*\d+\.\s*(.*)", initial_plan_text, re.MULTILINE) if "UPCYCLING OPTIONS:" in initial_plan_text else []
initial_plan = initial_plan_text if not upcycling_options else ""
status = "awaiting_selection" if upcycling_options else "awaiting_approval"
project_id = str(uuid.uuid4())
image_path = f"users/{uid}/projects/{project_id}/initial_image.png"
image_url = upload_to_storage(image_bytes, image_path, content_type=image_file.content_type)
project_data = {
"uid": uid, "projectId": project_id, "status": status, "createdAt": datetime.utcnow().isoformat(),
"userImageURL": image_url, "contextText": context_text, "projectTitle": title,
"projectDescription": description, "category": category, "initialPlan": initial_plan,
"upcyclingOptions": upcycling_options, "toolsList": [], "steps": []
}
db_ref.child(f'projects/{project_id}').set(project_data)
user_ref.update({'credits': user_data.get('credits', 0) - 1})
return jsonify(project_data), 201
except Exception as e:
print(traceback.format_exc())
return jsonify({'error': f"An error occurred: {e}"}), 500
@app.route('/api/projects/<string:project_id>/approve', methods=['PUT'])
def approve_project_plan(project_id):
start_time = time.time()
logger.info(f"[PROJECT APPROVAL] Starting approval process for project: {project_id}")
# Authorization timing
auth_start = time.time()
uid = verify_token(request.headers.get('Authorization'))
if not uid:
logger.error(f"[PROJECT APPROVAL] ERROR: Unauthorized access attempt for project: {project_id}")
return jsonify({'error': 'Unauthorized'}), 401
auth_time = time.time() - auth_start
logger.info(f"[PROJECT APPROVAL] Authorization completed in {auth_time:.3f}s for user: {uid}")
# User data fetch timing
user_fetch_start = time.time()
user_ref = db_ref.child(f'users/{uid}')
user_data = user_ref.get()
if not user_data or user_data.get('credits', 0) < 5:
logger.error(f"[PROJECT APPROVAL] ERROR: Insufficient credits for user: {uid}, credits: {user_data.get('credits', 0) if user_data else 0}")
return jsonify({'error': 'Insufficient credits'}), 402
user_fetch_time = time.time() - user_fetch_start
logger.info(f"[PROJECT APPROVAL] User data fetch completed in {user_fetch_time:.3f}s, credits: {user_data.get('credits', 0)}")
# Project data fetch timing
project_fetch_start = time.time()
project_ref = db_ref.child(f'projects/{project_id}')
project_data = project_ref.get()
if not project_data or project_data.get('uid') != uid:
logger.error(f"[PROJECT APPROVAL] ERROR: Project not found or access denied - project_id: {project_id}, uid: {uid}")
return jsonify({'error': 'Project not found or access denied'}), 404
project_fetch_time = time.time() - project_fetch_start
logger.info(f"[PROJECT APPROVAL] Project data fetch completed in {project_fetch_time:.3f}s for project: {project_data.get('projectTitle', 'Unknown')}")
# Image download and processing timing
selected_option = request.json.get('selectedOption')
logger.info(f"[PROJECT APPROVAL] Selected option: {selected_option}")
image_download_start = time.time()
response = requests.get(project_data['userImageURL'])
image_download_time = time.time() - image_download_start
logger.info(f"[PROJECT APPROVAL] Image download completed in {image_download_time:.3f}s, size: {len(response.content)} bytes")
image_processing_start = time.time()
pil_image = Image.open(io.BytesIO(response.content)).convert('RGB')
image_processing_time = time.time() - image_processing_start
logger.info(f"[PROJECT APPROVAL] Image processing completed in {image_processing_time:.3f}s")
# Context preparation timing
context_start = time.time()
context = (
f"The user chose the upcycling project: '{selected_option}'."
if selected_option
else f"The user has approved the plan for '{project_data['projectTitle']}'."
)
detailed_prompt = f"""
You are a DIY expert. The user wants to proceed with the project titled "{project_data['projectTitle']}".
{context}
Provide a detailed guide. For each step, you MUST provide a simple, clear illustrative image.
Format your response EXACTLY like this:
TOOLS AND MATERIALS:
- Tool A
- Material B
STEPS(Maximum 5 steps):
1. First step instructions.
2. Second step instructions...
"""
context_time = time.time() - context_start
logger.info(f"[PROJECT APPROVAL] Context preparation completed in {context_time:.3f}s")
try:
# AI generation timing
ai_start = time.time()
logger.info(f"[PROJECT APPROVAL] Starting AI generation with model: {GENERATION_MODEL}")
chat = client.chats.create(
model=GENERATION_MODEL,
config=types.GenerateContentConfig(response_modalities=["Text", "Image"])
)
full_resp = chat.send_message([detailed_prompt, pil_image])
ai_time = time.time() - ai_start
logger.info(f"[PROJECT APPROVAL] AI generation completed in {ai_time:.3f}s")
# Response parsing timing
parsing_start = time.time()
gen_parts = full_resp.candidates[0].content.parts
combined_text = ""
inline_images = []
for part in gen_parts:
if part.text is not None:
combined_text += part.text + "\n"
if part.inline_data is not None:
img = Image.open(io.BytesIO(part.inline_data.data)).convert('RGB')
inline_images.append(img)
combined_text = combined_text.strip()
parsing_time = time.time() - parsing_start
logger.info(f"[PROJECT APPROVAL] Response parsing completed in {parsing_time:.3f}s, found {len(inline_images)} images")
# Text extraction timing
extraction_start = time.time()
tools_section = re.search(r"TOOLS AND MATERIALS:\s*(.*?)\s*STEPS:", combined_text, re.DOTALL).group(1).strip()
steps_section = re.search(r"STEPS:\s*(.*)", combined_text, re.DOTALL).group(1).strip()
tools_list = [line.strip("- ").strip() for line in tools_section.split('\n') if line.strip()]
parsed_steps = parse_numbered_steps(steps_section)
extraction_time = time.time() - extraction_start
logger.info(f"[PROJECT APPROVAL] Text extraction completed in {extraction_time:.3f}s, tools: {len(tools_list)}, steps: {len(parsed_steps)}")
if len(parsed_steps) != len(inline_images):
logger.error(f"[PROJECT APPROVAL] ERROR: AI response mismatch - Steps: {len(parsed_steps)}, Images: {len(inline_images)}")
return jsonify({'error': 'AI response mismatch: Steps and images do not match.'}), 500
# Step processing timing
step_processing_start = time.time()
final_steps = []
total_upload_time = 0
total_tts_time = 0
for i, step_info in enumerate(parsed_steps):
logger.info(f"[PROJECT APPROVAL] Processing step {i+1}/{len(parsed_steps)}")
# Image upload timing
image_upload_start = time.time()
img_byte_arr = io.BytesIO()
inline_images[i].save(img_byte_arr, format='JPEG', optimize=True, quality=70)
img_path = f"users/{uid}/projects/{project_id}/steps/step_{i+1}_image.jpg"
img_url = upload_to_storage(img_byte_arr.getvalue(), img_path, 'image/jpeg')
image_upload_time = time.time() - image_upload_start
total_upload_time += image_upload_time
logger.info(f"[PROJECT APPROVAL] Step {i+1} image upload completed in {image_upload_time:.3f}s")
# TTS generation timing
tts_start = time.time()
narration_url = generate_tts_audio_and_upload(step_info['text'], uid, project_id, i + 1)
tts_time = time.time() - tts_start
total_tts_time += tts_time
logger.info(f"[PROJECT APPROVAL] Step {i+1} TTS generation completed in {tts_time:.3f}s")
step_info.update({
"imageUrl": img_url,
"narrationUrl": narration_url,
"isDone": False,
"notes": ""
})
final_steps.append(step_info)
step_processing_time = time.time() - step_processing_start
logger.info(f"[PROJECT APPROVAL] All steps processing completed in {step_processing_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] Total upload time: {total_upload_time:.3f}s, Total TTS time: {total_tts_time:.3f}s")
# Database update timing
db_update_start = time.time()
update_data = {
"status": "ready",
"toolsList": tools_list,
"steps": final_steps,
"selectedOption": selected_option or ""
}
project_ref.update(update_data)
db_update_time = time.time() - db_update_start
logger.info(f"[PROJECT APPROVAL] Database update completed in {db_update_time:.3f}s")
# Final project fetch timing
final_fetch_start = time.time()
updated_project = project_ref.get()
updated_project["projectId"] = project_id
final_fetch_time = time.time() - final_fetch_start
logger.info(f"[PROJECT APPROVAL] Final project fetch completed in {final_fetch_time:.3f}s")
# Credits deduction timing
credits_update_start = time.time()
user_ref.update({'credits': user_data.get('credits', 0) - 5})
credits_update_time = time.time() - credits_update_start
logger.info(f"[PROJECT APPROVAL] Credits update completed in {credits_update_time:.3f}s")
# Total time calculation
total_time = time.time() - start_time
logger.info(f"[PROJECT APPROVAL] SUCCESS: Project approval completed in {total_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] TIMING BREAKDOWN:")
logger.info(f"[PROJECT APPROVAL] - Authorization: {auth_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - User fetch: {user_fetch_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Project fetch: {project_fetch_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Image download: {image_download_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Image processing: {image_processing_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Context prep: {context_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - AI generation: {ai_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Response parsing: {parsing_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Text extraction: {extraction_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Step processing: {step_processing_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Total uploads: {total_upload_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Total TTS: {total_tts_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - DB update: {db_update_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Final fetch: {final_fetch_time:.3f}s")
logger.info(f"[PROJECT APPROVAL] - Credits update: {credits_update_time:.3f}s")
return jsonify(updated_project)
except Exception as e:
total_time = time.time() - start_time
logger.error(f"[PROJECT APPROVAL] ERROR: Exception occurred after {total_time:.3f}s: {e}")
logger.error(f"[PROJECT APPROVAL] Error type: {type(e).__name__}")
logger.error(f"[PROJECT APPROVAL] Project ID: {project_id}, User ID: {uid}")
import traceback
logger.error(f"[PROJECT APPROVAL] Full traceback: {traceback.format_exc()}")
return jsonify({'error': str(e)}), 500
@app.route('/api/projects', methods=['GET'])
def list_projects():
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Unauthorized'}), 401
projects = (db_ref.child('projects').order_by_child('uid').equal_to(uid).get() or {}).values()
return jsonify(list(projects))
@app.route('/api/projects/<string:project_id>', methods=['GET'])
def get_project(project_id):
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Unauthorized'}), 401
project_data = db_ref.child(f'projects/{project_id}').get()
if not project_data or project_data.get('uid') != uid:
return jsonify({'error': 'Project not found or access denied'}), 404
return jsonify(project_data)
@app.route('/api/projects/<string:project_id>/step/<int:step_number>', methods=['PUT'])
def update_step(project_id, step_number):
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Unauthorized'}), 401
data = request.get_json()
if data is None: return jsonify({'error': 'JSON body is required'}), 400
project_data = db_ref.child(f'projects/{project_id}').get()
if not project_data or project_data.get('uid') != uid:
return jsonify({'error': 'Project not found or access denied'}), 404
steps = project_data.get('steps', [])
step_index = next((i for i, s in enumerate(steps) if s.get('stepNumber') == step_number), -1)
if step_index == -1: return jsonify({'error': f'Step number {step_number} not found'}), 404
step_path = f'projects/{project_id}/steps/{step_index}'
if 'isDone' in data: db_ref.child(f'{step_path}/isDone').set(bool(data['isDone']))
if 'notes' in data: db_ref.child(f'{step_path}/notes').set(str(data['notes']))
return jsonify({"success": True, "updatedStep": db_ref.child(step_path).get()})
@app.route('/api/projects/<string:project_id>', methods=['DELETE'])
def delete_project(project_id):
uid = verify_token(request.headers.get('Authorization'))
if not uid: return jsonify({'error': 'Unauthorized'}), 401
project_ref = db_ref.child(f'projects/{project_id}')
project_data = project_ref.get()
if not project_data or project_data.get('uid') != uid:
return jsonify({'error': 'Project not found or access denied'}), 404
project_ref.delete()
for blob in bucket.list_blobs(prefix=f"users/{uid}/projects/{project_id}/"):
blob.delete()
return jsonify({"success": True, "message": f"Project {project_id} deleted."})
#------------------------
# AI phone call ElevenLabs
#-------------------------
# Configuration
CREDITS_PER_MIN = 3 # Adjust based on ElevenLabs conversational AI pricing
VOICE_NAME = "Daniel" # British male voice (more reliable than Archer)
# Known ElevenLabs voice IDs for common voices
KNOWN_VOICE_IDS = {
"Daniel": "onwK4e9ZLuTAKqWW03F9", # British male
"Charlie": "IKne3meq5aSn9XLyUdCD", # British male
"George": "JBFqnCBsd6RMkjVDRZzb", # British male
"Archer": "D38z5RcWu1voky8WS1ja", # British male
"Adam": "pNInz6obpgDQGcFmaJgB", # American male (fallback)
"Antoni": "ErXwobaYiN019PkySvjV", # American male (fallback)
}
import os
import time
import requests
import logging
# Assumes 'db_ref', 'logger', and 'KNOWN_VOICE_IDS' are initialized elsewhere in your application.
# from elevenlabs import ElevenLabs # This is no longer needed for the handler's logic.
class ConversationalAIHandler:
def __init__(self):
# CORRECT: The base URL for the API is /v1. All endpoints branch from here.
self.base_url = "https://api.elevenlabs.io/v1"
self.api_key = os.getenv("ELEVENLABS_API_KEY")
if not self.api_key:
logger.critical("[AGENT] ELEVENLABS_API_KEY environment variable not set.")
raise ValueError("ELEVENLABS_API_KEY environment variable not set.")
self.headers = {
"xi-api-key": self.api_key,
"Content-Type": "application/json"
}
def get_british_male_voice_id(self):
"""Get British male voice ID"""
try:
logger.info(f"[VOICE] Using known voice ID for Daniel: {KNOWN_VOICE_IDS['Daniel']}")
return KNOWN_VOICE_IDS["Daniel"]
except KeyError:
logger.error("[VOICE] Voice ID for 'Daniel' not found. Using hardcoded fallback.")
return "onwK4e9ZLuTAKqWW03F9"
def create_or_get_agent(self, project_id, project_data):
"""Create or retrieve existing agent for the project"""
try:
logger.info(f"[AGENT] Starting agent creation/retrieval for project: {project_id}")
existing_agent_id = db_ref.child(f'projects/{project_id}/agent_id').get()
if existing_agent_id:
logger.info(f"[AGENT] Found existing agent ID in DB: {existing_agent_id}. Verifying...")
verify_url = f"{self.base_url}/agents/{existing_agent_id}"
try:
response = requests.get(verify_url, headers=self.headers, timeout=15)
if response.status_code == 200:
logger.info(f"[AGENT] Existing agent {existing_agent_id} verified and active.")
return existing_agent_id
else:
logger.warning(f"[AGENT] Verification failed for agent {existing_agent_id} (Status: {response.status_code}). Creating a new one.")
except Exception as e:
logger.warning(f"[AGENT] Error verifying agent: {e}. Creating a new one.")
logger.info(f"[AGENT] Creating new agent.")
voice_id = self.get_british_male_voice_id()
diy_expert_prompt = f"""
You are an experienced DIY expert... [Your full prompt here] ...
Project: {project_data.get('projectTitle', 'DIY Project')}
Description: {project_data.get('projectDescription', '')}
Initial Plan: {project_data.get('initialPlan', '')}
...
"""
# CORRECT PAYLOAD: The detailed, structured payload is correct.
agent_payload = {
"name": f"DIY Expert - {project_data.get('projectTitle', project_id)}",
"prompt": diy_expert_prompt,
"voice_id": voice_id,
"initial_message": "Hello! I'm your Sozo DIY assistant. I'm here to help you with your project. What would you like to work on today?",
"webhook_url": f"{os.getenv('BASE_URL', 'https://rairo-neofix-api.hf.space')}/api/webhook/agent/{project_id}",
"model": "eleven_turbo_v2"
}
# CORRECT ENDPOINT: The creation URL is /v1/agents.
creation_url = f"{self.base_url}/agents"
logger.info(f"[AGENT] Posting to correct endpoint: {creation_url}")
response = requests.post(creation_url, headers=self.headers, json=agent_payload, timeout=30)
# A successful creation returns HTTP 200 OK.
if response.status_code == 200:
agent_info = response.json()
agent_id = agent_info.get("agent_id")
logger.info(f"[AGENT] New agent created successfully: {agent_id}")
db_ref.child(f'projects/{project_id}').update({
'agent_id': agent_id,
'agent_created_at': int(time.time())
})
logger.info(f"[AGENT] Agent ID stored in database.")
return agent_id
else:
logger.error(f"[AGENT] Failed to create agent. Status: {response.status_code}, Response: {response.text}")
raise Exception(f"Failed to create agent: HTTP {response.status_code} - {response.text}")
except Exception as e:
logger.error(f"[AGENT] An exception occurred in create_or_get_agent: {e}")
raise
def start_conversation(self, agent_id):
"""Start a conversation with the agent"""
try:
logger.info(f"[CONVERSATION] Starting conversation with agent: {agent_id}")
# CORRECT ENDPOINT: Follows the /agents/{id}/conversations pattern.
conversation_url = f"{self.base_url}/agents/{agent_id}/conversations"
response = requests.post(conversation_url, headers=self.headers, json={}, timeout=30)
# A successful start returns HTTP 200 OK.
if response.status_code == 200:
conversation_info = response.json()
conversation_id = conversation_info.get("conversation_id")
logger.info(f"[CONVERSATION] Conversation started successfully: {conversation_id}")
return conversation_id
else:
logger.error(f"[CONVERSATION] Failed to start conversation. Status: {response.status_code}, Response: {response.text}")
raise Exception(f"Failed to start conversation: {response.text}")
except Exception as e:
logger.error(f"[CONVERSATION] An exception occurred in start_conversation: {e}")
raise
def send_message(self, agent_id, conversation_id, message):
"""Send a message to the conversation"""
try:
logger.info(f"[MESSAGE] Sending message to conversation: {conversation_id}")
# CORRECT ENDPOINT: Uses the /conversations/{id}/messages pattern.
message_url = f"{self.base_url}/conversations/{conversation_id}/messages"
# CORRECT PAYLOAD: The payload requires the "text" of the message.
payload = {"text": message}
response = requests.post(message_url, headers=self.headers, json=payload, timeout=30)
if response.status_code == 200:
logger.info(f"[MESSAGE] Message sent successfully.")
return response.json()
else:
logger.error(f"[MESSAGE] Failed to send message. Status: {response.status_code}, Response: {response.text}")
raise Exception(f"Failed to send message: {response.text}")
except Exception as e:
logger.error(f"[MESSAGE] An exception occurred in send_message: {e}")
raise
def calculate_cost(duration_seconds):
"""Calculate cost based on conversation duration"""
minutes = (duration_seconds + 59) // 60
return minutes * CREDITS_PER_MIN
# Your existing Flask route - no changes needed
@app.route('/api/projects/<project_id>/start-conversation', methods=['POST'])
def start_conversation(project_id):
"""Start a conversational AI session for a project"""
start_time = time.time()
logger.info(f"[CONVERSATION] Starting conversation process for project: {project_id}")
try:
# Authorization
uid = verify_token(request.headers.get('Authorization'))
if not uid:
logger.error(f"[CONVERSATION] ERROR: Unauthorized access attempt for project: {project_id}")
return jsonify({'error': 'Unauthorized'}), 401
# Get user data
user_ref = db_ref.child(f'users/{uid}')
user = user_ref.get()
if not user:
logger.error(f"[CONVERSATION] ERROR: User not found for uid: {uid}")
return jsonify({'error': 'User not found'}), 404
# Get project data
project = db_ref.child(f'projects/{project_id}').get()
if not project or project.get('uid') != uid:
logger.error(f"[CONVERSATION] ERROR: Project not found or access denied")
return jsonify({'error': 'Project not found'}), 404
# Parse request data
data = request.get_json()
if data is None:
logger.error(f"[CONVERSATION] ERROR: No JSON data received")
return jsonify({'error': 'No JSON data provided'}), 400
initial_message = data.get('message', 'Hello, I need help with my DIY project.')
logger.info(f"[CONVERSATION] Initial message: {initial_message[:100]}{'...' if len(initial_message) > 100 else ''}")
# Initialize AI Handler
ai_handler = ConversationalAIHandler()
# Create or get agent
agent_id = ai_handler.create_or_get_agent(project_id, project)
logger.info(f"[CONVERSATION] Agent ready: {agent_id}")
# Start conversation
conversation_id = ai_handler.start_conversation(agent_id)
logger.info(f"[CONVERSATION] Conversation started: {conversation_id}")
# Send initial message
response_data = ai_handler.send_message(agent_id, conversation_id, initial_message)
logger.info(f"[CONVERSATION] Message sent successfully")
# Calculate cost and duration
duration = time.time() - start_time
cost = calculate_cost(duration)
# Check and deduct credits
user_credits = user.get('credits', 0)
if user_credits < cost:
logger.error(f"[CONVERSATION] ERROR: Insufficient credits")
return jsonify({'error': 'Insufficient credits', 'needed': cost}), 402
new_credits = user_credits - cost
user_ref.update({'credits': new_credits})
# Log conversation
conversation_log_id = f"{project_id}_{int(start_time)}"
conversation_data = {
'project_id': project_id,
'uid': uid,
'agent_id': agent_id,
'conversation_id': conversation_id,
'initial_message': initial_message,
'response': response_data,
'duration_seconds': duration,
'credits_used': cost,
'created_at': int(start_time)
}
db_ref.child(f'conversations/{conversation_log_id}').set(conversation_data)
# Return success response
return jsonify({
'conversation_log_id': conversation_log_id,
'agent_id': agent_id,
'conversation_id': conversation_id,
'response': response_data,
'durationSeconds': round(duration, 1),
'creditsDeducted': cost,
'remainingCredits': new_credits
}), 200
except Exception as e:
total_duration = time.time() - start_time
logger.error(f"[CONVERSATION] CRITICAL ERROR: {str(e)}")
logger.error(f"[CONVERSATION] Exception type: {type(e).__name__}")
return jsonify({'error': 'Failed to start conversation'}), 500
@app.route('/api/webhook/agent/<project_id>', methods=['POST'])
def agent_webhook(project_id):
"""Webhook to handle agent events"""
try:
data = request.get_json()
event_type = data.get('event_type')
# Log the webhook event
logger.info(f"Agent webhook for project {project_id}: {event_type}")
# Store webhook data for debugging/monitoring
db_ref.child(f'webhooks/{project_id}/{int(time.time())}').set({
'event_type': event_type,
'data': data,
'timestamp': int(time.time())
})
return jsonify({'status': 'received'}), 200
except Exception as e:
logger.error(f"Webhook error: {str(e)}")
return jsonify({'error': 'Webhook processing failed'}), 500
@app.route('/api/projects/<project_id>/continue-conversation', methods=['POST'])
def continue_conversation(project_id):
"""Continue an existing conversation"""
try:
uid = verify_token(request.headers.get('Authorization'))
if not uid:
return jsonify({'error': 'Unauthorized'}), 401
data = request.get_json()
agent_id = data.get('agent_id')
message = data.get('message')
if not agent_id or not message:
return jsonify({'error': 'agent_id and message required'}), 400
ai_handler = ConversationalAIHandler()
result = ai_handler.simulate_conversation(agent_id, message)
return jsonify({
'simulation_result': result,
'timestamp': int(time.time())
}), 200
except Exception as e:
logger.error(f"Error continuing conversation: {str(e)}")
return jsonify({'error': 'Failed to continue conversation'}), 500
# -----------------------------------------------------------------------------
# 7. MAIN EXECUTION
# -----------------------------------------------------------------------------
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860))) |