Spaces:
Sleeping
Sleeping
File size: 1,198 Bytes
8e92c05 |
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 |
import streamlit as st
import requests
import os
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from transformers import pipeline
st.title("Translation App")
# Load the model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
def translate(text, src_lang, tgt_lang):
translator = pipeline(
"translation",
model=model,
tokenizer=tokenizer,
src_lang=src_lang,
tgt_lang=tgt_lang,
)
output = translator(text, max_length=400)
return output[0]["translation_text"]
def main():
src_lang = st.text_input("Enter source language code (e.g., en):")
tgt_lang = st.text_input("Enter target language code (e.g., fr):")
text = st.text_area("Enter text to translate:")
if st.button("Translate"):
if src_lang and tgt_lang and text:
result = translate(text, src_lang, tgt_lang)
st.write("Translated Text:", result)
else:
st.warning("Please provide source language, target language, and text to translate.")
if __name__ == "__main__":
main()
|