Spaces:
Sleeping
Sleeping
Schmitz005
commited on
Create burrito.py
Browse files- burrito.py +27 -0
burrito.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Title of the app
|
5 |
+
st.title('French to English Translator')
|
6 |
+
|
7 |
+
# Load the tokenizer and model
|
8 |
+
tokenizer = MarianTokenizer.from_pretrained('Helsinki-NLP/opus-mt-fr-en')
|
9 |
+
model = MarianMTModel.from_pretrained('Helsinki-NLP/opus-mt-fr-en')
|
10 |
+
|
11 |
+
# Text area for user input
|
12 |
+
user_input = st.text_area("Enter French text", "Bonjour le monde!")
|
13 |
+
|
14 |
+
# Function to translate text
|
15 |
+
def translate(text):
|
16 |
+
# Tokenize the text
|
17 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
18 |
+
# Generate translation outputs
|
19 |
+
translated = model.generate(**inputs)
|
20 |
+
# Decode the translated text
|
21 |
+
translated_text = tokenizer.batch_decode(translated, skip_special_tokens=True)[0]
|
22 |
+
return translated_text
|
23 |
+
|
24 |
+
# Button to perform translation
|
25 |
+
if st.button('Translate'):
|
26 |
+
translation = translate(user_input)
|
27 |
+
st.write('English Translation:', translation)
|