Preeda's picture
Update app.py
c124fb1 verified
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()