finalproject / app.py
Assemm's picture
Add app.py and requirements.txt for translation app
5809bcc
import streamlit as st
from transformers import pipeline
x = st.slider('Select a value')
st.write(x, 'squared is', x * x)
# Title and Description
st.title("Translation Web App")
st.write("""
# Powered by Hugging Face and Streamlit
This app uses a pre-trained NLP model from Hugging Face to translate text from one language to another.
You can enter text and select the source and target languages for translation.
""")
# Cache model to improve performance
@st.cache_resource
def load_model():
print("Loading translation model...")
return pipeline("translation", model="Helsinki-NLP/opus-mt-en-ru") # Translation model: English to Russian
# Initialize Hugging Face Translation Pipeline
translator = load_model()
# Language selection (user chooses source and target language)
source_language = st.selectbox("Select Source Language", ["English", "French", "German", "Spanish", "Russian"])
target_language = st.selectbox("Select Target Language", ["English", "French", "German", "Spanish", "Russian"])
# Text input from the user
user_input = st.text_area("Enter text to translate:")
# Translate the input text
if user_input:
if source_language == target_language:
st.write("Source and target language are the same. Please choose different languages.")
else:
# Translate using Hugging Face model
translation = translator(user_input)
translated_text = translation[0]['translation_text']
st.write(f"### Translated Text ({target_language}):")
st.write(translated_text)