File size: 1,864 Bytes
8fb3b4a d1a442d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
from PIL import Image
import json
import streamlit as st
def load_image_from_local(image_path, image_resize=None):
image = Image.open(image_path)
if isinstance(image_resize, tuple):
image = image.resize(image_resize)
return image
# # reading the nct_ta dictionary
# with open('asset/data/input_sentence.json') as f:
# data = f.read()
# sample_sentences = json.loads(data) # load dict
# print(sample_sentences)
def unique_list(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
def pure_comma_separation(list_str, return_list=True):
r = unique_list([item.strip() for item in list_str.lower().split(",") if item.strip()])
if return_list:
return r
return ", ".join(r)
def local_css(css_path):
with open(css_path) as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
def remote_css(css_url):
st.markdown(f'<link href="{css_url}" rel="stylesheet">', unsafe_allow_html=True)
"""Basic utilities module"""
import requests
import csv
import re
def request_ct(url):
"""Performs a get request that provides a (somewhat) useful error message."""
try:
response = requests.get(url)
except ImportError:
raise ImportError(
"Couldn't retrieve the data, check your search expression or try again later."
)
else:
return response
def json_handler(url):
"""Returns request in JSON (dict) format"""
return request_ct(url).json()
def csv_handler(url):
"""Returns request in CSV (list of records) format"""
response = request_ct(url)
decoded_content = response.content.decode("utf-8")
split_by_blank = re.split(r"\n\s*\n", decoded_content) # Extracts header info
cr = csv.reader(split_by_blank[1].splitlines(), delimiter=",")
records = list(cr)
return records |