Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- ReadMe.txt +73 -0
- main.py +41 -0
- model.py +5 -0
- requirements.txt +3 -0
ReadMe.txt
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### **Project: English to French Translator App**
|
2 |
+
|
3 |
+
#### **Description**
|
4 |
+
This Streamlit application translates English text to French. It uses a pre-trained transformer model from Hugging Face to perform the translation. Users can enter multiple lines of text, and the translated text will be shown line by line. The app also includes a reset button to clear both the input and output fields.
|
5 |
+
|
6 |
+
#### **Features**
|
7 |
+
- **English to French Translation**: Using the Hugging Face translation model to translate the input English text to French.
|
8 |
+
- **Line-by-Line Translation**: Preserves the original line breaks in the translation.
|
9 |
+
- **Reset Functionality**: Clears output after pressing the reset button.
|
10 |
+
|
11 |
+
---
|
12 |
+
|
13 |
+
### **Usage**
|
14 |
+
|
15 |
+
1. **Install Dependencies**: Install the required Python libraries using the `requirements.txt`.
|
16 |
+
|
17 |
+
```bash
|
18 |
+
pip install -r requirements.txt
|
19 |
+
```
|
20 |
+
|
21 |
+
2. **Run the App**:
|
22 |
+
After installing the dependencies, run the Streamlit app with the following command:
|
23 |
+
|
24 |
+
```bash
|
25 |
+
streamlit run main.py
|
26 |
+
```
|
27 |
+
|
28 |
+
3. **Interacting with the App**:
|
29 |
+
- Enter your English text in the text area.
|
30 |
+
- Click the "Translate" button to see the French translation of the input text.
|
31 |
+
- Use the "Reset" button to clear the input and translated text.
|
32 |
+
|
33 |
+
---
|
34 |
+
|
35 |
+
### **Code Walkthrough**
|
36 |
+
|
37 |
+
1. **Importing the Libraries**:
|
38 |
+
- We import the necessary libraries, such as `pipeline` from Hugging Face and `streamlit` for the app interface.
|
39 |
+
|
40 |
+
2. **Session State**:
|
41 |
+
- `input_text` and `translated_lines` are stored in `st.session_state` to preserve the text entered and translated across multiple interactions with the app.
|
42 |
+
|
43 |
+
3. **Text Area**:
|
44 |
+
- The `input_text` is entered in a text area. This text is split by lines to preserve the line structure when translating.
|
45 |
+
|
46 |
+
4. **Translation Logic**:
|
47 |
+
- On clicking the "Translate" button, the input text is passed to the model for translation. The translation result for each line is stored in a list and displayed line by line.
|
48 |
+
|
49 |
+
5. **Reset Button**:
|
50 |
+
- The reset button clears the `translated_lines` in session state.
|
51 |
+
|
52 |
+
---
|
53 |
+
|
54 |
+
### **requirements.txt**
|
55 |
+
|
56 |
+
```txt
|
57 |
+
streamlit==1.10.0
|
58 |
+
transformers==4.12.5
|
59 |
+
sentencepiece==0.1.96
|
60 |
+
```
|
61 |
+
|
62 |
+
---
|
63 |
+
|
64 |
+
### **Troubleshooting**
|
65 |
+
- If you face issues with model loading or the translation, ensure you have an active internet connection to access the Hugging Face model.
|
66 |
+
- The "Reset" button works only if there is something written in the text area.
|
67 |
+
|
68 |
+
---
|
69 |
+
|
70 |
+
### **How to Contribute**
|
71 |
+
- If you'd like to contribute to the functionality of the reset button or improve the user interface, feel free to fork the repository and submit a pull request.
|
72 |
+
|
73 |
+
---
|
main.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from model import *
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
st.title("English to French Transaltor")
|
5 |
+
|
6 |
+
if 'input_text' not in st.session_state:
|
7 |
+
st.session_state['input_text'] = ''
|
8 |
+
|
9 |
+
if 'translated_lines' not in st.session_state:
|
10 |
+
st.session_state['translated_lines'] = []
|
11 |
+
|
12 |
+
input_text = st.text_area("Enter your English Text", value=st.session_state['input_text'], key="input_text")
|
13 |
+
|
14 |
+
if st.button("Translate"):
|
15 |
+
if input_text.strip():
|
16 |
+
lines = input_text.split("\n") # Split input text into lines
|
17 |
+
translated_lines = [] # List to store translated lines
|
18 |
+
for line in lines: # Loop through each line
|
19 |
+
translated_result = translator(line) # Pass each line to the translator
|
20 |
+
translated_text = translated_result[0]['translation_text'] # Get the translated text
|
21 |
+
translated_lines.append(translated_text) # Append translated text to the list
|
22 |
+
|
23 |
+
st.session_state['translated_lines'] = translated_lines
|
24 |
+
|
25 |
+
for translated_line in translated_lines:
|
26 |
+
st.write(translated_line) # Display each translated line separately
|
27 |
+
else:
|
28 |
+
st.write("Error! Please write something in the text area first!")
|
29 |
+
|
30 |
+
# Reset button logic
|
31 |
+
if st.button("Reset"):
|
32 |
+
if input_text.strip():
|
33 |
+
# Reset both input and translated text
|
34 |
+
st.session_state['input_text'] = ' '
|
35 |
+
st.session_state['translated_lines'] = []
|
36 |
+
else:
|
37 |
+
st.write("Error! Please write something in the text area first!")
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
# HOW TO RUN: streamlit run main.py
|
model.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use a pipeline as a high-level helper
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
translator = pipeline("translation", model="PaulineSanchez/autotrain-translation_food_english_to_french-52830124391")
|
5 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.10.0
|
2 |
+
transformers==4.12.5
|
3 |
+
sentencepiece==0.1.96
|