Update behavioral and technical id
Browse files
main.py
CHANGED
|
@@ -1,333 +1,267 @@
|
|
| 1 |
-
# =========================================
|
| 2 |
-
# 1. IMPORTS
|
| 3 |
-
# =========================================
|
| 4 |
-
import asyncio
|
| 5 |
-
import os
|
| 6 |
-
import json
|
| 7 |
-
import uuid
|
| 8 |
-
import cloudinary
|
| 9 |
-
import cloudinary.uploader
|
| 10 |
-
import firebase_admin
|
| 11 |
-
from firebase_admin import credentials, firestore
|
| 12 |
-
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 13 |
-
from pydantic import BaseModel
|
| 14 |
-
from gradio_client import Client
|
| 15 |
-
from google.cloud.firestore_v1.base_query import FieldFilter
|
| 16 |
-
import edge_tts
|
| 17 |
-
from
|
| 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 |
-
if os.path.exists(filename): os.remove(filename)
|
| 125 |
-
return
|
| 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 |
-
while client is None: await asyncio.sleep(5)
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
else:
|
| 162 |
-
track_text = TECH_CATEGORIES.get(track_id)
|
| 163 |
-
level_text = DIFFICULTY_MAP.get(difficulty)
|
| 164 |
-
prompt = f"Generate ONE unique {track_text} interview question for {level_text} level. Format: Q: [Question] A: [Answer]"
|
| 165 |
-
|
| 166 |
-
success_count = 0
|
| 167 |
-
while success_count < count:
|
| 168 |
-
try:
|
| 169 |
-
raw_output = await safe_generate(prompt)
|
| 170 |
-
q_text, a_text = parse_question_output(raw_output)
|
| 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 |
-
s_type =
|
| 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 |
-
except Exception: pass
|
| 269 |
-
|
| 270 |
-
background_tasks.add_task(delete_job, request.audioUrls)
|
| 271 |
-
return {"message": "Cleanup started"}
|
| 272 |
-
|
| 273 |
-
# @app.get("/trigger-full-prefill")
|
| 274 |
-
# async def trigger_full_prefill(background_tasks: BackgroundTasks):
|
| 275 |
-
# """Prefills 30 questions for every track and every difficulty level"""
|
| 276 |
-
# async def full_prefill_task():
|
| 277 |
-
# for t_id in TECH_CATEGORIES.keys():
|
| 278 |
-
# for diff in DIFFICULTY_MAP.keys():
|
| 279 |
-
# print(f"Starting full prefill for Track {t_id}, Level {diff}")
|
| 280 |
-
# await refill_specific_pool(t_id, diff, 30)
|
| 281 |
-
|
| 282 |
-
# background_tasks.add_task(full_prefill_task)
|
| 283 |
-
# return {"message": "Full system prefill started in background (30 questions per track/level)"}
|
| 284 |
-
#?##############################################################################
|
| 285 |
-
# @app.get("/trigger-behavioral-prefill")
|
| 286 |
-
# async def trigger_behavioral_prefill(background_tasks: BackgroundTasks):
|
| 287 |
-
# """Prefills 30 Behavioral questions (No track or difficulty needed)"""
|
| 288 |
-
|
| 289 |
-
# async def run_behavioral_task():
|
| 290 |
-
# print("Starting Behavioral questions prefill...")
|
| 291 |
-
# await refill_specific_pool(track_id=0, difficulty=0, count=30, session_type=2)
|
| 292 |
-
# print("Finished prefilling 30 Behavioral questions!")
|
| 293 |
-
|
| 294 |
-
# background_tasks.add_task(run_behavioral_task)
|
| 295 |
-
# return {"message": "Behavioral prefill (30 questions) started in background."}
|
| 296 |
-
|
| 297 |
-
@app.get("/health")
|
| 298 |
-
async def health(): return {"status": "running", "hf_connected": client is not None}
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
#?##########################################################################
|
| 302 |
-
|
| 303 |
-
# @app.get("/final-migration-fix")
|
| 304 |
-
# async def final_migration_fix(background_tasks: BackgroundTasks):
|
| 305 |
-
# def run_fix():
|
| 306 |
-
# print("🔄 Starting Final Data Fix...")
|
| 307 |
-
# docs = db.collection("questions_pool").get()
|
| 308 |
-
# updated_count = 0
|
| 309 |
-
|
| 310 |
-
# for doc in docs:
|
| 311 |
-
# data = doc.to_dict()
|
| 312 |
-
# updates = {}
|
| 313 |
-
|
| 314 |
-
# # 1. تصحيح الـ session_type (Technical: 0, Behavioral: 1)
|
| 315 |
-
# # لو كان 1 (قديم) خليه 0، ولو كان 2 (قديم) خليه 1
|
| 316 |
-
# curr_type = data.get("session_type")
|
| 317 |
-
# if curr_type == 1: updates["session_type"] = 0
|
| 318 |
-
# elif curr_type == 2: updates["session_type"] = 1
|
| 319 |
-
|
| 320 |
-
# # 2. تصحيح الـ difficulty (Easy: 0, Intermediate: 1, Hard: 2)
|
| 321 |
-
# # الأسئلة القديمة كانت 1 و 2 و 3، هننقص منها 1
|
| 322 |
-
# curr_diff = data.get("difficulty")
|
| 323 |
-
# if curr_diff in [1, 2, 3]:
|
| 324 |
-
# updates["difficulty"] = curr_diff - 1
|
| 325 |
-
|
| 326 |
-
# if updates:
|
| 327 |
-
# db.collection("questions_pool").document(doc.id).update(updates)
|
| 328 |
-
# updated_count += 1
|
| 329 |
-
|
| 330 |
-
# print(f"✅ Final Fix Done! Updated {updated_count} questions.")
|
| 331 |
-
|
| 332 |
-
# background_tasks.add_task(run_fix)
|
| 333 |
-
# return {"message": "Final migration started. Your pool will be ready in a minute!"}
|
|
|
|
| 1 |
+
# =========================================
|
| 2 |
+
# 1. IMPORTS
|
| 3 |
+
# =========================================
|
| 4 |
+
import asyncio
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import uuid
|
| 8 |
+
import cloudinary
|
| 9 |
+
import cloudinary.uploader
|
| 10 |
+
import firebase_admin
|
| 11 |
+
from firebase_admin import credentials, firestore
|
| 12 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 13 |
+
from pydantic import BaseModel
|
| 14 |
+
from gradio_client import Client
|
| 15 |
+
from google.cloud.firestore_v1.base_query import FieldFilter
|
| 16 |
+
import edge_tts
|
| 17 |
+
from typing import Optional
|
| 18 |
+
from dotenv import load_dotenv
|
| 19 |
+
from contextlib import asynccontextmanager
|
| 20 |
+
|
| 21 |
+
# =========================================
|
| 22 |
+
# 2. INITIALIZATIONS & CONFIG
|
| 23 |
+
# =========================================
|
| 24 |
+
load_dotenv()
|
| 25 |
+
|
| 26 |
+
if not firebase_admin._apps:
|
| 27 |
+
fb_json = os.getenv("FIREBASE_JSON")
|
| 28 |
+
if fb_json:
|
| 29 |
+
cred_dict = json.loads(fb_json)
|
| 30 |
+
cred = credentials.Certificate(cred_dict)
|
| 31 |
+
else:
|
| 32 |
+
cred = credentials.Certificate("serviceAccountKey.json")
|
| 33 |
+
firebase_admin.initialize_app(cred)
|
| 34 |
+
|
| 35 |
+
db = firestore.client()
|
| 36 |
+
|
| 37 |
+
cloudinary.config(
|
| 38 |
+
cloud_name=os.getenv("CLOUD_NAME"),
|
| 39 |
+
api_key=os.getenv("API_KEY"),
|
| 40 |
+
api_secret=os.getenv("API_SECRET"),
|
| 41 |
+
secure=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
HF_SPACE = "Fayza38/Question_and_answer_model"
|
| 45 |
+
client = None
|
| 46 |
+
|
| 47 |
+
# =========================================
|
| 48 |
+
# 3. MODELS & CONSTANTS
|
| 49 |
+
# =========================================
|
| 50 |
+
TECH_CATEGORIES = {
|
| 51 |
+
0: "Security", 1: "BackEnd", 2: "Networking", 3: "FrontEnd",
|
| 52 |
+
4: "DataEngineering", 5: "WebDevelopment", 6: "FullStack",
|
| 53 |
+
7: "VersionControl", 8: "SystemDesign", 9: "MachineLearning",
|
| 54 |
+
10: "LanguagesAndFrameworks", 11: "DatabaseSystems",
|
| 55 |
+
12: "ArtificialIntelligence", 13: "SoftwareTesting",
|
| 56 |
+
14: "DistributedSystems", 15: "DevOps", 16: "LowLevelSystems",
|
| 57 |
+
17: "DatabaseAndSql", 18: "GeneralProgramming",
|
| 58 |
+
19: "DataStructures", 20: "Algorithms"
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
DIFFICULTY_MAP = {0: "Easy", 1: "Intermediate", 2: "Hard"}
|
| 62 |
+
|
| 63 |
+
class GenerateSessionRequest(BaseModel):
|
| 64 |
+
sessionId: str
|
| 65 |
+
sessionType: int # 0: Behavioral, 1: Technical
|
| 66 |
+
difficultyLevel: int = 0
|
| 67 |
+
trackName: Optional[int] = None
|
| 68 |
+
|
| 69 |
+
class CleanupRequest(BaseModel):
|
| 70 |
+
audioUrls: list[str]
|
| 71 |
+
|
| 72 |
+
# =========================================
|
| 73 |
+
# 4. BACKGROUND TASKS (Auto-Cleaner)
|
| 74 |
+
# =========================================
|
| 75 |
+
async def auto_clean_invalid_questions():
|
| 76 |
+
"""Background loop to remove questions with missing or broken audio."""
|
| 77 |
+
while True:
|
| 78 |
+
try:
|
| 79 |
+
print("[Auto-Cleaner] Scanning for broken questions...")
|
| 80 |
+
docs = db.collection("questions_pool").get()
|
| 81 |
+
deleted_count = 0
|
| 82 |
+
for doc in docs:
|
| 83 |
+
data = doc.to_dict()
|
| 84 |
+
if not data.get("audio_url"):
|
| 85 |
+
db.collection("questions_pool").document(doc.id).delete()
|
| 86 |
+
deleted_count += 1
|
| 87 |
+
if deleted_count > 0:
|
| 88 |
+
print(f"[Auto-Cleaner] Removed {deleted_count} broken questions.")
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"[Auto-Cleaner] Error: {e}")
|
| 91 |
+
await asyncio.sleep(600) # Scan every 10 minutes
|
| 92 |
+
|
| 93 |
+
# =========================================
|
| 94 |
+
# 5. LIFESPAN MANAGEMENT
|
| 95 |
+
# =========================================
|
| 96 |
+
@asynccontextmanager
|
| 97 |
+
async def lifespan(app: FastAPI):
|
| 98 |
+
global client
|
| 99 |
+
print("Connecting to Hugging Face Space...")
|
| 100 |
+
try:
|
| 101 |
+
loop = asyncio.get_event_loop()
|
| 102 |
+
client = await loop.run_in_executor(None, lambda: Client(HF_SPACE))
|
| 103 |
+
print("Connected Successfully!")
|
| 104 |
+
# Start the background cleaner
|
| 105 |
+
asyncio.create_task(auto_clean_invalid_questions())
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"Startup Connection failed: {e}")
|
| 108 |
+
|
| 109 |
+
yield
|
| 110 |
+
print("Shutting down Intervision Service...")
|
| 111 |
+
|
| 112 |
+
app = FastAPI(title="Intervision AI Question Service", lifespan=lifespan)
|
| 113 |
+
|
| 114 |
+
# =========================================
|
| 115 |
+
# 6. HELPERS
|
| 116 |
+
# =========================================
|
| 117 |
+
async def generate_audio(text, filename):
|
| 118 |
+
try:
|
| 119 |
+
communicate = edge_tts.Communicate(text, "en-US-GuyNeural", rate="-15%")
|
| 120 |
+
await communicate.save(filename)
|
| 121 |
+
upload_result = cloudinary.uploader.upload(
|
| 122 |
+
filename, resource_type="video", folder="interview_audio"
|
| 123 |
+
)
|
| 124 |
+
if os.path.exists(filename): os.remove(filename)
|
| 125 |
+
return upload_result["secure_url"]
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f"Audio Error: {e}")
|
| 128 |
+
if os.path.exists(filename): os.remove(filename)
|
| 129 |
+
return None
|
| 130 |
+
|
| 131 |
+
async def safe_generate(prompt, retries=3):
|
| 132 |
+
if client is None: raise Exception("Gradio Client not initialized")
|
| 133 |
+
for attempt in range(retries):
|
| 134 |
+
try:
|
| 135 |
+
loop = asyncio.get_running_loop()
|
| 136 |
+
return await loop.run_in_executor(None, lambda: client.predict(prompt=prompt, api_name="/generate_questions"))
|
| 137 |
+
except Exception as e:
|
| 138 |
+
if attempt == retries - 1: raise e
|
| 139 |
+
await asyncio.sleep(2)
|
| 140 |
+
|
| 141 |
+
def parse_question_output(raw_output: str):
|
| 142 |
+
if not raw_output: return None, None
|
| 143 |
+
text = raw_output.split("assistant")[-1].strip() if "assistant" in raw_output else raw_output
|
| 144 |
+
if "Q:" in text and "A:" in text:
|
| 145 |
+
try:
|
| 146 |
+
parts = text.split("A:")
|
| 147 |
+
q = parts[0].replace("Q:", "").strip()
|
| 148 |
+
a = parts[1].split("<|im_end|>")[0].strip()
|
| 149 |
+
return q, a
|
| 150 |
+
except: return None, None
|
| 151 |
+
return None, None
|
| 152 |
+
|
| 153 |
+
async def refill_specific_pool(track_id: int, difficulty: int, count: int, session_type: int = 1):
|
| 154 |
+
while client is None: await asyncio.sleep(5)
|
| 155 |
+
|
| 156 |
+
if session_type == 0:
|
| 157 |
+
prompt = ("Generate ONE simple Behavioral interview question for a fresh graduate. "
|
| 158 |
+
"Focus on soft skills like teamwork or leadership. Strictly NO technical questions. "
|
| 159 |
+
"Format: Q: [Question] A: [Answer]")
|
| 160 |
+
track_text = "Behavioral"
|
| 161 |
+
else:
|
| 162 |
+
track_text = TECH_CATEGORIES.get(track_id)
|
| 163 |
+
level_text = DIFFICULTY_MAP.get(difficulty)
|
| 164 |
+
prompt = f"Generate ONE unique {track_text} interview question for {level_text} level. Format: Q: [Question] A: [Answer]"
|
| 165 |
+
|
| 166 |
+
success_count = 0
|
| 167 |
+
while success_count < count:
|
| 168 |
+
try:
|
| 169 |
+
raw_output = await safe_generate(prompt)
|
| 170 |
+
q_text, a_text = parse_question_output(raw_output)
|
| 171 |
+
if q_text and a_text:
|
| 172 |
+
filename = f"{uuid.uuid4()}.mp3"
|
| 173 |
+
audio_url = await generate_audio(q_text, filename)
|
| 174 |
+
if audio_url:
|
| 175 |
+
db.collection("questions_pool").add({
|
| 176 |
+
"session_type": session_type,
|
| 177 |
+
"track_id": track_id if session_type == 1 else -1,
|
| 178 |
+
"difficulty": difficulty if session_type == 1 else 0,
|
| 179 |
+
"questionText": q_text,
|
| 180 |
+
"questionIdealAnswer": a_text,
|
| 181 |
+
"audio_url": audio_url,
|
| 182 |
+
"created_at": firestore.SERVER_TIMESTAMP
|
| 183 |
+
})
|
| 184 |
+
success_count += 1
|
| 185 |
+
print(f"Refilled {success_count}/{count} for {track_text}")
|
| 186 |
+
await asyncio.sleep(3)
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print(f"Refill error: {e}")
|
| 189 |
+
await asyncio.sleep(5)
|
| 190 |
+
|
| 191 |
+
# =========================================
|
| 192 |
+
# 7. MAIN ENDPOINTS
|
| 193 |
+
# =========================================
|
| 194 |
+
@app.post("/generate-session")
|
| 195 |
+
async def generate_session(request: GenerateSessionRequest, background_tasks: BackgroundTasks):
|
| 196 |
+
s_type, diff, t_id = request.sessionType, request.difficultyLevel, request.trackName
|
| 197 |
+
query = db.collection("questions_pool").where(filter=FieldFilter("session_type", "==", s_type))
|
| 198 |
+
|
| 199 |
+
if s_type == 1: # Technical
|
| 200 |
+
if t_id is None: raise HTTPException(status_code=400, detail="trackName required for technical.")
|
| 201 |
+
query = query.where(filter=FieldFilter("track_id", "==", t_id)).where(filter=FieldFilter("difficulty", "==", diff))
|
| 202 |
+
|
| 203 |
+
docs_query = query.limit(10).get()
|
| 204 |
+
final_questions = []
|
| 205 |
+
for index, doc in enumerate(docs_query, start=1):
|
| 206 |
+
data = doc.to_dict()
|
| 207 |
+
final_questions.append({
|
| 208 |
+
"question_id": index, "text": data["questionText"],
|
| 209 |
+
"expected_answer": data["questionIdealAnswer"], "audio_url": data.get("audio_url", "")
|
| 210 |
+
})
|
| 211 |
+
db.collection("questions_pool").document(doc.id).delete()
|
| 212 |
+
|
| 213 |
+
async def maintain_stock():
|
| 214 |
+
snap = query.count().get()
|
| 215 |
+
current = snap[0][0].value
|
| 216 |
+
if current < 50:
|
| 217 |
+
await refill_specific_pool(t_id if s_type == 1 else -1, diff, 50 - current, session_type=s_type)
|
| 218 |
+
|
| 219 |
+
background_tasks.add_task(maintain_stock)
|
| 220 |
+
if not final_questions:
|
| 221 |
+
raise HTTPException(status_code=503, detail="Question pool is currently empty.")
|
| 222 |
+
return {"session_id": request.sessionId, "questions": final_questions}
|
| 223 |
+
|
| 224 |
+
@app.post("/cleanup-audio")
|
| 225 |
+
async def cleanup_audio(request: CleanupRequest, background_tasks: BackgroundTasks):
|
| 226 |
+
def delete_job(urls):
|
| 227 |
+
for url in urls:
|
| 228 |
+
try:
|
| 229 |
+
public_id = "interview_audio/" + url.split('/')[-1].split('.')[0]
|
| 230 |
+
cloudinary.uploader.destroy(public_id, resource_type="video")
|
| 231 |
+
except: pass
|
| 232 |
+
background_tasks.add_task(delete_job, request.audioUrls)
|
| 233 |
+
return {"message": "Cleanup started"}
|
| 234 |
+
|
| 235 |
+
@app.get("/system-cleanup")
|
| 236 |
+
async def system_cleanup(background_tasks: BackgroundTasks):
|
| 237 |
+
"""Scan and delete all questions with missing or invalid audio URLs"""
|
| 238 |
+
def run_cleanup():
|
| 239 |
+
print("Starting System Cleanup...")
|
| 240 |
+
# Get all documents in the pool
|
| 241 |
+
docs = db.collection("questions_pool").get()
|
| 242 |
+
deleted_count = 0
|
| 243 |
+
|
| 244 |
+
for doc in docs:
|
| 245 |
+
data = doc.to_dict()
|
| 246 |
+
# Check if audio_url is missing, None, or empty string
|
| 247 |
+
if not data.get("audio_url") or data.get("audio_url") == "":
|
| 248 |
+
db.collection("questions_pool").document(doc.id).delete()
|
| 249 |
+
deleted_count += 1
|
| 250 |
+
|
| 251 |
+
print(f"Cleanup finished! Deleted {deleted_count} broken questions.")
|
| 252 |
+
|
| 253 |
+
background_tasks.add_task(run_cleanup)
|
| 254 |
+
return {"message": "Cleanup started in background. Check your console/logs."}
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
@app.get("/health")
|
| 258 |
+
async def health():
|
| 259 |
+
return {"status": "active", "hf_connected": client is not None}
|
| 260 |
+
|
| 261 |
+
@app.get("/")
|
| 262 |
+
async def root():
|
| 263 |
+
return {"app": "Intervision AI Service","Status": "Running.."}
|
| 264 |
+
|
| 265 |
+
if __name__ == "__main__":
|
| 266 |
+
import uvicorn
|
| 267 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|