Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,88 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
# Function to list files with .csv and .txt extensions in the current directory
|
5 |
def list_files(file_path):
|
6 |
-
|
7 |
-
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 file
|
18 |
def read_file(file_path):
|
19 |
try:
|
20 |
with open(file_path, "r") as file:
|
21 |
-
|
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 file
|
28 |
def delete_file(file_path):
|
29 |
try:
|
30 |
-
import os
|
31 |
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 file
|
37 |
def 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 file
|
46 |
def 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 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
|
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 |
-
|
74 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
|
|
|
|
|
4 |
def list_files(file_path):
|
5 |
+
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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
|
|
7 |
def read_file(file_path):
|
8 |
try:
|
9 |
with open(file_path, "r") as file:
|
10 |
+
return file.read()
|
|
|
|
|
11 |
except FileNotFoundError:
|
12 |
+
return "โ File not found."
|
13 |
|
|
|
14 |
def delete_file(file_path):
|
15 |
try:
|
|
|
16 |
os.remove(file_path)
|
17 |
+
return f"๐๏ธ {file_path} has been deleted."
|
18 |
except FileNotFoundError:
|
19 |
+
return "โ File not found."
|
20 |
|
|
|
21 |
def write_file(file_path, content):
|
22 |
try:
|
23 |
with open(file_path, "w") as file:
|
24 |
file.write(content)
|
25 |
+
return f"๐พ Successfully written to {file_path}."
|
26 |
except:
|
27 |
+
return "โ Error occurred while writing to file."
|
28 |
|
|
|
29 |
def append_file(file_path, content):
|
30 |
try:
|
31 |
with open(file_path, "a") as file:
|
32 |
file.write(content)
|
33 |
+
return f"โ๏ธ Successfully appended to {file_path}."
|
34 |
except:
|
35 |
+
return "โ Error occurred while appending to file."
|
36 |
+
|
37 |
+
def add_emojis(interface):
|
38 |
+
interface.inputs[0].label = f"๐ {interface.inputs[0].label}"
|
39 |
+
interface.title = f"๐ {interface.title}"
|
40 |
+
interface.description = f"๐ {interface.description}"
|
41 |
+
interface.outputs.label = f"๐ {interface.outputs.label}"
|
42 |
+
return interface
|
43 |
+
|
44 |
+
demo = add_emojis(gr.Interface(
|
45 |
+
fn=list_files,
|
46 |
+
inputs=[gr.inputs.Textbox(label="List CSV and TXT File(s)")],
|
47 |
+
outputs="text",
|
48 |
+
layout="vertical"
|
49 |
+
))
|
50 |
+
|
51 |
+
demo2 = add_emojis(gr.Interface(
|
52 |
+
fn=read_file,
|
53 |
+
inputs=[
|
54 |
+
gr.inputs.Textbox(label="Read File"),
|
55 |
+
gr.inputs.TextArea(label="โ๏ธ File Content")
|
56 |
+
],
|
57 |
+
outputs="text",
|
58 |
+
layout="vertical"
|
59 |
+
))
|
60 |
+
|
61 |
+
demo3 = add_emojis(gr.Interface(
|
62 |
+
fn=write_file,
|
63 |
+
inputs=[
|
64 |
+
gr.inputs.Textbox(label="Save File"),
|
65 |
+
gr.inputs.TextArea(label="โ๏ธ File Content")
|
66 |
+
],
|
67 |
+
outputs="text",
|
68 |
+
layout="vertical"
|
69 |
+
))
|
70 |
+
|
71 |
+
demo4 = add_emojis(gr.Interface(
|
72 |
+
fn=delete_file,
|
73 |
+
inputs=[gr.inputs.Textbox(label="Delete File")],
|
74 |
+
outputs="text",
|
75 |
+
layout="vertical"
|
76 |
+
))
|
77 |
|
78 |
+
demo5 = add_emojis(gr.Interface(
|
79 |
+
fn=append_file,
|
80 |
+
inputs=[
|
81 |
+
gr.inputs.Textbox(label="Append File"),
|
82 |
+
gr.inputs.TextArea(label="โ๏ธ File Content")
|
83 |
+
],
|
84 |
+
outputs="text",
|
85 |
+
layout="vertical"
|
86 |
+
))
|
87 |
|
88 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|