Ahsan658 commited on
Commit
cf265ad
1 Parent(s): 9d3f3ce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ # Load the translation pipeline from Hugging Face
5
+ translator = pipeline("translation_en_to_fr") # Default to French translation
6
+
7
+ # Available languages and their corresponding models
8
+ available_languages = {
9
+ "French": "translation_en_to_fr",
10
+ "German": "translation_en_to_de",
11
+ "Spanish": "translation_en_to_es",
12
+ "Chinese": "Helsinki-NLP/opus-mt-en-zh", # For Chinese, we use a specific model from Hugging Face
13
+ "Japanese": "Helsinki-NLP/opus-mt-en-jap",
14
+ "Russian": "Helsinki-NLP/opus-mt-en-ru",
15
+ "Arabic": "Helsinki-NLP/opus-mt-en-ar",
16
+ }
17
+
18
+ # Streamlit app title
19
+ st.title("Language Translator")
20
+
21
+ # User input for text to translate
22
+ text_to_translate = st.text_area("Enter text in English:", "")
23
+
24
+ # Language selection
25
+ target_language = st.selectbox("Select the target language:", list(available_languages.keys()))
26
+
27
+ # Change the translation model based on the selected language
28
+ translator = pipeline("translation", model=available_languages[target_language])
29
+
30
+ # Translate button
31
+ if st.button("Translate"):
32
+ if text_to_translate:
33
+ # Perform the translation
34
+ translation = translator(text_to_translate)
35
+ # Display the translated text
36
+ st.write(f"**Translated text in {target_language}:**")
37
+ st.write(translation[0]['translation_text'])
38
+ else:
39
+ st.warning("Please enter some text to translate.")
40
+
41
+ # Footer
42
+ st.markdown("Powered by [Hugging Face Transformers](https://huggingface.co/transformers/).")