File size: 1,460 Bytes
637c687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os

# Log file where all the uppercase results are saved
LOG_FILE = "log.txt"

def convert_to_uppercase(text):
    """Convert the input text to uppercase and log the result."""
    result = text.upper()
    # Append the result to the log file with a newline
    with open(LOG_FILE, "a") as f:
        f.write(result + "\n")
    return result

def get_log_file():
    """Ensure the log file exists and return its path."""
    if not os.path.exists(LOG_FILE):
        with open(LOG_FILE, "w") as f:
            f.write("")
    return LOG_FILE

with gr.Blocks() as demo:
    gr.Markdown("## Text Uppercase Converter")
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(label="Enter text", placeholder="Type something here...", lines=2)
            output_text = gr.Textbox(label="Converted Text", lines=2)
            convert_button = gr.Button("Submit")
        with gr.Column():
            download_button = gr.Button("Download Log")
            # This File component will output the log file for download
            download_file = gr.File(label="Log File")
    
    # On clicking the submit button, convert text to uppercase and display it
    convert_button.click(fn=convert_to_uppercase, inputs=input_text, outputs=output_text)
    
    # On clicking the download button, provide the log file for download
    download_button.click(fn=get_log_file, inputs=[], outputs=download_file)

demo.launch()