Spaces:
Sleeping
Sleeping
Mitchell Kilpatrick SE2022
commited on
Commit
·
256e579
1
Parent(s):
771d9ea
push
Browse files
app.py
CHANGED
|
@@ -1,83 +1,26 @@
|
|
| 1 |
-
import
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
| 4 |
-
import gradio as gr
|
| 5 |
|
| 6 |
-
# ----------------------
|
| 7 |
-
# Model configuration
|
| 8 |
-
# ----------------------
|
| 9 |
MODEL_NAME = "google/byt5-small"
|
| 10 |
|
| 11 |
-
logging.basicConfig(level=logging.INFO)
|
| 12 |
-
logging.info("Loading ByT5 model...")
|
| 13 |
-
|
| 14 |
-
# Load tokenizer and model
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 16 |
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 17 |
model.eval()
|
| 18 |
|
| 19 |
-
logging.info("Model loaded successfully")
|
| 20 |
-
|
| 21 |
-
# ----------------------
|
| 22 |
-
# Prompt engineering / fine-tuning-ready function
|
| 23 |
-
# ----------------------
|
| 24 |
def text_to_ipa(text: str) -> str:
|
| 25 |
-
""
|
| 26 |
-
|
| 27 |
-
You can customize this prompt for better results.
|
| 28 |
-
"""
|
| 29 |
-
prompt = f"""
|
| 30 |
-
You are a Scottish Gaelic tutor. Convert Gaelic text into IPA.
|
| 31 |
-
Only respond with the IPA transcription.
|
| 32 |
-
|
| 33 |
-
Text: beurla
|
| 34 |
-
IPA: /bjɤːRLə/
|
| 35 |
-
|
| 36 |
-
Text: faclair
|
| 37 |
-
IPA: /faxgLɛrʲ/
|
| 38 |
-
|
| 39 |
-
Text: {text}
|
| 40 |
-
IPA:
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
# Tokenize input
|
| 44 |
-
inputs = tokenizer(
|
| 45 |
-
prompt,
|
| 46 |
-
return_tensors="pt",
|
| 47 |
-
truncation=True,
|
| 48 |
-
max_length=512,
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Generate IPA
|
| 52 |
with torch.no_grad():
|
| 53 |
-
outputs = model.generate(
|
| 54 |
-
|
| 55 |
-
max_new_tokens=64,
|
| 56 |
-
do_sample=False,
|
| 57 |
-
num_beams=4,
|
| 58 |
-
repetition_penalty=1.1,
|
| 59 |
-
early_stopping=True,
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# ----------------------
|
| 65 |
-
# API setup (no UI)
|
| 66 |
-
# ----------------------
|
| 67 |
iface = gr.Interface(
|
| 68 |
fn=text_to_ipa,
|
| 69 |
inputs=gr.Textbox(),
|
| 70 |
outputs=gr.Textbox(),
|
| 71 |
-
api_name="predict" #
|
| 72 |
)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
iface.queue()
|
| 76 |
-
|
| 77 |
-
# Launch server
|
| 78 |
-
iface.launch(
|
| 79 |
-
server_name="0.0.0.0",
|
| 80 |
-
server_port=7860,
|
| 81 |
-
show_error=True,
|
| 82 |
-
# share=True, # optional: public URL for testing
|
| 83 |
-
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
MODEL_NAME = "google/byt5-small"
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 9 |
model.eval()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def text_to_ipa(text: str) -> str:
|
| 12 |
+
prompt = f"Text: {text}\nIPA:"
|
| 13 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
with torch.no_grad():
|
| 15 |
+
outputs = model.generate(**inputs, max_new_tokens=64)
|
| 16 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# ✅ This exposes /api/predict/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
iface = gr.Interface(
|
| 20 |
fn=text_to_ipa,
|
| 21 |
inputs=gr.Textbox(),
|
| 22 |
outputs=gr.Textbox(),
|
| 23 |
+
api_name="predict" # ← critical
|
| 24 |
)
|
| 25 |
|
| 26 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|