Commit
·
e42c125
1
Parent(s):
3aede43
Create api.py
Browse files
api.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 2 |
+
|
| 3 |
+
# Define the model and tokenizer
|
| 4 |
+
model_name = "t5-small"
|
| 5 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
|
| 8 |
+
# Provide a text to be converted
|
| 9 |
+
text = "Translate this text to French."
|
| 10 |
+
|
| 11 |
+
# Tokenize the text and generate the output
|
| 12 |
+
inputs = tokenizer.encode("translate English to French: " + text, return_tensors="pt")
|
| 13 |
+
outputs = model.generate(inputs, max_length=40, num_beams=4, early_stopping=True)
|
| 14 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 15 |
+
|
| 16 |
+
# Print the generated text
|
| 17 |
+
print("Generated Text:", generated_text)
|