added security
Browse files
app.py
CHANGED
@@ -1,27 +1,26 @@
|
|
1 |
import gradio as gr
|
2 |
from datasets import load_dataset, Dataset, concatenate_datasets
|
3 |
from datetime import datetime
|
4 |
-
import requests
|
5 |
import os
|
|
|
|
|
6 |
|
7 |
# Load your private Hugging Face dataset
|
8 |
DATASET_NAME = "andito/technical_interview_internship_2025"
|
9 |
TOKEN = os.environ.get("HF_TOKEN")
|
10 |
EXERCISE_URL = os.environ.get("EXERCISE")
|
11 |
dataset = load_dataset(DATASET_NAME, split="train")
|
12 |
-
LOCAL_FILE_PATH = "exercise.pdf"
|
13 |
|
14 |
# Function to fetch the exercise file if not already downloaded
|
15 |
def fetch_exercise_file():
|
16 |
-
|
17 |
-
response = requests.get(EXERCISE_URL)
|
18 |
-
with open(LOCAL_FILE_PATH, "wb") as f:
|
19 |
-
f.write(response.content)
|
20 |
|
21 |
# Function to log download data to the HF Dataset
|
22 |
-
def log_to_hf_dataset(
|
23 |
-
if
|
24 |
-
return "
|
|
|
|
|
25 |
|
26 |
# Get current timestamp
|
27 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
@@ -34,19 +33,20 @@ def log_to_hf_dataset(username):
|
|
34 |
})
|
35 |
updated_dataset = concatenate_datasets([dataset, new_entry])
|
36 |
updated_dataset.push_to_hub(DATASET_NAME, token=TOKEN)
|
|
|
37 |
|
38 |
# Provide file for download
|
39 |
-
return "Thank you! Your download is ready.",
|
40 |
|
41 |
# Gradio interface
|
42 |
with gr.Blocks() as demo:
|
43 |
-
|
|
|
44 |
download_button = gr.Button("Download Exercise")
|
45 |
output = gr.Text()
|
46 |
file = gr.File(label="Download your exercise file")
|
47 |
|
48 |
-
download_button.click(log_to_hf_dataset, inputs=[
|
49 |
|
50 |
# Launch the app
|
51 |
-
demo.launch()
|
52 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from datasets import load_dataset, Dataset, concatenate_datasets
|
3 |
from datetime import datetime
|
|
|
4 |
import os
|
5 |
+
from huggingface_hub import hf_hub_download, whoami
|
6 |
+
from huggingface_hub import HfApi, ModelCard, whoami
|
7 |
|
8 |
# Load your private Hugging Face dataset
|
9 |
DATASET_NAME = "andito/technical_interview_internship_2025"
|
10 |
TOKEN = os.environ.get("HF_TOKEN")
|
11 |
EXERCISE_URL = os.environ.get("EXERCISE")
|
12 |
dataset = load_dataset(DATASET_NAME, split="train")
|
|
|
13 |
|
14 |
# Function to fetch the exercise file if not already downloaded
|
15 |
def fetch_exercise_file():
|
16 |
+
return hf_hub_download(repo_id=DATASET_NAME, filename=EXERCISE_URL, repo_type="dataset", local_dir=".")
|
|
|
|
|
|
|
17 |
|
18 |
# Function to log download data to the HF Dataset
|
19 |
+
def log_to_hf_dataset(oauth_token: gr.OAuthToken | None):
|
20 |
+
if oauth_token is None:
|
21 |
+
return "You have to be logged in.", "README.md"
|
22 |
+
|
23 |
+
username = whoami(token=oauth_token.token)["name"]
|
24 |
|
25 |
# Get current timestamp
|
26 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
33 |
})
|
34 |
updated_dataset = concatenate_datasets([dataset, new_entry])
|
35 |
updated_dataset.push_to_hub(DATASET_NAME, token=TOKEN)
|
36 |
+
local_file_path = fetch_exercise_file()
|
37 |
|
38 |
# Provide file for download
|
39 |
+
return "Thank you! Your download is ready.", local_file_path # Replace with your file path
|
40 |
|
41 |
# Gradio interface
|
42 |
with gr.Blocks() as demo:
|
43 |
+
gr.Markdown("You must be logged in to use download the exercise.")
|
44 |
+
gr.LoginButton(min_width=250)
|
45 |
download_button = gr.Button("Download Exercise")
|
46 |
output = gr.Text()
|
47 |
file = gr.File(label="Download your exercise file")
|
48 |
|
49 |
+
download_button.click(log_to_hf_dataset, inputs=[], outputs=[output, file])
|
50 |
|
51 |
# Launch the app
|
52 |
+
demo.launch()
|
|