Skip to content

Instantly share code, notes, and snippets.

Avatar

Korakot Chaovavanich korakot

  • VISTEC
  • Bangkok, Thailand
View GitHub Profile
@korakot
korakot / request_google.py
Last active Jan 19, 2021
Use requests with Google default credentials in Colab
View request_google.py
import google.auth
from google.auth.transport.requests import AuthorizedSession
# login
from google.colab import auth
auth.authenticate_user()
# use default credentials to create session
cred = google.auth.default()[0] # for cred.token
s = AuthorizedSession(cred)
# s.get or s.post
url = 'https://storage.googleapis.com/storage/v1/b/co-lab'
@korakot
korakot / to_sec.py
Created Jan 6, 2021
Convert time_string to seconds
View to_sec.py
def to_sec(tstr):
""" 1:01:01 -> 3661 """
t = 0
for p in tstr.split(':'):
t *= 60
t += int(p)
return t
@korakot
korakot / duration.py
Last active Jan 6, 2021
Get youtube video duration from its id, using YouTube Data API.
View duration.py
from requests import get
!pip install isodate -q
import isodate
key = 'AZxxxx' # from https://console.cloud.google.com/apis/credentials
def get_durations(id_list):
url = 'https://www.googleapis.com/youtube/v3/videos'
kw = {
'id': ','.join(id_list),
@korakot
korakot / dict_string.py
Last active Dec 30, 2020
Create a dict from string easily. No need to quote each string.
View dict_string.py
d = '''
a 1
b 2
c 3
'''.strip().split('\n')
d = dict(row.split() for row in d)
# {'a': '1', 'b': '2', 'c': '3'}
@korakot
korakot / df_string.py
Created Dec 30, 2020
Create pandas dataframe from string
View df_string.py
import pandas as pd
from io import StringIO
data = StringIO('''
symbol,quan,v0
ONE-UGERMF, 71472.71, 21.9749
ONE-UGG-ASSF, 5593.87, 35.7534
1SG-LTF-T, 11523.57, 31.9562
'''.strip())
@korakot
korakot / fastquant.py
Created Dec 29, 2020
Install fastquant on Colab without fix-versioned dependency
View fastquant.py
# manually install dependencies, then use no-deps
!pip install backtrader croniter cryptography twython yfinance
!pip install fastquant ccxt --no-deps
@korakot
korakot / plotly.py
Last active Dec 30, 2020
Use plotly as pandas backend in Colab
View plotly.py
!pip install -U plotly
import pandas as pd
pd.options.plotting.backend = "plotly"
df.col.plot()
# To get log scale
fig = df.col.plot() # can't logy=True
fig.update_layout(yaxis_type="log")
@korakot
korakot / kenlm.py
Created Dec 11, 2020
Install KenLM in Colab
View kenlm.py
!curl -L bit.ly/kenlm-colab | tar xz -C /
@korakot
korakot / mfa_align.py
Created Dec 9, 2020
Align wav file to label, with Montreal Forced Aligner
View mfa_align.py
# install
import pandas as pd
!pip install textgrid -q
from textgrid import TextGrid
url = 'https://github.com/MontrealCorpusTools/Montreal-Forced-Aligner/releases/download/v1.1.0-beta.2/montreal-forced-aligner_linux.tar.gz'
!curl -L $url | tar xz --strip-components=2
!wget https://github.com/MontrealCorpusTools/mfa-models/raw/master/acoustic/thai.zip
!mv thai.zip pretrained_models/
!gdown --id 1-61YXBHhuXWmsRsi_QG-3nmCzzD2MlPR
!mv mfa_dict_2.txt dict.txt
@korakot
korakot / tg_to_df.py
Created Dec 9, 2020
Convert textgrid to dataframe of words
View tg_to_df.py
import pandas as pd
!pip install textgrid -q
from textgrid import TextGrid
def read_tg(tg_file):
tg = TextGrid.fromFile(tg_file)
data = [(w.minTime, w.maxTime, w.mark) for w in tg[0]]
left, right, words = zip(*data)
index = pd.IntervalIndex.from_arrays(left, right, name='time')
df = pd.DataFrame(words, index, ['word'])
You can’t perform that action at this time.