CoolFace
Apppublic

RealTimeLiveAIForHealth/DeepRL-LLM-FeedbackFileOperations

sourceHugging Facemitupdated 4y agoView on Hugging Face
0likes
app.py74 linesDownload Raw Back to root
1import gradio as gr2 3 4# Function to list files with .csv and .txt extensions in the current directory5def list_files(file_path):6    import os7    current_directory = os.getcwd()8    file_list = []9    for filename in os.listdir(current_directory):10        if filename.endswith(".csv") or filename.endswith(".txt"):11            file_list.append(filename)12    if file_list:13        return "\n".join(file_list)14    else:15        return "No .csv or .txt files found in the current directory."16 17# Function to read a file18def read_file(file_path):19    try:20        with open(file_path, "r") as file:21            contents = file.read()22            return f"{contents}"23            #return f"Contents of {file_path}:\n{contents}"24    except FileNotFoundError:25        return "File not found."26 27# Function to delete a file28def delete_file(file_path):29    try:30        import os31        os.remove(file_path)32        return f"{file_path} has been deleted."33    except FileNotFoundError:34        return "File not found."35 36# Function to write to a file37def write_file(file_path, content):38    try:39        with open(file_path, "w") as file:40            file.write(content)41        return f"Successfully written to {file_path}."42    except:43        return "Error occurred while writing to file."44 45# Function to append to a file46def append_file(file_path, content):47    try:48        with open(file_path, "a") as file:49            file.write(content)50        return f"Successfully appended to {file_path}."51    except:52        return "Error occurred while appending to file."53 54demo = gr.Blocks()55with demo:56    fileName = gr.Textbox(label="Filename")57    fileContent = gr.TextArea(label="File Content")58    completedMessage = gr.Textbox(label="Completed")59    60    label = gr.Label()61 62    listFiles = gr.Button("List CSV and TXT File(s)")63    readFile = gr.Button("Read File")64    saveFile = gr.Button("Save File")65    deleteFile = gr.Button("Delete File")66    appendFile = gr.Button("Append File")67    68    listFiles.click(list_files, inputs=fileName, outputs=fileContent)69    readFile.click(read_file, inputs=fileName, outputs=fileContent)70    saveFile.click(write_file, inputs=[fileName, fileContent], outputs=completedMessage)71    deleteFile.click(delete_file, inputs=fileName, outputs=completedMessage)72    appendFile.click(append_file, inputs=[fileName, fileContent], outputs=completedMessage )73    74demo.launch()