File size: 2,381 Bytes
f31e402 c71b48d 933a7e0 f31e402 933a7e0 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 c71b48d f31e402 |
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 |
import os
import gradio as gr
def list_files(file_path):
return "\n".join([filename for filename in os.listdir() if filename.endswith((".csv", ".txt"))]) or "๐ No .csv or .txt files found in the current directory."
def read_file(file_path):
try:
with open(file_path, "r") as file:
return file.read()
except FileNotFoundError:
return "โ File not found."
def delete_file(file_path):
try:
os.remove(file_path)
return f"๐๏ธ {file_path} has been deleted."
except FileNotFoundError:
return "โ File not found."
def write_file(file_path, content):
try:
with open(file_path, "w") as file:
file.write(content)
return f"๐พ Successfully written to {file_path}."
except:
return "โ Error occurred while writing to file."
def append_file(file_path, content):
try:
with open(file_path, "a") as file:
file.write(content)
return f"โ๏ธ Successfully appended to {file_path}."
except:
return "โ Error occurred while appending to file."
def add_emojis(interface):
interface.inputs[0].label = f"๐ {interface.inputs[0].label}"
interface.title = f"๐ {interface.title}"
interface.description = f"๐ {interface.description}"
interface.outputs.label = f"๐ {interface.outputs.label}"
return interface
demo = add_emojis(gr.Interface(
fn=list_files,
inputs=[gr.inputs.Textbox(label="List CSV and TXT File(s)")],
outputs="text",
layout="vertical"
))
demo2 = add_emojis(gr.Interface(
fn=read_file,
inputs=[
gr.inputs.Textbox(label="Read File"),
gr.inputs.TextArea(label="โ๏ธ File Content")
],
outputs="text",
layout="vertical"
))
demo3 = add_emojis(gr.Interface(
fn=write_file,
inputs=[
gr.inputs.Textbox(label="Save File"),
gr.inputs.TextArea(label="โ๏ธ File Content")
],
outputs="text",
layout="vertical"
))
demo4 = add_emojis(gr.Interface(
fn=delete_file,
inputs=[gr.inputs.Textbox(label="Delete File")],
outputs="text",
layout="vertical"
))
demo5 = add_emojis(gr.Interface(
fn=append_file,
inputs=[
gr.inputs.Textbox(label="Append File"),
gr.inputs.TextArea(label="โ๏ธ File Content")
],
outputs="text",
layout="vertical"
))
demo.launch()
|