Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
# Function to read a file
|
4 |
+
def read_file(file_path):
|
5 |
+
try:
|
6 |
+
with open(file_path, "r") as file:
|
7 |
+
contents = file.read()
|
8 |
+
return f"{contents}"
|
9 |
+
#return f"Contents of {file_path}:\n{contents}"
|
10 |
+
except FileNotFoundError:
|
11 |
+
return "File not found."
|
12 |
+
|
13 |
+
# Function to delete a file
|
14 |
+
def delete_file(file_path):
|
15 |
+
try:
|
16 |
+
import os
|
17 |
+
os.remove(file_path)
|
18 |
+
return f"{file_path} has been deleted."
|
19 |
+
except FileNotFoundError:
|
20 |
+
return "File not found."
|
21 |
+
|
22 |
+
# Function to write to a file
|
23 |
+
def write_file(file_path, content):
|
24 |
+
try:
|
25 |
+
with open(file_path, "w") as file:
|
26 |
+
file.write(content)
|
27 |
+
return f"Successfully written to {file_path}."
|
28 |
+
except:
|
29 |
+
return "Error occurred while writing to file."
|
30 |
+
|
31 |
+
# Function to append to a file
|
32 |
+
def append_file(file_path, content):
|
33 |
+
try:
|
34 |
+
with open(file_path, "a") as file:
|
35 |
+
file.write(content)
|
36 |
+
return f"Successfully appended to {file_path}."
|
37 |
+
except:
|
38 |
+
return "Error occurred while appending to file."
|
39 |
+
|
40 |
+
demo = gr.Blocks()
|
41 |
+
with demo:
|
42 |
+
fileName = gr.Textbox(label="Filename")
|
43 |
+
fileContent = gr.TextArea(label="File Content")
|
44 |
+
completedMessage = gr.Textbox(label="Completed")
|
45 |
+
|
46 |
+
label = gr.Label()
|
47 |
+
|
48 |
+
readFile = gr.Button("Read File")
|
49 |
+
saveFile = gr.Button("Save File")
|
50 |
+
deleteFile = gr.Button("Delete File")
|
51 |
+
appendFile = gr.Button("Append File")
|
52 |
+
|
53 |
+
readFile.click(read_file, inputs=fileName, outputs=fileContent)
|
54 |
+
saveFile.click(write_file, inputs=[fileName, fileContent], outputs=completedMessage)
|
55 |
+
deleteFile.click(delete_file, inputs=fileName, outputs=completedMessage)
|
56 |
+
appendFile.click(append_file, inputs=[fileName, fileContent], outputs=completedMessage )
|
57 |
+
|
58 |
+
demo.launch()
|