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