# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/bstevens94/Babel/blob/main/B_b3l.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="I9Jp-W4SKboc"
                                                         
                                  .--,-``-.              
    ,---,.                       /   /     '.    ,--,    
  ,'  .'  \             ,---,   / ../        ; ,--.'|    
,---.' .' |           ,---.'|   \ ``\  .`-    '|  | :    
|   |  |: |           |   | :    \___\/   \   ::  : '    
:   :  :  /  ,--.--.  :   : :         \   :   ||  ' |    
:   |    ;  /       \ :     |,-.      /  /   / '  | |    
|   :     \.--.  .-. ||   : '  |      \  \   \ |  | :    
|   |   . | \__\/: . .|   |  / :  ___ /   :   |'  : |__  
'   :  '; | ," .--.; |'   : |: | /   /\   /   :|  | '.'| 
|   |  | ; /  /  ,.  ||   | '/ :/ ,,/  ',-    .;  :    ; 
|   :   / ;  :   .'   \   :    |\ ''\        ; |  ,   /  
|   | ,'  |  ,     .-./    \  /  \   \     .'   ---`-'   
`----'     `--`---'   `-'----'    `--`-,,-'              
.
Results may vary. Godspeed.

By: <NAME>.


A mobile browser friendly audio transcriber and text translator built using txtai
---
---
---
Hardware requirements: Ability to open browser and internet
  

(don't press play on this cell obv)

# + [markdown] id="_33L79RfK-fT"
# ---
# ---
# ---

# + id="KJzbtfMp0Wnq"
#@title Press play and follow the prompts below. This will upload your audio file and convert filetype automatically. Proceed when finished. { display-mode: "form" }

#@markdown ---

from google.colab import files
import sys
import os
import re
import IPython
from IPython.display import clear_output

uploaded = files.upload()
filename = next(iter(uploaded))
print(uploaded)
for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))
  
#converting to 16khz .wav from input
# !ffmpeg -i $filename -acodec pcm_s16le -ac 1 -ar 16000 out.wav

final_wav = 'out.wav'


#installing and initializing transcription and translation

# !pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline]

clear_output()
from txtai.pipeline import Transcription
from txtai.pipeline import Translation

# Create transcription model
transcribe = Transcription("facebook/wav2vec2-large-960h")
# Create translation model
translate = Translation()
clear_output()




# + id="-K2YJJzsVtfq"
#@title Please input the two letter language code for your desired translation output. { display-mode: "form" }
#@markdown Example: es for Espanol. 
from IPython.display import Audio, display

text = transcribe("out.wav")
Language = "" #@param {type: "string"}
translated = translate(text, Language)

clear_output()





print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')



display(Audio("out.wav"))
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')

print('Transcription:')
print('---------------------------------------------------------------')

print(text)
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('Translated Text:')
print('---------------------------------------------------------------')
print(translated)
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')




# + cellView="form" id="9BRW5s5hOeFP"
#@title Press Play to download .txt file of transcription and translation to your device.
#@markdown This is recommended as translations are lost after browser closes. { display-mode: "form" }
with open('translated.txt', 'w') as f:
  f.write('Original:\n')
  f.write(text + "\n")
  f.write('Translated:\n')
  f.write(translated)


files.download('translated.txt')


# + [markdown] id="S4p6Hd9dDU63"
# ---
# ---
# ---
# Shoutout to txtai (https://github.com/neuml/txtai)

StarcoderdataJupyterScriptsDedupFiltered