Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -5,9 +5,13 @@ import numpy as np
|
|
5 |
import tempfile
|
6 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
7 |
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
|
8 |
-
from librosa.sequence import dtw
|
9 |
|
10 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# --- Core Class Definition ---
|
13 |
class QuranRecitationComparer:
|
@@ -118,13 +122,11 @@ class QuranRecitationComparer:
|
|
118 |
"""Clear the embedding cache to free memory."""
|
119 |
self.embedding_cache = {}
|
120 |
|
121 |
-
|
122 |
# --- FastAPI Startup Event ---
|
123 |
-
# In production, consider loading sensitive tokens from environment variables or configuration files.
|
124 |
@app.on_event("startup")
|
125 |
def startup_event():
|
126 |
global comparer
|
127 |
-
#
|
128 |
auth_token = os.environ.get("HF_TOKEN")
|
129 |
comparer = QuranRecitationComparer(
|
130 |
model_name="jonatasgrosman/wav2vec2-large-xlsr-53-arabic",
|
@@ -132,13 +134,11 @@ def startup_event():
|
|
132 |
)
|
133 |
print("Model initialized and ready for predictions!")
|
134 |
|
135 |
-
|
136 |
# --- API Endpoints ---
|
137 |
@app.get("/", summary="Health Check")
|
138 |
async def root():
|
139 |
return {"message": "Quran Recitation Comparer API is up and running."}
|
140 |
|
141 |
-
|
142 |
@app.post("/predict", summary="Compare Two Audio Files", response_model=dict)
|
143 |
async def predict(file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
144 |
"""
|
@@ -177,7 +177,6 @@ async def predict(file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
|
177 |
if tmp2_path and os.path.exists(tmp2_path):
|
178 |
os.remove(tmp2_path)
|
179 |
|
180 |
-
|
181 |
@app.post("/clear_cache", summary="Clear Embedding Cache", response_model=dict)
|
182 |
async def clear_cache():
|
183 |
"""
|
|
|
5 |
import tempfile
|
6 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
7 |
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
|
8 |
+
from librosa.sequence import dtw # Ensure librosa==0.9.2 is installed
|
9 |
|
10 |
+
app = FastAPI(
|
11 |
+
title="Quran Recitation Comparer API",
|
12 |
+
description="Compares two Quran recitations using a deep wav2vec2 model.",
|
13 |
+
version="1.0"
|
14 |
+
)
|
15 |
|
16 |
# --- Core Class Definition ---
|
17 |
class QuranRecitationComparer:
|
|
|
122 |
"""Clear the embedding cache to free memory."""
|
123 |
self.embedding_cache = {}
|
124 |
|
|
|
125 |
# --- FastAPI Startup Event ---
|
|
|
126 |
@app.on_event("startup")
|
127 |
def startup_event():
|
128 |
global comparer
|
129 |
+
# In production, use environment variables or configuration management for tokens.
|
130 |
auth_token = os.environ.get("HF_TOKEN")
|
131 |
comparer = QuranRecitationComparer(
|
132 |
model_name="jonatasgrosman/wav2vec2-large-xlsr-53-arabic",
|
|
|
134 |
)
|
135 |
print("Model initialized and ready for predictions!")
|
136 |
|
|
|
137 |
# --- API Endpoints ---
|
138 |
@app.get("/", summary="Health Check")
|
139 |
async def root():
|
140 |
return {"message": "Quran Recitation Comparer API is up and running."}
|
141 |
|
|
|
142 |
@app.post("/predict", summary="Compare Two Audio Files", response_model=dict)
|
143 |
async def predict(file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
144 |
"""
|
|
|
177 |
if tmp2_path and os.path.exists(tmp2_path):
|
178 |
os.remove(tmp2_path)
|
179 |
|
|
|
180 |
@app.post("/clear_cache", summary="Clear Embedding Cache", response_model=dict)
|
181 |
async def clear_cache():
|
182 |
"""
|