Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Log file where all the uppercase results are saved
|
| 5 |
+
LOG_FILE = "log.txt"
|
| 6 |
+
|
| 7 |
+
def convert_to_uppercase(text):
|
| 8 |
+
"""Convert the input text to uppercase and log the result."""
|
| 9 |
+
result = text.upper()
|
| 10 |
+
# Append the result to the log file with a newline
|
| 11 |
+
with open(LOG_FILE, "a") as f:
|
| 12 |
+
f.write(result + "\n")
|
| 13 |
+
return result
|
| 14 |
+
|
| 15 |
+
def get_log_file():
|
| 16 |
+
"""Ensure the log file exists and return its path."""
|
| 17 |
+
if not os.path.exists(LOG_FILE):
|
| 18 |
+
with open(LOG_FILE, "w") as f:
|
| 19 |
+
f.write("")
|
| 20 |
+
return LOG_FILE
|
| 21 |
+
|
| 22 |
+
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown("## Text Uppercase Converter")
|
| 24 |
+
with gr.Row():
|
| 25 |
+
with gr.Column():
|
| 26 |
+
input_text = gr.Textbox(label="Enter text", placeholder="Type something here...", lines=2)
|
| 27 |
+
output_text = gr.Textbox(label="Converted Text", lines=2)
|
| 28 |
+
convert_button = gr.Button("Submit")
|
| 29 |
+
with gr.Column():
|
| 30 |
+
download_button = gr.Button("Download Log")
|
| 31 |
+
# This File component will output the log file for download
|
| 32 |
+
download_file = gr.File(label="Log File")
|
| 33 |
+
|
| 34 |
+
# On clicking the submit button, convert text to uppercase and display it
|
| 35 |
+
convert_button.click(fn=convert_to_uppercase, inputs=input_text, outputs=output_text)
|
| 36 |
+
|
| 37 |
+
# On clicking the download button, provide the log file for download
|
| 38 |
+
download_button.click(fn=get_log_file, inputs=[], outputs=download_file)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|