Spaces:
Running
Running
Commit
·
5a3f658
1
Parent(s):
203a7f7
add sync
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())
|
@@ -18,6 +19,35 @@ client = OpenAI(
|
|
18 |
api_key=get_token(),
|
19 |
)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def generate_prompt():
|
23 |
if random.choice([True, False]):
|
@@ -58,9 +88,10 @@ def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *
|
|
58 |
"vote": vote,
|
59 |
"user_id": user_id,
|
60 |
}
|
61 |
-
with
|
62 |
-
|
63 |
-
|
|
|
64 |
return f"Logged: {vote} by user {user_id}"
|
65 |
|
66 |
|
@@ -72,13 +103,12 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
72 |
gr.Markdown("<h1 style='text-align: center;'>Would you read this book?</h1>")
|
73 |
gr.Markdown(
|
74 |
"""<p style='text-align: center;'>Looking for your next summer read?
|
75 |
-
Would you read a book based on this LLM generated blurb? <br> Your vote will be added to <a href="https://
|
76 |
)
|
77 |
|
78 |
# Add the login button
|
79 |
with gr.Row():
|
80 |
-
login_btn = gr.LoginButton()
|
81 |
-
|
82 |
with gr.Row():
|
83 |
generate_btn = gr.Button("Create a book", variant="primary")
|
84 |
|
|
|
3 |
from datetime import datetime
|
4 |
from theme import TufteInspired
|
5 |
import uuid
|
6 |
+
from huggingface_hub import InferenceClient, CommitScheduler
|
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())
|
|
|
19 |
api_key=get_token(),
|
20 |
)
|
21 |
|
22 |
+
# Set up dataset storage
|
23 |
+
dataset_folder = Path("dataset")
|
24 |
+
dataset_folder.mkdir(exist_ok=True)
|
25 |
+
|
26 |
+
|
27 |
+
# Function to get the latest dataset file
|
28 |
+
def get_latest_dataset_file():
|
29 |
+
files = list(dataset_folder.glob("data_*.jsonl"))
|
30 |
+
return max(files, key=os.path.getctime) if files else None
|
31 |
+
|
32 |
+
|
33 |
+
# Check for existing dataset and create or append to it
|
34 |
+
if latest_file := get_latest_dataset_file():
|
35 |
+
dataset_file = latest_file
|
36 |
+
print(f"Appending to existing dataset file: {dataset_file}")
|
37 |
+
else:
|
38 |
+
dataset_file = dataset_folder / f"data_{uuid.uuid4()}.jsonl"
|
39 |
+
print(f"Creating new dataset file: {dataset_file}")
|
40 |
+
|
41 |
+
# Set up CommitScheduler for dataset uploads
|
42 |
+
repo_id = "davanstrien/summer-reading-preferences" #
|
43 |
+
scheduler = CommitScheduler(
|
44 |
+
repo_id=repo_id,
|
45 |
+
repo_type="dataset",
|
46 |
+
folder_path=dataset_folder,
|
47 |
+
path_in_repo="data",
|
48 |
+
every=1, # Upload every 5 minutes
|
49 |
+
)
|
50 |
+
|
51 |
|
52 |
def generate_prompt():
|
53 |
if random.choice([True, False]):
|
|
|
88 |
"vote": vote,
|
89 |
"user_id": user_id,
|
90 |
}
|
91 |
+
with scheduler.lock:
|
92 |
+
with dataset_file.open("a") as f:
|
93 |
+
f.write(json.dumps(log_entry) + "\n")
|
94 |
+
gr.Info("Thank you for voting! Your feedback will be synced to the dataset.")
|
95 |
return f"Logged: {vote} by user {user_id}"
|
96 |
|
97 |
|
|
|
103 |
gr.Markdown("<h1 style='text-align: center;'>Would you read this book?</h1>")
|
104 |
gr.Markdown(
|
105 |
"""<p style='text-align: center;'>Looking for your next summer read?
|
106 |
+
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>"""
|
107 |
)
|
108 |
|
109 |
# Add the login button
|
110 |
with gr.Row():
|
111 |
+
login_btn = gr.LoginButton(size="sm")
|
|
|
112 |
with gr.Row():
|
113 |
generate_btn = gr.Button("Create a book", variant="primary")
|
114 |
|