Spaces:
Running
Running
File size: 7,132 Bytes
bb5dd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 125 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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 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 | import os
import shutil
from contextlib import asynccontextmanager
from pathlib import Path
import requests
from dotenv import load_dotenv
from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from pymongo import MongoClient
load_dotenv()
MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017/")
WHISPER_MODEL_SIZE = os.getenv("WHISPER_MODEL", "small")
WHISPER_API_URL = os.getenv("WHISPER_API_URL", "").strip().rstrip("/")
DISFLUENCY_API_URL = os.getenv("DISFLUENCY_API_URL", "").strip().rstrip("/")
REMOTE_API_TIMEOUT = int(os.getenv("REMOTE_API_TIMEOUT", "300"))
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
UI_DIR = Path(__file__).parent / "ui"
client = MongoClient(MONGODB_URI)
db = client["SignApp"]
sign_rules_col = db["sign_rules"]
fingerspell_col = db["fingerspelling"]
_whisper_model = None
_disfluency_fn = None
def _auth_headers() -> dict[str, str]:
if not HF_TOKEN:
return {}
return {"Authorization": f"Bearer {HF_TOKEN}"}
def get_whisper():
global _whisper_model
if _whisper_model is None:
import whisper
_whisper_model = whisper.load_model(WHISPER_MODEL_SIZE)
return _whisper_model
def get_disfluency_fn():
global _disfluency_fn
if _disfluency_fn is None:
from .disfluency.inference import remove_disfluency
_disfluency_fn = remove_disfluency
return _disfluency_fn
def transcribe_audio(file_path: Path) -> dict:
if WHISPER_API_URL:
with file_path.open("rb") as audio_file:
response = requests.post(
f"{WHISPER_API_URL}/transcribe/",
headers=_auth_headers(),
files={"file": (file_path.name, audio_file, "audio/webm")},
timeout=REMOTE_API_TIMEOUT,
)
response.raise_for_status()
data = response.json()
return {
"text": data.get("text", ""),
"language": data.get("language", "en"),
}
whisper_model = get_whisper()
result = whisper_model.transcribe(str(file_path), language="en")
return {
"text": result["text"],
"language": result["language"],
}
def clean_disfluency(text: str) -> str:
if DISFLUENCY_API_URL:
response = requests.post(
f"{DISFLUENCY_API_URL}/clean/",
headers=_auth_headers(),
json={"text": text},
timeout=REMOTE_API_TIMEOUT,
)
response.raise_for_status()
data = response.json()
return data.get("cleaned_text", "").strip()
return get_disfluency_fn()(text)
@asynccontextmanager
async def lifespan(app: FastAPI):
if not WHISPER_API_URL:
print("Loading local Whisper model on startup...")
get_whisper()
else:
print(f"Using remote Whisper API: {WHISPER_API_URL}")
if not DISFLUENCY_API_URL:
print("Loading local disfluency model on startup...")
get_disfluency_fn()
else:
print(f"Using remote disfluency API: {DISFLUENCY_API_URL}")
print("SignApp startup complete.")
yield
app = FastAPI(title="SignApp", version="0.1.0", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
from .sign_language_text.gloss_converter import convert_to_sign_gloss
class TextInput(BaseModel):
text: str
def build_sign_sequence(gloss_tokens: list[str]) -> list[dict]:
"""Look up each gloss token in MongoDB sign_rules, fall back to fingerspelling."""
sign_sequence = []
for word in gloss_tokens:
rule = sign_rules_col.find_one({"sign": word})
if rule:
sign_sequence.append(
{
"type": "sign",
"gloss": word,
"handshape": rule["handshape"],
"location": rule["location"],
"movement": rule["movement"],
"expression": rule.get("expression", "neutral"),
}
)
else:
for letter in word:
finger = fingerspell_col.find_one({"letter": letter.upper()})
if finger:
sign_sequence.append(
{
"type": "fingerspell",
"letter": letter.upper(),
"handshape": finger["handshape"],
"location": "neutral_space",
"movement": finger.get("movement") or "none",
}
)
return sign_sequence
def text_pipeline(text: str) -> dict:
cleaned_text = clean_disfluency(text)
sign_friendly_text = convert_to_sign_gloss(cleaned_text)
sign_sequence = build_sign_sequence(sign_friendly_text)
return {
"cleaned_transcription": cleaned_text,
"sign_friendly_text": sign_friendly_text,
"sign_sequence": sign_sequence,
}
@app.get("/health")
def health():
return {
"status": "ok",
"whisper": "remote" if WHISPER_API_URL else "local",
"disfluency": "remote" if DISFLUENCY_API_URL else "local",
}
@app.post("/voice-to-text/")
def voice_to_text_endpoint(file: UploadFile = File(...)):
"""Full pipeline: audio -> transcription -> gloss -> sign sequence."""
file_path = UPLOAD_DIR / (file.filename or "recording.webm")
try:
with file_path.open("wb") as audio_file:
shutil.copyfileobj(file.file, audio_file)
transcription_result = transcribe_audio(file_path)
transcription = transcription_result["text"]
language = transcription_result["language"]
result = text_pipeline(transcription)
return {
"language": language,
"raw_transcription": transcription,
**result,
}
except requests.RequestException as exc:
raise HTTPException(status_code=502, detail=f"Remote model service failed: {exc}") from exc
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
if file_path.exists():
file_path.unlink()
@app.post("/text-to-sign/")
def text_to_sign_endpoint(body: TextInput):
"""Text-only pipeline: text -> gloss -> sign sequence."""
text = body.text.strip()
if not text:
raise HTTPException(status_code=400, detail="Text is empty")
try:
return text_pipeline(text)
except requests.RequestException as exc:
raise HTTPException(status_code=502, detail=f"Remote model service failed: {exc}") from exc
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
@app.get("/")
def serve_ui():
return FileResponse(UI_DIR / "index.html")
app.mount("/", StaticFiles(directory=str(UI_DIR)), name="ui")
|