Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def greet(name):
|
|
|
|
| 6 |
return "Hello " + name + "!!"
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
with open(local_path, "w") as f:
|
| 14 |
-
f.write("This file was saved inside the Hugging Face Space AND uploaded to the repo!")
|
| 15 |
-
|
| 16 |
-
# 2. Upload to the Space repo so it persists
|
| 17 |
-
# Replace these with your actual values
|
| 18 |
-
repo_id = "aparke21/simple_gradio_app" # e.g., "aparke21/test-space"
|
| 19 |
-
repo_type = "space"
|
| 20 |
-
|
| 21 |
-
token = os.getenv("HF_TOKEN")
|
| 22 |
-
if token is None:
|
| 23 |
-
# Helpful message if the secret isn't set
|
| 24 |
-
print("HF_TOKEN environment variable is not set. Skipping upload.")
|
| 25 |
-
return local_path
|
| 26 |
-
|
| 27 |
-
api = HfApi(token=token)
|
| 28 |
-
|
| 29 |
-
# Path inside the repo where the file will be stored
|
| 30 |
-
remote_path = "saved/test_output.txt"
|
| 31 |
-
|
| 32 |
-
api.upload_file(
|
| 33 |
-
path_or_fileobj=local_path,
|
| 34 |
-
repo_id=repo_id,
|
| 35 |
-
repo_type=repo_type,
|
| 36 |
-
path_in_repo=remote_path,
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
print(f"Uploaded {local_path} to {repo_id}/{remote_path}")
|
| 40 |
-
return local_path # still return local path so user can download it
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
-
gr.Markdown("## Greeting
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
name_input = gr.Textbox(label="Enter your name")
|
|
@@ -49,10 +29,10 @@ with gr.Blocks() as demo:
|
|
| 49 |
|
| 50 |
greet_button.click(fn=greet, inputs=name_input, outputs=greet_output)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
file_output = gr.File(label="Download saved file")
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
def write_log(message):
|
| 6 |
+
os.makedirs("/data", exist_ok=True)
|
| 7 |
+
log_path = "/data/logs.txt"
|
| 8 |
+
timestamp = datetime.utcnow().isoformat()
|
| 9 |
+
with open(log_path, "a") as f:
|
| 10 |
+
f.write(f"{timestamp} - {message}\n")
|
| 11 |
|
| 12 |
def greet(name):
|
| 13 |
+
write_log(f"Greeted: {name}")
|
| 14 |
return "Hello " + name + "!!"
|
| 15 |
|
| 16 |
+
def download_logs():
|
| 17 |
+
log_path = "/data/logs.txt"
|
| 18 |
+
if os.path.exists(log_path):
|
| 19 |
+
return log_path
|
| 20 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown("## Greeting App with Persistent Logs (/data)")
|
| 24 |
|
| 25 |
with gr.Row():
|
| 26 |
name_input = gr.Textbox(label="Enter your name")
|
|
|
|
| 29 |
|
| 30 |
greet_button.click(fn=greet, inputs=name_input, outputs=greet_output)
|
| 31 |
|
| 32 |
+
download_button = gr.Button("Download Logs")
|
| 33 |
+
log_file_output = gr.File(label="Logs File")
|
|
|
|
| 34 |
|
| 35 |
+
download_button.click(fn=download_logs, inputs=None, outputs=log_file_output)
|
| 36 |
|
| 37 |
demo.launch()
|
| 38 |
+
|