Download app.py from NeoPy/SAPI4: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/spaces/NeoPy/SAPI4/resolve/b9ccc108fddd3052fef2bbf268f2cb10e33a14b0/app.py
- Command line
-
hf download hf://spaces/NeoPy/SAPI4@b9ccc108fddd3052fef2bbf268f2cb10e33a14b0/app.py
-
curl -L -o app.py https://huggingface.co/spaces/NeoPy/SAPI4/resolve/b9ccc108fddd3052fef2bbf268f2cb10e33a14b0/app.py
1.34 kB
| import gradio as gr | |
| from gradio import Server | |
| from transformers import T5ForConditionalGeneration, T5Tokenizer | |
| # Load model and tokenizer | |
| MODEL_NAME = "alirezamsh/small100" | |
| print("Loading model...") | |
| tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME) | |
| model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME) | |
| print("Model loaded!") | |
| app = Server() | |
| def translate(text, prompt): | |
| """ | |
| Translate text using T5 with a task prefix. | |
| Note: t5-small is trained mainly on English tasks and has limited | |
| multilingual translation ability. See note at the bottom. | |
| """ | |
| if not text.strip(): | |
| return "Please enter some text to translate." | |
| tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_length=512, | |
| num_beams=4, | |
| early_stopping=True, | |
| ) | |
| result = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return result | |
| # βββββββββββββββββββββββββββββββββββββββββββββ | |
| # 5. LAUNCH | |
| # βββββββββββββββββββββββββββββββββββββββββββββ | |
| app.launch() |