Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,14 @@ from fastapi.responses import FileResponse
|
|
| 11 |
from pydantic import BaseModel, HttpUrl
|
| 12 |
from process_interview import process_interview
|
| 13 |
|
| 14 |
-
#
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
logger = logging.getLogger("EvalBot-Audio-Processor")
|
| 17 |
|
| 18 |
-
# Initialize
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
-
# Directories
|
| 22 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
TEMP_DIR = os.path.join(BASE_DIR, "temp_files")
|
| 24 |
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
|
@@ -29,15 +29,15 @@ PDF_DIR = os.path.join(OUTPUT_DIR, "pdf")
|
|
| 29 |
for folder in [TEMP_DIR, JSON_DIR, PDF_DIR]:
|
| 30 |
os.makedirs(folder, exist_ok=True)
|
| 31 |
|
| 32 |
-
# Mount static files
|
| 33 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 34 |
|
| 35 |
-
#
|
| 36 |
VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac')
|
| 37 |
MAX_FILE_SIZE_MB = 300
|
| 38 |
-
BASE_URL = os.getenv("BASE_URL", "https://evalbot-audio-evalbot.hf.space")
|
| 39 |
|
| 40 |
-
#
|
| 41 |
class ProcessResponse(BaseModel):
|
| 42 |
summary: str
|
| 43 |
json_url: str
|
|
@@ -47,7 +47,7 @@ class ProcessAudioRequest(BaseModel):
|
|
| 47 |
file_url: HttpUrl
|
| 48 |
user_id: str
|
| 49 |
|
| 50 |
-
#
|
| 51 |
def download_file(file_url: str, dest_path: str):
|
| 52 |
logger.info(f"Downloading file from {file_url}")
|
| 53 |
try:
|
|
@@ -69,6 +69,7 @@ def validate_file_size(file_path: str):
|
|
| 69 |
raise HTTPException(status_code=400, detail=f"File too large: {file_size_mb:.2f} MB")
|
| 70 |
|
| 71 |
def generate_public_url(sub_path: str) -> str:
|
|
|
|
| 72 |
return f"{BASE_URL}/static/{sub_path}"
|
| 73 |
|
| 74 |
# Main endpoint
|
|
@@ -134,7 +135,7 @@ async def process_audio(request: ProcessAudioRequest = Body(...)):
|
|
| 134 |
if os.path.exists(temp_path):
|
| 135 |
os.remove(temp_path)
|
| 136 |
|
| 137 |
-
# Serve output files
|
| 138 |
@app.get("/outputs/json/{filename}")
|
| 139 |
async def get_json_file(filename: str):
|
| 140 |
file_path = os.path.join(JSON_DIR, filename)
|
|
|
|
| 11 |
from pydantic import BaseModel, HttpUrl
|
| 12 |
from process_interview import process_interview
|
| 13 |
|
| 14 |
+
# Logging setup
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
logger = logging.getLogger("EvalBot-Audio-Processor")
|
| 17 |
|
| 18 |
+
# Initialize FastAPI app
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
+
# Directories
|
| 22 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
TEMP_DIR = os.path.join(BASE_DIR, "temp_files")
|
| 24 |
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
|
|
|
| 29 |
for folder in [TEMP_DIR, JSON_DIR, PDF_DIR]:
|
| 30 |
os.makedirs(folder, exist_ok=True)
|
| 31 |
|
| 32 |
+
# Mount static files
|
| 33 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 34 |
|
| 35 |
+
# Config
|
| 36 |
VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac')
|
| 37 |
MAX_FILE_SIZE_MB = 300
|
| 38 |
+
BASE_URL = os.getenv("BASE_URL", "https://evalbot-audio-evalbot.hf.space") # بدون /static في النهاية
|
| 39 |
|
| 40 |
+
# Models
|
| 41 |
class ProcessResponse(BaseModel):
|
| 42 |
summary: str
|
| 43 |
json_url: str
|
|
|
|
| 47 |
file_url: HttpUrl
|
| 48 |
user_id: str
|
| 49 |
|
| 50 |
+
# Helpers
|
| 51 |
def download_file(file_url: str, dest_path: str):
|
| 52 |
logger.info(f"Downloading file from {file_url}")
|
| 53 |
try:
|
|
|
|
| 69 |
raise HTTPException(status_code=400, detail=f"File too large: {file_size_mb:.2f} MB")
|
| 70 |
|
| 71 |
def generate_public_url(sub_path: str) -> str:
|
| 72 |
+
# هنا مهم جداً ما تضيفش /static مرتين
|
| 73 |
return f"{BASE_URL}/static/{sub_path}"
|
| 74 |
|
| 75 |
# Main endpoint
|
|
|
|
| 135 |
if os.path.exists(temp_path):
|
| 136 |
os.remove(temp_path)
|
| 137 |
|
| 138 |
+
# Serve output files (optional if using StaticFiles)
|
| 139 |
@app.get("/outputs/json/{filename}")
|
| 140 |
async def get_json_file(filename: str):
|
| 141 |
file_path = os.path.join(JSON_DIR, filename)
|