pykara commited on
Commit
7383c72
·
1 Parent(s): 581b677
Files changed (2) hide show
  1. .env +2 -2
  2. ragg/app.py +111 -12
.env CHANGED
@@ -11,8 +11,8 @@ DID_API_KEY=cmFqYWxhc2htaS5uQHB5a2FyYS5uZXQ:J2uPGx3uD4L7UKgHEiMJI
11
  DID_SOURCE_IMAGE_URL=https://i.ibb.co/Tpq77ZJ/teacher.png
12
  DID_VOICE_ID=en-US-JennyNeural
13
  TESSERACT_CMD=C:\Program Files\Tesseract-OCR\tesseract.exe
14
- CHROMA_DIR=C:/Users/DELL/Desktop/Deploymnet/29 oct/py-learn-backend/ragg/chroma
15
- CHROMA_ROOT=C:/Users/DELL/Desktop/Deploymnet/29 oct/py-learn-backend/ragg/chroma
16
  EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
17
  ALLOWED_ORIGINS=http://localhost:4200,http://127.0.0.1:4200
18
  RAG_INGEST_URL=http://localhost:5000/rag/ingest
 
11
  DID_SOURCE_IMAGE_URL=https://i.ibb.co/Tpq77ZJ/teacher.png
12
  DID_VOICE_ID=en-US-JennyNeural
13
  TESSERACT_CMD=C:\Program Files\Tesseract-OCR\tesseract.exe
14
+ CHROMA_DIR=C:/Users/DELL/Desktop/Deploymnet/29 oct/mj-learn-backend/ragg/chroma
15
+ CHROMA_ROOT=C:/Users/DELL/Desktop/Deploymnet/29 oct/mj-learn-backend/ragg/chroma
16
  EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
17
  ALLOWED_ORIGINS=http://localhost:4200,http://127.0.0.1:4200
18
  RAG_INGEST_URL=http://localhost:5000/rag/ingest
ragg/app.py CHANGED
@@ -332,6 +332,115 @@ def rag_generate_questions():
332
 
333
 
334
  # @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
336
  def rag_explain_grammar():
337
  if request.method == "OPTIONS":
@@ -391,18 +500,8 @@ def rag_explain_grammar():
391
  reference_files=None,
392
  language=data.get("language", "en"),
393
  )
394
- # Local: serve from /rag/audio/*
395
- if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
396
- base = request.host_url.rstrip("/")
397
- result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
398
- else:
399
- # Deployed: try S3 first; fallback to public SPACE_URL if set
400
- s3_url = _upload_to_s3(str(wav_path))
401
- if s3_url:
402
- result_dict["audio_url"] = s3_url
403
- else:
404
- base = os.getenv("SPACE_URL", "https://pykara-py-learn-backend.hf.space")
405
- result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
406
  except FileNotFoundError as e:
407
  current_app.logger.error("XTTS reference audio missing: %s", e)
408
  except Exception as e:
 
332
 
333
 
334
  # @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
335
+ # @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
336
+ # def rag_explain_grammar():
337
+ # if request.method == "OPTIONS":
338
+ # return ("", 204)
339
+
340
+ # data = request.get_json(force=True) or {}
341
+
342
+ # # --- Extract username and db_level ---
343
+ # username = extract_username_from_request(request)
344
+ # db_level = user_to_db_level(username)
345
+
346
+ # # --- MAIN BODY (your preferred structure) ---
347
+ # body = ExplainBody(
348
+ # question=(data.get("question") or "").strip(),
349
+ # model=data.get("model", "gpt-4o-mini"),
350
+ # db_level=db_level,
351
+ # source_ids=data.get("source_ids") or []
352
+ # )
353
+
354
+ # # --- 1) Run LLM / RAG explanation ---
355
+ # result_raw = llm_explain(body)
356
+
357
+ # # --- 2) Normalize + extract answer safely ---
358
+ # result_dict = None
359
+ # answer_text = ""
360
+ # try:
361
+ # if isinstance(result_raw, dict):
362
+ # result_dict = dict(result_raw)
363
+ # elif hasattr(result_raw, "model_dump"):
364
+ # result_dict = result_raw.model_dump()
365
+ # elif hasattr(result_raw, "dict"):
366
+ # result_dict = result_raw.dict()
367
+ # elif isinstance(result_raw, str):
368
+ # result_dict = {"answer": result_raw}
369
+ # else:
370
+ # result_dict = {"answer": str(result_raw)}
371
+
372
+ # answer_text = (
373
+ # result_dict.get("answer")
374
+ # or result_dict.get("response")
375
+ # or result_dict.get("text")
376
+ # or ""
377
+ # ).strip()
378
+ # except Exception as e:
379
+ # current_app.logger.exception("Failed to normalize llm_explain result: %s", e)
380
+ # return jsonify({"error": "Internal error normalizing LLM response"}), 500
381
+
382
+ # # --- 3) Optional: synthesize TTS audio ---
383
+ # try:
384
+ # if data.get("synthesize_audio"):
385
+ # try:
386
+ # out_name = f"explain_{uuid.uuid4().hex}.wav"
387
+ # wav_path = xtts_speak_to_file(
388
+ # text=answer_text or result_dict.get("answer", ""),
389
+ # out_file=AUDIO_DIR / out_name,
390
+ # reference_dir=XTTS_REF_DIR,
391
+ # reference_files=None,
392
+ # language=data.get("language", "en"),
393
+ # )
394
+ # # Local: serve from /rag/audio/*
395
+ # if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
396
+ # base = request.host_url.rstrip("/")
397
+ # result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
398
+ # else:
399
+ # # Deployed: try S3 first; fallback to public SPACE_URL if set
400
+ # s3_url = _upload_to_s3(str(wav_path))
401
+ # if s3_url:
402
+ # result_dict["audio_url"] = s3_url
403
+ # else:
404
+ # base = os.getenv("SPACE_URL", "https://pykara-py-learn-backend.hf.space")
405
+ # result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
406
+ # except FileNotFoundError as e:
407
+ # current_app.logger.error("XTTS reference audio missing: %s", e)
408
+ # except Exception as e:
409
+ # current_app.logger.exception("XTTS synthesis during explain-grammar failed: %s", e)
410
+ # except Exception:
411
+ # current_app.logger.exception("Unexpected error while attempting inline synthesis")
412
+
413
+ # # --- 4) Optional: synthesize video (D-ID) ---
414
+ # try:
415
+ # if data.get("synthesize_video"):
416
+ # if not DID_API_KEY or not DID_SOURCE_IMAGE_URL:
417
+ # current_app.logger.error("D-ID not configured for inline explain-grammar video synthesis")
418
+ # else:
419
+ # try:
420
+ # talk_id, err = _did_create_talk(answer_text or result_dict.get("answer", ""))
421
+ # if err:
422
+ # current_app.logger.error(
423
+ # "D-ID create error during explain-grammar: %s",
424
+ # err[0] if isinstance(err, tuple) else err,
425
+ # )
426
+ # else:
427
+ # video_url, err = _did_poll_talk(talk_id, timeout_sec=120, interval_sec=2.0)
428
+ # if err:
429
+ # current_app.logger.error(
430
+ # "D-ID poll error during explain-grammar: %s",
431
+ # err[0] if isinstance(err, tuple) else err,
432
+ # )
433
+ # else:
434
+ # if video_url:
435
+ # result_dict["video_url"] = video_url
436
+ # except Exception as e:
437
+ # current_app.logger.exception("D-ID inline synthesis failed during explain-grammar: %s", e)
438
+ # except Exception:
439
+ # current_app.logger.exception("Unexpected error while attempting inline video synthesis")
440
+
441
+ # # --- Final response ---
442
+ # return jsonify(result_dict), 200
443
+
444
  @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
445
  def rag_explain_grammar():
446
  if request.method == "OPTIONS":
 
500
  reference_files=None,
501
  language=data.get("language", "en"),
502
  )
503
+ base = request.host_url.rstrip("/")
504
+ result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
 
 
 
 
 
 
 
 
 
 
505
  except FileNotFoundError as e:
506
  current_app.logger.error("XTTS reference audio missing: %s", e)
507
  except Exception as e: