FajarHidaa commited on
Commit
049d2ef
·
verified ·
1 Parent(s): 60190a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
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() # Folder sementara sistem
17
  temp_filename = "greeting_output.txt" # Nama file tetap
18
- temp_path = os.path.join(temp_dir, temp_filename) # Path lengkap
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
- # Kembalikan teks sapaan dan file untuk diunduh
25
- return output_text, temp_path
 
 
 
 
 
 
 
 
26
 
27
  # Konfigurasi UI Gradio
28
- with gr.Blocks() as app:
29
- gr.Markdown("# Aplikasi Greet dengan Tombol Download")
30
- gr.Markdown("Masukkan nama Anda, dapatkan sapaan, dan unduh hasilnya dalam file sementara.")
31
-
32
- with gr.Row():
33
- name_input = gr.Textbox(
34
- placeholder="Masukkan nama Anda",
35
- label="Nama",
36
- lines=1
37
- )
38
- submit_button = gr.Button("Submit")
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
- app.launch()
 
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()