Spaces:
Runtime error
Runtime error
Commit
·
dd2079c
1
Parent(s):
86629e2
chore: Add dataset storage and CommitScheduler for dataset uploads
Browse files
app.py
CHANGED
|
@@ -3,12 +3,13 @@ import json
|
|
| 3 |
from datetime import datetime
|
| 4 |
from theme import TufteInspired
|
| 5 |
import uuid
|
| 6 |
-
from huggingface_hub import InferenceClient
|
| 7 |
from openai import OpenAI
|
| 8 |
from huggingface_hub import get_token, login
|
| 9 |
from prompts import detailed_genre_description_prompt, basic_prompt
|
| 10 |
import random
|
| 11 |
import os
|
|
|
|
| 12 |
|
| 13 |
# Ensure you're logged in to Hugging Face
|
| 14 |
login(get_token())
|
|
@@ -21,6 +22,55 @@ MODELS = [
|
|
| 21 |
|
| 22 |
CHOSEN_MODEL = None
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def get_random_model():
|
| 26 |
global CHOSEN_MODEL
|
|
@@ -84,8 +134,9 @@ def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *
|
|
| 84 |
"user_id": user_id,
|
| 85 |
"model": CHOSEN_MODEL,
|
| 86 |
}
|
| 87 |
-
with
|
| 88 |
-
|
|
|
|
| 89 |
gr.Info("Thank you for voting!")
|
| 90 |
return f"Logged: {vote} by user {user_id}"
|
| 91 |
|
|
@@ -98,7 +149,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
| 98 |
gr.Markdown("<h1 style='text-align: center;'>Would you read this book?</h1>")
|
| 99 |
gr.Markdown(
|
| 100 |
"""<p style='text-align: center;'>Looking for your next summer read?
|
| 101 |
-
Would you read a book based on this LLM generated blurb? <br> Your vote will be added to <a href="https://
|
| 102 |
)
|
| 103 |
|
| 104 |
# Add the login button
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
from theme import TufteInspired
|
| 5 |
import uuid
|
| 6 |
+
from huggingface_hub import InferenceClient, CommitScheduler, hf_hub_download
|
| 7 |
from openai import OpenAI
|
| 8 |
from huggingface_hub import get_token, login
|
| 9 |
from prompts import detailed_genre_description_prompt, basic_prompt
|
| 10 |
import random
|
| 11 |
import os
|
| 12 |
+
from pathlib import Path
|
| 13 |
|
| 14 |
# Ensure you're logged in to Hugging Face
|
| 15 |
login(get_token())
|
|
|
|
| 22 |
|
| 23 |
CHOSEN_MODEL = None
|
| 24 |
|
| 25 |
+
# Set up dataset storage
|
| 26 |
+
dataset_folder = Path("dataset")
|
| 27 |
+
dataset_folder.mkdir(exist_ok=True)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Function to get the latest dataset file
|
| 31 |
+
def get_latest_dataset_file():
|
| 32 |
+
files = list(dataset_folder.glob("data_*.jsonl"))
|
| 33 |
+
return max(files, key=os.path.getctime) if files else None
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Check for existing dataset and create or append to it
|
| 37 |
+
latest_file = get_latest_dataset_file()
|
| 38 |
+
if latest_file:
|
| 39 |
+
dataset_file = latest_file
|
| 40 |
+
print(f"Appending to existing dataset file: {dataset_file}")
|
| 41 |
+
else:
|
| 42 |
+
dataset_file = dataset_folder / f"data_{uuid.uuid4()}.jsonl"
|
| 43 |
+
print(f"Creating new dataset file: {dataset_file}")
|
| 44 |
+
|
| 45 |
+
# Set up CommitScheduler for dataset uploads
|
| 46 |
+
repo_id = "your-username/your-dataset-repo" # Replace with your desired dataset repo
|
| 47 |
+
scheduler = CommitScheduler(
|
| 48 |
+
repo_id=repo_id,
|
| 49 |
+
repo_type="dataset",
|
| 50 |
+
folder_path=dataset_folder,
|
| 51 |
+
path_in_repo="data",
|
| 52 |
+
every=5, # Upload every 5 minutes
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Function to download existing dataset files
|
| 57 |
+
def download_existing_dataset():
|
| 58 |
+
try:
|
| 59 |
+
files = hf_hub_download(
|
| 60 |
+
repo_id=repo_id, filename="data", repo_type="dataset", recursive=True
|
| 61 |
+
)
|
| 62 |
+
for file in Path(files).glob("*.jsonl"):
|
| 63 |
+
dest_file = dataset_folder / file.name
|
| 64 |
+
if not dest_file.exists():
|
| 65 |
+
dest_file.write_bytes(file.read_bytes())
|
| 66 |
+
print(f"Downloaded existing dataset file: {dest_file}")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error downloading existing dataset: {e}")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# Download existing dataset files at startup
|
| 72 |
+
download_existing_dataset()
|
| 73 |
+
|
| 74 |
|
| 75 |
def get_random_model():
|
| 76 |
global CHOSEN_MODEL
|
|
|
|
| 134 |
"user_id": user_id,
|
| 135 |
"model": CHOSEN_MODEL,
|
| 136 |
}
|
| 137 |
+
with scheduler.lock:
|
| 138 |
+
with dataset_file.open("a") as f:
|
| 139 |
+
f.write(json.dumps(log_entry) + "\n")
|
| 140 |
gr.Info("Thank you for voting!")
|
| 141 |
return f"Logged: {vote} by user {user_id}"
|
| 142 |
|
|
|
|
| 149 |
gr.Markdown("<h1 style='text-align: center;'>Would you read this book?</h1>")
|
| 150 |
gr.Markdown(
|
| 151 |
"""<p style='text-align: center;'>Looking for your next summer read?
|
| 152 |
+
Would you read a book based on this LLM generated blurb? <br> Your vote will be added to <a href="https://huggingface.co/datasets/your-username/your-dataset-repo">this</a> Hugging Face dataset</p>"""
|
| 153 |
)
|
| 154 |
|
| 155 |
# Add the login button
|