Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,34 +13,37 @@ def greet_and_save(name):
|
|
| 13 |
output_text = f"Hello, {name}!"
|
| 14 |
|
| 15 |
# Tentukan direktori file sementara
|
| 16 |
-
temp_dir = tempfile.gettempdir() #
|
| 17 |
temp_filename = "greeting_output.txt" # Nama file tetap
|
| 18 |
-
temp_path = os.path.join(temp_dir, temp_filename) #
|
| 19 |
|
| 20 |
# Simpan teks ke file sementara
|
| 21 |
with open(temp_path, "w", encoding="utf-8") as temp_file:
|
| 22 |
temp_file.write(output_text)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Konfigurasi UI Gradio
|
| 28 |
-
with gr.Blocks() as
|
| 29 |
-
gr.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
message_output = gr.Text(label="Pesan")
|
| 41 |
-
download_button = gr.File(label="Download File") # Tombol download otomatis
|
| 42 |
-
|
| 43 |
-
submit_button.click(greet_and_save, inputs=name_input, outputs=[message_output, download_button])
|
| 44 |
|
| 45 |
# Jalankan aplikasi
|
| 46 |
-
|
|
|
|
| 13 |
output_text = f"Hello, {name}!"
|
| 14 |
|
| 15 |
# Tentukan direktori file sementara
|
| 16 |
+
temp_dir = tempfile.gettempdir() # Ambil folder temporer sistem
|
| 17 |
temp_filename = "greeting_output.txt" # Nama file tetap
|
| 18 |
+
temp_path = os.path.join(temp_dir, temp_filename) # Gabungkan path
|
| 19 |
|
| 20 |
# Simpan teks ke file sementara
|
| 21 |
with open(temp_path, "w", encoding="utf-8") as temp_file:
|
| 22 |
temp_file.write(output_text)
|
| 23 |
|
| 24 |
+
# Simpan jalur file ke variabel global untuk diakses oleh fungsi unduh
|
| 25 |
+
global saved_file_path
|
| 26 |
+
saved_file_path = temp_path
|
| 27 |
+
|
| 28 |
+
# Kembalikan teks sapaan
|
| 29 |
+
return output_text
|
| 30 |
+
|
| 31 |
+
def download_file():
|
| 32 |
+
"""Fungsi untuk mengembalikan file yang telah disimpan."""
|
| 33 |
+
return saved_file_path
|
| 34 |
|
| 35 |
# Konfigurasi UI Gradio
|
| 36 |
+
with gr.Blocks() as iface:
|
| 37 |
+
name_input = gr.Textbox(
|
| 38 |
+
placeholder="Masukkan nama Anda",
|
| 39 |
+
label="Nama",
|
| 40 |
+
lines=1
|
| 41 |
+
)
|
| 42 |
+
greet_output = gr.Text(label="Pesan")
|
| 43 |
+
download_button = gr.Button("Unduh File")
|
| 44 |
+
|
| 45 |
+
name_input.submit(greet_and_save, inputs=name_input, outputs=greet_output)
|
| 46 |
+
download_button.click(download_file, inputs=None, outputs=gr.File())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Jalankan aplikasi
|
| 49 |
+
iface.launch()
|