Shriharsh's picture
Create app.py
637c687 verified
raw
history blame
1.46 kB
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()