test_2024 / app.py
andito's picture
andito HF staff
added file download
48d708b
raw
history blame
1.76 kB
import gradio as gr
from datasets import load_dataset, Dataset, concatenate_datasets
from datetime import datetime
import requests
import os
# 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")
dataset = load_dataset(DATASET_NAME, split="train")
LOCAL_FILE_PATH = "exercise.pdf"
# Function to fetch the exercise file if not already downloaded
def fetch_exercise_file():
if not os.path.exists(LOCAL_FILE_PATH):
response = requests.get(EXERCISE_URL)
with open(LOCAL_FILE_PATH, "wb") as f:
f.write(response.content)
# Function to log download data to the HF Dataset
def log_to_hf_dataset(username):
if not username:
return "Please enter your username to proceed.", None
# 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"],
})
updated_dataset = concatenate_datasets([dataset, new_entry])
updated_dataset.push_to_hub(DATASET_NAME, token=TOKEN)
# 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:
username = gr.Textbox(label="Enter your username", placeholder="Your Hugging Face username")
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=[username], outputs=[output, file])
# Launch the app
demo.launch()