Spaces:
Running
Running
fix
Browse files
.env
CHANGED
|
@@ -15,4 +15,9 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 19 |
+
AWS_ACCESS_KEY_ID=AKIA3PWGNRHL7RTV3XRJ
|
| 20 |
+
AWS_SECRET_ACCESS_KEY=SZBvxZHPw8OVkrFd7nMXe+Nt/3ulrpynXVrGBiKm
|
| 21 |
+
AWS_REGION=ap-south-1
|
| 22 |
+
S3_BUCKET=pykara-tts-audio
|
| 23 |
+
S3_PREFIX=audio/
|
media/audio/explain_0f06635a143f44bd9e705a53485476d7.wav
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:189a4ca997451cafe72c7652a89ccd0f43fc107064a075624af5499b10f219fe
|
| 3 |
-
size 1092844
|
|
|
|
|
|
|
|
|
|
|
|
media/audio/explain_1bf217407423408f8800bcb2a3db2d3b.wav
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:c0f7690ce19971fba91c94c34076908f61d335d88ddbcf456d57a98c53fd155b
|
| 3 |
-
size 1305324
|
|
|
|
|
|
|
|
|
|
|
|
ragg/app.py
CHANGED
|
@@ -12,6 +12,13 @@ from flask_cors import CORS
|
|
| 12 |
import requests
|
| 13 |
from TTS.api import TTS
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# RAG imports
|
| 17 |
try:
|
|
@@ -103,6 +110,49 @@ try:
|
|
| 103 |
except Exception:
|
| 104 |
pass
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# Media and voice references
|
| 107 |
|
| 108 |
# MEDIA_ROOT = Path(os.getenv("MEDIA_ROOT", "./media"))
|
|
@@ -320,14 +370,18 @@ def rag_explain_grammar():
|
|
| 320 |
reference_files=None,
|
| 321 |
language=data.get("language", "en"),
|
| 322 |
)
|
| 323 |
-
#
|
| 324 |
if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
|
| 325 |
base = request.host_url.rstrip("/")
|
|
|
|
| 326 |
else:
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
| 331 |
except FileNotFoundError as e:
|
| 332 |
current_app.logger.error("XTTS reference audio missing: %s", e)
|
| 333 |
except Exception as e:
|
|
@@ -582,15 +636,19 @@ def rag_synthesize_audio():
|
|
| 582 |
reference_files=reference_files,
|
| 583 |
language=language,
|
| 584 |
)
|
| 585 |
-
#
|
| 586 |
if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
|
| 587 |
base = request.host_url.rstrip("/")
|
|
|
|
| 588 |
else:
|
| 589 |
-
|
| 590 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
-
# base = request.host_url.rstrip("/") # e.g., http://localhost:7000
|
| 593 |
-
# audio_url = f"{base}/rag/audio/{wav_path.name}"
|
| 594 |
return jsonify({"audio_url": audio_url, "file": wav_path.name}), 200
|
| 595 |
except FileNotFoundError as e:
|
| 596 |
current_app.logger.error("XTTS references missing: %s", e)
|
|
|
|
| 12 |
import requests
|
| 13 |
from TTS.api import TTS
|
| 14 |
|
| 15 |
+
# --- S3 (added) ---
|
| 16 |
+
try:
|
| 17 |
+
import boto3
|
| 18 |
+
from botocore.exceptions import NoCredentialsError, ClientError
|
| 19 |
+
except Exception:
|
| 20 |
+
boto3 = None
|
| 21 |
+
NoCredentialsError = ClientError = Exception # fallbacks so type names exist
|
| 22 |
|
| 23 |
# RAG imports
|
| 24 |
try:
|
|
|
|
| 110 |
except Exception:
|
| 111 |
pass
|
| 112 |
|
| 113 |
+
# --- S3 config (added) ---
|
| 114 |
+
S3_BUCKET = os.getenv("S3_BUCKET", "").strip()
|
| 115 |
+
AWS_REGION = os.getenv("AWS_REGION", "ap-south-1").strip()
|
| 116 |
+
S3_PREFIX = os.getenv("S3_PREFIX", "audio/").strip()
|
| 117 |
+
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "").strip()
|
| 118 |
+
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "").strip()
|
| 119 |
+
|
| 120 |
+
_s3_client = None
|
| 121 |
+
if boto3 and S3_BUCKET and AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
|
| 122 |
+
try:
|
| 123 |
+
_s3_client = boto3.client(
|
| 124 |
+
"s3",
|
| 125 |
+
region_name=AWS_REGION,
|
| 126 |
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
| 127 |
+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
|
| 128 |
+
)
|
| 129 |
+
except Exception as _e:
|
| 130 |
+
_s3_client = None
|
| 131 |
+
|
| 132 |
+
def _upload_to_s3(file_path: Union[str, Path]) -> Optional[str]:
|
| 133 |
+
"""
|
| 134 |
+
Upload the file to S3 and return a presigned URL (24h).
|
| 135 |
+
If S3 is not configured, returns None (caller will fallback).
|
| 136 |
+
"""
|
| 137 |
+
if not _s3_client or not S3_BUCKET:
|
| 138 |
+
return None
|
| 139 |
+
try:
|
| 140 |
+
file_path = str(file_path)
|
| 141 |
+
key = f"{S3_PREFIX}{Path(file_path).name}"
|
| 142 |
+
_s3_client.upload_file(file_path, S3_BUCKET, key)
|
| 143 |
+
url = _s3_client.generate_presigned_url(
|
| 144 |
+
"get_object",
|
| 145 |
+
Params={"Bucket": S3_BUCKET, "Key": key},
|
| 146 |
+
ExpiresIn=24 * 3600,
|
| 147 |
+
)
|
| 148 |
+
return url
|
| 149 |
+
except (NoCredentialsError, ClientError) as e:
|
| 150 |
+
try:
|
| 151 |
+
current_app.logger.error(f"S3 upload failed: {e}")
|
| 152 |
+
except Exception:
|
| 153 |
+
print(f"S3 upload failed: {e}")
|
| 154 |
+
return None
|
| 155 |
+
|
| 156 |
# Media and voice references
|
| 157 |
|
| 158 |
# MEDIA_ROOT = Path(os.getenv("MEDIA_ROOT", "./media"))
|
|
|
|
| 370 |
reference_files=None,
|
| 371 |
language=data.get("language", "en"),
|
| 372 |
)
|
| 373 |
+
# Local: serve from /rag/audio/*
|
| 374 |
if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
|
| 375 |
base = request.host_url.rstrip("/")
|
| 376 |
+
result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
|
| 377 |
else:
|
| 378 |
+
# Deployed: try S3 first; fallback to public SPACE_URL if set
|
| 379 |
+
s3_url = _upload_to_s3(str(wav_path))
|
| 380 |
+
if s3_url:
|
| 381 |
+
result_dict["audio_url"] = s3_url
|
| 382 |
+
else:
|
| 383 |
+
base = os.getenv("SPACE_URL", "https://pykara-py-learn-backend.hf.space")
|
| 384 |
+
result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
|
| 385 |
except FileNotFoundError as e:
|
| 386 |
current_app.logger.error("XTTS reference audio missing: %s", e)
|
| 387 |
except Exception as e:
|
|
|
|
| 636 |
reference_files=reference_files,
|
| 637 |
language=language,
|
| 638 |
)
|
| 639 |
+
# Local: serve static file
|
| 640 |
if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
|
| 641 |
base = request.host_url.rstrip("/")
|
| 642 |
+
audio_url = f"{base}/rag/audio/{wav_path.name}"
|
| 643 |
else:
|
| 644 |
+
# Deployed: try S3 first; fallback to SPACE_URL
|
| 645 |
+
s3_url = _upload_to_s3(str(wav_path))
|
| 646 |
+
if s3_url:
|
| 647 |
+
audio_url = s3_url
|
| 648 |
+
else:
|
| 649 |
+
base = os.getenv("SPACE_URL", "https://pykara-py-learn-backend.hf.space")
|
| 650 |
+
audio_url = f"{base}/rag/audio/{wav_path.name}"
|
| 651 |
|
|
|
|
|
|
|
| 652 |
return jsonify({"audio_url": audio_url, "file": wav_path.name}), 200
|
| 653 |
except FileNotFoundError as e:
|
| 654 |
current_app.logger.error("XTTS references missing: %s", e)
|