Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from streamlit_language_texts import *
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
|
7 |
+
st.set_page_config(layout="wide", page_title=app_title["eng"], page_icon="π²π¦")
|
8 |
+
|
9 |
+
token = os.getenv("HF_TOKEN")
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
languages = {"π¬π§ English": "eng", "π²π¦ Darija": "ary", "πΈπ¦ Arabic": "ara", "π«π· French": "fra"}
|
12 |
+
|
13 |
+
if "lang" not in st.query_params:
|
14 |
+
st.query_params["lang"] = "eng"
|
15 |
+
st.rerun()
|
16 |
+
|
17 |
+
def set_language():
|
18 |
+
if "selected_language" in st.session_state:
|
19 |
+
st.query_params["lang"] = languages.get(st.session_state["selected_language"])
|
20 |
+
|
21 |
+
with st.sidebar:
|
22 |
+
sel_lang = st.selectbox(
|
23 |
+
"Select a primary language for this app",
|
24 |
+
options=languages,
|
25 |
+
on_change=set_language,
|
26 |
+
key="selected_language",
|
27 |
+
)
|
28 |
+
|
29 |
+
if 'eng2ary_pipe' not in st.session_state:
|
30 |
+
st.session_state['eng2ary_pipe'] = pipeline("text2text-generation", model="ychafiqui/english-to-darija-2", token=token, device=device)
|
31 |
+
if 'ary2eng_pipe' not in st.session_state:
|
32 |
+
st.session_state['ary2eng_pipe'] = pipeline("text2text-generation", model="ychafiqui/darija-to-english-2", token=token, device=device)
|
33 |
+
|
34 |
+
def eng_to_ary(text):
|
35 |
+
if text:
|
36 |
+
return st.session_state.eng2ary_pipe(text)[0]['generated_text']
|
37 |
+
else:
|
38 |
+
return ""
|
39 |
+
|
40 |
+
def ary_to_eng(text):
|
41 |
+
if text:
|
42 |
+
return st.session_state.ary2eng_pipe(text)[0]['generated_text']
|
43 |
+
else:
|
44 |
+
return ""
|
45 |
+
|
46 |
+
st.title(app_title[languages[sel_lang]])
|
47 |
+
st.write(app_description[languages[sel_lang]])
|
48 |
+
|
49 |
+
direction = st.radio(".", ["English to Darija", "Darija to English"], key="lang", horizontal=True, label_visibility="collapsed")
|
50 |
+
|
51 |
+
if direction == "English to Darija":
|
52 |
+
input_col, output_col = st.columns(2)
|
53 |
+
input_col_container = input_col.container()
|
54 |
+
|
55 |
+
eng_text = input_col_container.text_area("π¬π§ Write English text here", key="eng_text")
|
56 |
+
|
57 |
+
output_col_container = output_col.container()
|
58 |
+
output_col_container.text_area("π²π¦ Darija translation will appear here", value=eng_to_ary(eng_text))
|
59 |
+
|
60 |
+
elif direction == "Darija to English":
|
61 |
+
input_col, output_col = st.columns(2)
|
62 |
+
input_col_container = input_col.container()
|
63 |
+
|
64 |
+
ary_text = input_col_container.text_area("π²π¦ Write Darija text here", key="ary_text")
|
65 |
+
|
66 |
+
output_col_container = output_col.container()
|
67 |
+
output_col_container.text_area("π¬π§ English translation will appear here", value=ary_to_eng(ary_text))
|
68 |
+
|
69 |
+
st.markdown(app_footer[languages[sel_lang]])
|