listener / app.py
hltdx's picture
Adding password type input for save_data function.
c742adc unverified
import gradio as gr
import os
# Define the path to the local storage file
local_storage_file = "local_storage.txt"
# Specify the secret value
save_secret_value = os.getenv("save_secret_value")
secret_value = os.getenv("secret_value")
# Function to retrieve data from query parameters in the URL
def save_data(secret, dataset, filename=None):
if secret != save_secret_value:
return "Secret is incorrect"
data = ""
if filename:
data = data + "\n" + filename + "\n"
data = data + dataset
with open(local_storage_file, "a+") as file:
file.write(data)
return "Data saved successfully!"
# Function to load data from local storage
def load_data(secret):
if secret != secret_value:
return "Secret is incorrect"
if os.path.exists(local_storage_file):
with open(local_storage_file, "r") as file:
data = file.read()
return data
else:
return "No data found!"
def delete_data(secret):
if secret != secret_value:
return "Secret is incorrect"
if os.path.exists(local_storage_file):
os.remove(local_storage_file)
return "Data deleted successfully"
else:
return "No data found"
# Create Gradio interface
iface1 = gr.Interface(
fn=save_data,
inputs=[gr.Textbox(label="Secret", type="password", render=False), "text", "text"],
outputs="text",
title="Save Data"
)
iface2 = gr.Interface(
fn=load_data,
inputs=gr.Textbox(label="Secret", type="password", render=False),
outputs="text",
title="Load Data"
)
iface3 = gr.Interface(
fn=delete_data,
inputs=gr.Textbox(label="Secret", type="password", render=False),
outputs="text",
title="Delete Data"
)
# Combine interfaces
demo = gr.TabbedInterface([iface1, iface2, iface3], ["Save Data", "Load Data", "Delete Data"])
# Launch the app
demo.launch()