Spaces:
Runtime error
Runtime error
File size: 4,237 Bytes
b45aa80 2a36db0 7239bb1 b45aa80 9e932f2 2c71c59 2a36db0 2c71c59 2a36db0 9e932f2 2c71c59 2a36db0 9e5f446 48961ce 49d636d 48961ce 9e5f446 9e932f2 9e5f446 48961ce df7aef4 bb74e62 df7aef4 48961ce df7aef4 de6b037 48961ce c124fb1 48961ce 9e932f2 b324001 9e5f446 9e932f2 d67c5db 9e5f446 9e932f2 9e5f446 |
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 |
import gradio as gr
import os
import requests
import base64
# Get the Hugging Face token for accessing the private space
hf_read_write = os.getenv('hf_read_write')
print('hf_read_write = ', hf_read_write)
# Load the private space interface
iface = gr.load("spaces/Preeda/stock-update-processer", hf_token=hf_read_write)
# Define a function to return the file for download
def download_file():
# List of file URLs (replace these with your actual file URLs)
file_list = [
"https://github.com/mmlanla/resource_share/raw/main/sp_1_shop_1.xlsx",
"https://github.com/mmlanla/resource_share/raw/main/sp_1_shop_2.xlsx",
"https://github.com/mmlanla/resource_share/raw/main/sp_1_laz_1.xlsx"
# Add more URLs here
]
# Loop through the file list
outfile_path = []
for i, file_url in enumerate(file_list):
try:
print(f"Downloading file {i+1} from: {file_url}")
# Download the base64 encoded file content from GitHub
response = requests.get(file_url)
# Ensure the download was successful
if response.status_code == 200:
base64_content = response.text # Get the raw text content (base64 encoded)
# Decode the base64 content
decoded_content = base64.b64decode(base64_content)
# Save the decoded content back to a file (give it a unique name or index it)
output_file = f"downloaded_file_{i+1}.xlsx"
with open(output_file, "wb") as file:
file.write(decoded_content)
print(f"File {i+1} downloaded and saved as '{output_file}'.")
abs_path = os.path.abspath(output_file)
print(f"File {i+1} downloaded and saved as '{output_file}' : abs_path",abs_path)
else:
print(f"Failed to download file {i+1} from {file_url}. Status code: {response.status_code}")
outfile_path.append(abs_path)
except Exception as e:
print(f"An error occurred while downloading file {i+1}: {e}")
return outfile_path
# Create a new Blocks interface for the download button
with gr.Blocks() as demo:
# Inject CSS for custom button height
gr.HTML("""
<style>
.custom-button .gr-button {
height: 10px; /* Set your desired height */
width: 150px; /* Optional: Set custom width */
font-size: 16px; /* Optional: Set custom font size */
}
</style>
""")
# Add a button and file download functionality
with gr.Row():
# download_btn = gr.Button("Download File 1")
download_btn = gr.Button("Download File", elem_id="custom_button")
file_output = gr.File(label="Get file for Shopee (shop 1)", file_types=[".xlsx"])
file_output_2 = gr.File(label="Get file for Shopee (shop 2)", file_types=[".xlsx"])
file_output_3 = gr.File(label="Get file for Lazada (shop 1)", file_types=[".xlsx"])
# Set up the button click to trigger file download
download_btn.click(download_file, inputs=None, outputs=[file_output,file_output_2,file_output_3])
# Load the private interface from the space inside this block
gr.Markdown("## Loaded Private Space Interface:")
iface.render() # Use render() to embed the loaded space interface into this custom block
# Launch the combined interface
demo.launch()
if 0:
import gradio as gr
import os
# Replace 'your_token_here' with your actual Hugging Face access token
# hf_token = os.environ['hf_write']
hf_read_write = os.getenv('hf_read_write')
print('hf_read_write = ', hf_read_write)
# os.environ['HF_TOKEN'] = ".."
# Load the Space with access token
# iface = gr.load("spaces/Preeda/private_test", hf_token=hf_write)
# iface = gr.load("spaces/Preeda/stock_auto_update", hf_token=hf_write)
iface = gr.load("spaces/Preeda/stock-update-processer", hf_token=hf_read_write)
print('OK')
# iface.launch(share=True, allow_flagging='never', show_download_button=True)
iface.launch()
|