File size: 1,291 Bytes
212dc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import os
from typing import Any

import streamlit as st
import streamlit.components.v1 as components


def is_on_huggingface() -> bool:
    return os.environ.get("SPACE_REPO_NAME") is not None


def download_link(title: str, data: Any, file_name: str, mime_type: str):
    b64_bytes = base64.urlsafe_b64encode(data)
    b64_string = b64_bytes.decode()

    file_string = f"data:{mime_type};base64,{b64_string}"

    components.html(f"""
            <style>
                a {{
                    font-family: "Source Sans Pro", sans-serif;
                    color: rgb(250, 250, 250);
                }}
            </style>
            <a download="{file_name}" href="{file_string}">{title}</a>
            """, height=30)


def main():
    if is_on_huggingface():
        st.title("Example on HuggingFace")
    else:
        st.title("Example Local")

    with st.form("Generate"):
        x = st.slider('Select a value')

        submitted = st.form_submit_button("Generate")

    if submitted:
        st.write("File has been created")

        data = bytes(f"value squared: {x}", 'utf-8')

        st.download_button("Download", data, "test.txt", "text/plain")
        download_link("Download", data, "test.txt", "text/plain")


if __name__ == "__main__":
    main()