|
|
|
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.label = f"π {interface.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.Textbox(label="List CSV and TXT File(s)"), |
|
outputs="text", |
|
layout="vertical" |
|
)) |
|
|
|
demo2 = add_emojis(gr.Interface( |
|
fn=read_file, |
|
inputs=[ |
|
gr.Textbox(label="Read File"), |
|
gr.TextArea(label="βοΈ File Content") |
|
], |
|
outputs="text", |
|
layout="vertical" |
|
)) |
|
|
|
demo3 = add_emojis(gr.Interface( |
|
fn=write_file, |
|
inputs=[ |
|
gr.Textbox(label="Save File"), |
|
gr.TextArea(label="βοΈ File Content") |
|
], |
|
outputs="text", |
|
layout="vertical" |
|
)) |
|
|
|
demo4 = add_emojis(gr.Interface( |
|
fn=delete_file, |
|
inputs=gr.Textbox(label="Delete File"), |
|
outputs="text", |
|
layout="vertical" |
|
)) |
|
|
|
demo5 = add_emojis(gr.Interface( |
|
fn=append_file, |
|
inputs=[ |
|
gr.Textbox(label="Append File"), |
|
gr.TextArea(label="βοΈ File Content") |
|
], |
|
outputs="text", |
|
layout="vertical" |
|
)) |
|
|
|
demo.launch() |
|
|