import gradio as gr #gr.Interface.load("models/bigscience/bloom").launch() api = gr.Interface.load("models/bigscience/bloom") def change_textbox(choice, textin): if choice == "Expository": return [textin[:-50] + api(textin[-50:]), api(textin[-50:])] elif choice == "Narrative": #return textin[:-50] + api(textin[-50:]) return [textin[:-50] + api(textin[-50:]), api(textin[-50:])] else: #return textin[:-50] + api(textin[-50:]) return [textin[:-50] + api(textin[-50:]), api(textin[-50:])] # Function to list files with .csv and .txt extensions in the current directory def list_files(file_path): import os current_directory = os.getcwd() file_list = [] for filename in os.listdir(current_directory): if filename.endswith(".csv") or filename.endswith(".txt"): file_list.append(filename) if file_list: return "\n".join(file_list) else: return "No .csv or .txt files found in the current directory." # Function to read a file def read_file(file_path): try: with open(file_path, "r") as file: contents = file.read() return f"{contents}" #return f"Contents of {file_path}:\n{contents}" except FileNotFoundError: return "File not found." # Function to delete a file def delete_file(file_path): try: import os os.remove(file_path) return f"{file_path} has been deleted." except FileNotFoundError: return "File not found." # Function to write to a file 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." # Function to append to a 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." with gr.Blocks() as block: fileName = gr.Textbox(label="Filename") fileContent = gr.TextArea(label="File Content") completedMessage = gr.Textbox(label="Completed") radio = gr.Radio( ["Expository", "Narrative", "Opinion"], label="What kind of essay would you like to write?" ) textin = gr.Textbox(lines=2, interactive=True) textin.update(lines=8, visible=True) textout = gr.Textbox(lines=2, interactive=True) textout.update(lines=8, visible=True) radio.change(fn=change_textbox, inputs=[radio, textin], outputs=textout) #radio.change(fn=change_textbox, inputs=[radio, textin], outputs=[textout, fileContent]) listFiles = gr.Button("List CSV and TXT File(s)") readFile = gr.Button("Read File") saveFile = gr.Button("Save File") deleteFile = gr.Button("Delete File") appendFile = gr.Button("Append File") listFiles.click(list_files, inputs=fileName, outputs=fileContent) readFile.click(read_file, inputs=fileName, outputs=fileContent) saveFile.click(write_file, inputs=[fileName, fileContent], outputs=completedMessage) deleteFile.click(delete_file, inputs=fileName, outputs=completedMessage) appendFile.click(append_file, inputs=[fileName, fileContent], outputs=completedMessage ) block.launch()