File size: 2,470 Bytes
1e4f033 2b640c5 bb65e5f 53d33b0 8b4aeb9 da58160 53d33b0 c2b39ba 8b4aeb9 aacd0d1 e8fc88e 17ddbbe 21d4cc6 dc7f01a 835a781 bb65e5f ee2d389 2ed5320 ee2d389 14990c7 ee2d389 bb65e5f 792b6b9 b6c2499 792b6b9 bb65e5f f22a924 14990c7 792b6b9 fc2cd82 c2b39ba 2cf5d73 792b6b9 bb65e5f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import zipfile
import os
import gradio as gr
global value
value = set()
def list_attributes_and_values():
global value
attr = 'temp_files'
new_value = getattr(fi_output, attr)
print(f"value: {value}\nnew value: {new_value}")
tmp = list(new_value - value)[0]
value = set(new_value)
html_script = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="0; url=https://organizedprogrammers-test-file-editing.hf.space/file={tmp}">
<title>Redirecting to Google</title>
</head>
<body>
<p>If you are not redirected automatically, please <a href="https://www.google.com/">click here</a>.</p>
</body>
</html>
"""
return f"[Click here to download the file](https://organizedprogrammers-test-file-editing.hf.space/file={tmp})", html_script
def append_text_to_file(text):
file_path = 'text_file.txt'
zip_file_path = 'text_file.zip'
with open(file_path, 'a') as file:
file.write(text + '\n')
with open(file_path, 'r') as file:
new_content = file.read()
print(new_content)
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
zipf.write(file_path, os.path.basename(file_path))
return zip_file_path
def load_html():
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="0; url=https://organizedprogrammers-test-file-editing.hf.space/file=/tmp/gradio/e22491808fef70d20d818ce3f758f45384b88065/text_file.zip">
<title>Redirecting to Google</title>
</head>
<body>
<p>If you are not redirected automatically, please <a href="https://www.google.com/">click here</a>.</p>
</body>
</html>
"""
with gr.Blocks() as demo:
tb_input = gr.Textbox(label='enter some text')
global fi_output
fi_output = gr.File(type='binary', visible=False)
md_hypertext = gr.Markdown()
ht_dl = gr.HTML()
btn_test = gr.Button()
tb_input.submit(append_text_to_file, inputs=tb_input, outputs=fi_output)
fi_output.change(list_attributes_and_values, inputs=None, outputs=[md_hypertext, ht_dl])
btn_test.click(load_html, outputs=ht_dl)
demo.launch(debug=True) |