Spaces:
Sleeping
Sleeping
File size: 1,317 Bytes
f810166 2b6b06d f810166 2b6b06d b24a61c 2b6b06d b24a61c 2b6b06d b24a61c 2b6b06d b24a61c 2b6b06d b24a61c 2b6b06d |
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 |
from fastapi import FastAPI
from pydantic import BaseModel
import torch
from transformers import AutoTokenizer, T5ForConditionalGeneration
MODEL_NAME = "google/byt5-small"
app = FastAPI()
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
model.eval()
print("Model loaded.")
class TextRequest(BaseModel):
text: str
def text_to_ipa(text: str) -> str:
# Few-shot examples for better IPA predictions
prompt = f"""
You are a Scottish Gaelic teacher.
Convert Scottish Gaelic text into the International Phonetic Alphabet (IPA).
Only return the IPA transcription.
Examples:
Text: halò
IPA: /haˈloː/
Text: uisge
IPA: /ˈɯʃkʲə/
Text: {text}
IPA:
"""
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=64,
do_sample=False # deterministic output
)
# Decode and return only the IPA portion
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
return result.split("IPA:")[-1].strip()
@app.post("/predict")
def predict(request: TextRequest):
ipa_result = text_to_ipa(request.text)
return {"ipa": ipa_result}
|