guymorlan commited on
Commit
84d9d62
1 Parent(s): fcb9043

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -2
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
  import pandas as pd
 
 
 
 
4
 
5
  dialects = {"Palestinian/Jordanian": "P", "Syrian": "S", "Lebanese": "L", "Egyptian": "E"}
6
 
@@ -11,17 +15,40 @@ st.title("English to Levantine Arabic")
11
  num_translations = st.sidebar.selectbox("Number of Translations Per Dialect:", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], index=0)
12
  input_text = st.text_input("Enter English text:")
13
 
14
- if input_text:
 
15
  inputs = [f"{val} {input_text}" for val in dialects.values()]
16
  result = pipeline(inputs, max_length=1024, num_return_sequences=num_translations, num_beams=max(num_translations, 5))
 
 
 
 
17
  #df = pd.DataFrame({"Dialect": [x for x in dialects.keys()],
18
  # "Translation": [x["translation_text"] for x in result]})
19
 
20
  for i in range(len(result)):
 
21
  st.markdown(f"<div style='font-size:24px'><b>{list(dialects.keys())[i]}:</b></div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  if num_translations > 1:
23
  for j in range(num_translations):
24
  st.markdown(f"<div style='font-size:24px; text-align:right; direction:rtl;'>{result[i][j]['translation_text']}</div>", unsafe_allow_html=True)
25
  else:
26
  st.markdown(f"<div style='font-size:24px; text-align:right; direction:rtl;'>{result[i]['translation_text']}</div>", unsafe_allow_html=True)
27
- st.markdown("<br>", unsafe_allow_html=True)
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
  import pandas as pd
4
+ import os
5
+ import azure.cognitiveservices.speech as speechsdk
6
+ import base64
7
+ import torch
8
 
9
  dialects = {"Palestinian/Jordanian": "P", "Syrian": "S", "Lebanese": "L", "Egyptian": "E"}
10
 
 
15
  num_translations = st.sidebar.selectbox("Number of Translations Per Dialect:", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], index=0)
16
  input_text = st.text_input("Enter English text:")
17
 
18
+ @st.cache(hash_funcs={torch.nn.parameter.Parameter: lambda parameter: parameter.data.numpy()})
19
+ def get_translation(input_text, num_translations):
20
  inputs = [f"{val} {input_text}" for val in dialects.values()]
21
  result = pipeline(inputs, max_length=1024, num_return_sequences=num_translations, num_beams=max(num_translations, 5))
22
+ return result
23
+
24
+ if input_text:
25
+ result = get_translation(input_text, num_translations)
26
  #df = pd.DataFrame({"Dialect": [x for x in dialects.keys()],
27
  # "Translation": [x["translation_text"] for x in result]})
28
 
29
  for i in range(len(result)):
30
+ # play = st.button("Play Audio (Machine Generated)")
31
  st.markdown(f"<div style='font-size:24px'><b>{list(dialects.keys())[i]}:</b></div>", unsafe_allow_html=True)
32
+ if i == 0:
33
+ if num_translations > 1:
34
+ get = result[0][0]["translation_text"]
35
+ else:
36
+ get = result[0]["translation_text"]
37
+
38
+ speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
39
+ audio_config = speechsdk.audio.AudioOutputConfig(filename=f"{input_text}.wav")
40
+ speech_config.speech_synthesis_voice_name='ar-SY-AmanyNeural'
41
+ speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
42
+ speech_synthesis_result = speech_synthesizer.speak_text_async(get).get()
43
+ audio_file = open(f"{input_text}.wav", "rb")
44
+ audio_bytes = audio_file.read()
45
+ #autoplay_audio(f"{input_text}.wav")
46
+ st.audio(audio_bytes, format="audio/mp3", start_time=0)
47
+
48
  if num_translations > 1:
49
  for j in range(num_translations):
50
  st.markdown(f"<div style='font-size:24px; text-align:right; direction:rtl;'>{result[i][j]['translation_text']}</div>", unsafe_allow_html=True)
51
  else:
52
  st.markdown(f"<div style='font-size:24px; text-align:right; direction:rtl;'>{result[i]['translation_text']}</div>", unsafe_allow_html=True)
53
+ st.markdown("<br>", unsafe_allow_html=True)
54
+