File size: 4,521 Bytes
c2522bb
 
 
 
 
 
aa667a1
c2522bb
 
 
fa154e4
 
 
 
c2522bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa667a1
 
 
 
 
c2522bb
fa154e4
 
 
aa667a1
fa154e4
 
 
 
 
 
 
aa667a1
 
c2522bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2174f1
 
 
c2522bb
 
b2174f1
c2522bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os 
import io
import pickle
import base64
import streamlit as st
import pandas as pd
import numpy as np

from htbuilder import HtmlElement, div, hr, a, p, img, styles
from pathlib import Path
from huggingface_hub.inference_api import InferenceApi
#from google.oauth2 import service_account
#from googleapiclient.discovery import build




######################## LOAD DATA FROM REPO ##########################

@st.cache_data(ttl=3600, show_spinner=False)
def load_data_pickle(path, file):
    """Load data from pickle file"""
    df = pd.read_pickle(os.path.join(path,file))
    return df

@st.cache_data(ttl=3600, show_spinner=False)
def load_data_csv(path, file):
    "Load data from csv file"
    df = pd.read_csv(os.path.join(path,file))
    return df

@st.cache_data(ttl=3600, show_spinner=False)
def load_model_pickle(path, file):
    """Load model from pickle file"""
    path_file = os.path.join(path, file)
    model = pickle.load(open(path_file, 'rb'))
    return model

@st.cache_data(ttl=3600, show_spinner=False)
def load_numpy(path,file):
    array = np.load(os.path.join(path,file))
    return array



###################### LOAD MODEL HUGGINGFACE #############################

st.cache_data(ttl=3600, show_spinner=False)
def load_model_huggingface(repo_id, token, task=None):
    """ Load model using Huggingface's Inference API 
    """
    model = InferenceApi(repo_id=repo_id, token=token, task=task)
    return model




#################### LOAD DATA FROM GOOGLE DRIVE ###################

# @st.cache_data(ttl=3600, show_spinner=False)
# def load_data(file, sheet_name, **kwargs):
#     df = pd.read_excel(file, sheet_name=sheet_name, **kwargs)
#     return df

# @st.cache_data(ttl=3600, show_spinner=False)
# def load_model(file):
#     """Load model from pickle file"""
#     model = pickle.load(file)
#     return model


# @st.cache_data(show_spinner=False) #3600 seconds
# def authenticate_drive():
#     creds = service_account.Credentials.from_service_account_info(
#         st.secrets["connections_gcs"],
#         scopes=["https://www.googleapis.com/auth/drive.readonly"]
#     )
#     drive_service = build('drive', 'v3', credentials=creds)
    
#     return drive_service

# @st.cache_data(ttl=3600, show_spinner=False)
# def load_content_drive(file_id, _drive_service):
#     """ Load content from google drive
#     """
#     request = _drive_service.files().get_media(fileId=file_id)
#     file_content = io.BytesIO(request.execute())
    
#     return file_content
    

# @st.cache_data(ttl=3600, show_spinner=False)
# def load_data_drive(file_content, sheet_name=None, **kwargs):
#     """ Load data using file_content    
#     """
#     if sheet_name is None:
#         df = pd.read_excel(file_content, **kwargs)
#     else:
#         df = pd.read_excel(file_content, sheet_name=sheet_name, **kwargs)
    
#     return df




#################### PASSEWORD #####################

def check_password():
    """Returns `True` if the user had the correct password."""

    password_key = os.environ["PASSWORD"]
    #password_key = st.secrets["password"]

    def password_entered():
        """Checks whether a password entered by the user is correct."""
        if "password" in st.session_state and st.session_state["password"] == password_key:
            st.session_state["password_correct"] = True
            del st.session_state["password"]  # don't store password
        else:
            st.session_state["password_correct"] = False

    if "password_correct" not in st.session_state:
        # First run, show input for password.
        st.text_input(
            "Password", type="password", on_change=password_entered, key="password"
        )
        return False
    elif not st.session_state["password_correct"]:
        # Password not correct, show input + error.
        st.text_input(
            "Password", type="password", on_change=password_entered, key="password"
        )
        st.error("😕 Password incorrect")
        return False
    else:
        # Password correct.
        return True


###################### OTHER ######################

def img_to_bytes(img_path):
    img_bytes = Path(img_path).read_bytes()
    encoded = base64.b64encode(img_bytes).decode()
    return encoded


def link(link, text, **style):
    return a(_href=link, _target="_blank", style=styles(**style))(text)

@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
    return df.to_csv().encode('utf-8')