File size: 1,989 Bytes
3180ac9
 
 
 
e323368
3180ac9
 
 
 
48d708b
4591309
48d708b
 
 
e323368
3180ac9
 
e323368
 
 
 
 
4591309
 
3180ac9
 
 
 
 
 
 
 
3e0daf1
3180ac9
3c25ef6
3180ac9
 
e323368
3180ac9
 
e323368
3180ac9
 
 
e323368
 
3180ac9
 
48d708b
3180ac9
e323368
3180ac9
 
e323368
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
from datasets import load_dataset, Dataset, concatenate_datasets
from datetime import datetime
import os
from huggingface_hub import hf_hub_download, whoami

# Load your private Hugging Face dataset
DATASET_NAME = "andito/technical_interview_internship_2025"
TOKEN = os.environ.get("HF_TOKEN")
EXERCISE_URL = os.environ.get("EXERCISE")
whitelist = os.environ.get("WHITELIST").split(",")

# Function to fetch the exercise file if not already downloaded
def fetch_exercise_file():
    return hf_hub_download(repo_id=DATASET_NAME, filename=EXERCISE_URL, repo_type="dataset", local_dir=".")

# Function to log download data to the HF Dataset
def log_to_hf_dataset(oauth_token: gr.OAuthToken | None):
    if oauth_token is None:
        return "You have to be logged in.", "README.md"

    username = whoami(token=oauth_token.token)["name"]
    if username not in whitelist:
        return "You are not authorized to download the exercise.", "README.md"

    # Get current timestamp
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    # Append new data to the dataset
    new_entry = Dataset.from_dict({
        "username": [username],
        "timestamp": [timestamp],
        "ip_address": ["egg"],
    })
    dataset = load_dataset(DATASET_NAME, split="train")
    updated_dataset = concatenate_datasets([dataset, new_entry])
    updated_dataset.push_to_hub(DATASET_NAME, token=TOKEN)
    local_file_path = fetch_exercise_file()

    # Provide file for download
    return "Thank you! Your download is ready.", local_file_path  # Replace with your file path

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("You must be logged in to use download the exercise.")
    gr.LoginButton(min_width=250)
    download_button = gr.Button("Download Exercise")
    output = gr.Text()
    file = gr.File(label="Download your exercise file")
    
    download_button.click(log_to_hf_dataset, inputs=[], outputs=[output, file])

# Launch the app
demo.launch()