andito HF staff commited on
Commit
e323368
·
1 Parent(s): 48d708b

added security

Browse files
Files changed (1) hide show
  1. app.py +14 -14
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
- if not os.path.exists(LOCAL_FILE_PATH):
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(username):
23
- if not username:
24
- return "Please enter your username to proceed.", None
 
 
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.", LOCAL_FILE_PATH # Replace with your file path
40
 
41
  # Gradio interface
42
  with gr.Blocks() as demo:
43
- username = gr.Textbox(label="Enter your username", placeholder="Your Hugging Face username")
 
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=[username], outputs=[output, file])
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()