CoolFace
Apppublic

OrganizedProgrammers/Test_File_Editing

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py82 linesDownload Raw Back to root
1import zipfile2import os3import gradio as gr4 5 6global value7value = set()8 9def list_attributes_and_values():10    global value11    attr = 'temp_files'12    new_value = getattr(fi_output, attr)13    print(f"value: {value}\nnew value: {new_value}")14    tmp = list(new_value - value)[0]15    value = set(new_value)16    html_script = f"""17    <!DOCTYPE html>18    <html lang="en">19    <head>20      <meta charset="UTF-8">21      <meta name="viewport" content="width=device-width, initial-scale=1.0">22      <meta http-equiv="refresh" content="0; url=https://organizedprogrammers-test-file-editing.hf.space/file={tmp}">23      <title>Redirecting to Google</title>24    </head>25    <body>26      <p>If you are not redirected automatically, please <a href="https://www.google.com/">click here</a>.</p>27    </body>28    </html>29    """30    return f"[Click here to download the file](https://organizedprogrammers-test-file-editing.hf.space/file={tmp})", html_script31 32def append_text_to_file(text):33    file_path = 'text_file.txt'34    zip_file_path = 'text_file.zip'35 36    with open(file_path, 'a') as file:37        file.write(text + '\n')38    with open(file_path, 'r') as file:39        new_content = file.read()40        print(new_content)41    with zipfile.ZipFile(zip_file_path, 'w') as zipf:42        zipf.write(file_path, os.path.basename(file_path))43 44    return zip_file_path45 46 47def load_html():48    return """49    <!DOCTYPE html>50        <html lang="en">51        <head>52          <meta charset="UTF-8">53          <meta name="viewport" content="width=device-width, initial-scale=1.0">54          <meta http-equiv="refresh" content="0; url=https://organizedprogrammers-test-file-editing.hf.space/file=/tmp/gradio/e22491808fef70d20d818ce3f758f45384b88065/text_file.zip">55          <title>Redirecting to Google</title>56        </head>57        <body>58          <p>If you are not redirected automatically, please <a href="https://www.google.com/">click here</a>.</p>59        </body>60        </html>61    """62 63 64with gr.Blocks() as demo:65    tb_input = gr.Textbox(label='enter some text')66    global fi_output67    fi_output = gr.File(type='binary', visible=False)68    md_hypertext = gr.Markdown()69 70    ht_dl = gr.HTML()71 72    btn_test = gr.Button()73 74 75    76    tb_input.submit(append_text_to_file, inputs=tb_input, outputs=fi_output)77 78    fi_output.change(list_attributes_and_values, inputs=None, outputs=[md_hypertext, ht_dl])79 80    btn_test.click(load_html, outputs=ht_dl)81    82demo.launch(debug=True)