Spaces:
Running
Running
Commit
Β·
b2e385a
1
Parent(s):
b4fc14a
check for previous vote
Browse files
app.py
CHANGED
@@ -1,16 +1,17 @@
|
|
1 |
-
import
|
2 |
import json
|
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 |
|
15 |
# Ensure you're logged in to Hugging Face
|
16 |
login(os.getenv("HF_TOKEN"))
|
@@ -40,7 +41,7 @@ else:
|
|
40 |
print(f"Creating new dataset file: {dataset_file}")
|
41 |
|
42 |
# Set up CommitScheduler for dataset uploads
|
43 |
-
repo_id = "davanstrien/summer-reading-preferences"
|
44 |
scheduler = CommitScheduler(
|
45 |
repo_id=repo_id,
|
46 |
repo_type="dataset",
|
@@ -49,6 +50,9 @@ scheduler = CommitScheduler(
|
|
49 |
every=1, # Upload every 5 minutes
|
50 |
)
|
51 |
|
|
|
|
|
|
|
52 |
|
53 |
def generate_prompt():
|
54 |
if random.choice([True, False]):
|
@@ -79,9 +83,21 @@ def generate_blurb(prompt):
|
|
79 |
yield full_text
|
80 |
|
81 |
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
83 |
def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *args):
|
84 |
user_id = user_info.username if user_info is not None else str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
log_entry = {
|
86 |
"timestamp": datetime.now().isoformat(),
|
87 |
"prompt": prompt,
|
@@ -115,6 +131,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
115 |
|
116 |
prompt_state = gr.State()
|
117 |
blurb_output = gr.Markdown(label="Book blurb")
|
|
|
118 |
|
119 |
with gr.Row(visible=False) as voting_row:
|
120 |
upvote_btn = gr.Button("π would read")
|
@@ -122,14 +139,19 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
122 |
|
123 |
vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=False)
|
124 |
|
125 |
-
def generate_and_show(prompt):
|
126 |
-
|
|
|
|
|
|
|
127 |
|
128 |
def show_voting_buttons(blurb):
|
129 |
return blurb, gr.Row.update(visible=True)
|
130 |
|
131 |
generate_btn.click(get_and_store_prompt, outputs=prompt_state).then(
|
132 |
-
generate_and_show,
|
|
|
|
|
133 |
).then(generate_blurb, inputs=prompt_state, outputs=blurb_output).then(
|
134 |
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
135 |
)
|
@@ -140,7 +162,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
140 |
prompt_state,
|
141 |
blurb_output,
|
142 |
gr.Textbox(value="upvote", visible=False),
|
143 |
-
|
144 |
],
|
145 |
outputs=vote_output,
|
146 |
)
|
@@ -150,7 +172,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
150 |
prompt_state,
|
151 |
blurb_output,
|
152 |
gr.Textbox(value="downvote", visible=False),
|
153 |
-
|
154 |
],
|
155 |
outputs=vote_output,
|
156 |
)
|
|
|
1 |
+
import hashlib
|
2 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
+
import random
|
5 |
+
import uuid
|
6 |
+
from datetime import datetime
|
7 |
from pathlib import Path
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
from huggingface_hub import CommitScheduler, InferenceClient, get_token, login
|
11 |
+
from openai import OpenAI
|
12 |
+
|
13 |
+
from prompts import basic_prompt, detailed_genre_description_prompt
|
14 |
+
from theme import TufteInspired
|
15 |
|
16 |
# Ensure you're logged in to Hugging Face
|
17 |
login(os.getenv("HF_TOKEN"))
|
|
|
41 |
print(f"Creating new dataset file: {dataset_file}")
|
42 |
|
43 |
# Set up CommitScheduler for dataset uploads
|
44 |
+
repo_id = "davanstrien/summer-reading-preferences"
|
45 |
scheduler = CommitScheduler(
|
46 |
repo_id=repo_id,
|
47 |
repo_type="dataset",
|
|
|
50 |
every=1, # Upload every 5 minutes
|
51 |
)
|
52 |
|
53 |
+
# Add a dictionary to store votes
|
54 |
+
votes = {}
|
55 |
+
|
56 |
|
57 |
def generate_prompt():
|
58 |
if random.choice([True, False]):
|
|
|
83 |
yield full_text
|
84 |
|
85 |
|
86 |
+
def generate_vote_id(user_id, blurb):
|
87 |
+
# Create a unique identifier for this vote opportunity
|
88 |
+
return hashlib.md5(f"{user_id}:{blurb}".encode()).hexdigest()
|
89 |
+
|
90 |
+
|
91 |
+
# Modified log_blurb_and_vote function
|
92 |
def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *args):
|
93 |
user_id = user_info.username if user_info is not None else str(uuid.uuid4())
|
94 |
+
vote_id = generate_vote_id(user_id, blurb)
|
95 |
+
|
96 |
+
if vote_id in votes:
|
97 |
+
return "You've already voted on this blurb!"
|
98 |
+
|
99 |
+
votes[vote_id] = vote
|
100 |
+
|
101 |
log_entry = {
|
102 |
"timestamp": datetime.now().isoformat(),
|
103 |
"prompt": prompt,
|
|
|
131 |
|
132 |
prompt_state = gr.State()
|
133 |
blurb_output = gr.Markdown(label="Book blurb")
|
134 |
+
user_state = gr.State()
|
135 |
|
136 |
with gr.Row(visible=False) as voting_row:
|
137 |
upvote_btn = gr.Button("π would read")
|
|
|
139 |
|
140 |
vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=False)
|
141 |
|
142 |
+
def generate_and_show(prompt, user_info):
|
143 |
+
# Reset votes for new blurb
|
144 |
+
global votes
|
145 |
+
votes = {}
|
146 |
+
return "Generating...", gr.Row.update(visible=False), user_info
|
147 |
|
148 |
def show_voting_buttons(blurb):
|
149 |
return blurb, gr.Row.update(visible=True)
|
150 |
|
151 |
generate_btn.click(get_and_store_prompt, outputs=prompt_state).then(
|
152 |
+
generate_and_show,
|
153 |
+
inputs=[prompt_state, login_btn],
|
154 |
+
outputs=[blurb_output, voting_row, user_state],
|
155 |
).then(generate_blurb, inputs=prompt_state, outputs=blurb_output).then(
|
156 |
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
157 |
)
|
|
|
162 |
prompt_state,
|
163 |
blurb_output,
|
164 |
gr.Textbox(value="upvote", visible=False),
|
165 |
+
user_state,
|
166 |
],
|
167 |
outputs=vote_output,
|
168 |
)
|
|
|
172 |
prompt_state,
|
173 |
blurb_output,
|
174 |
gr.Textbox(value="downvote", visible=False),
|
175 |
+
user_state,
|
176 |
],
|
177 |
outputs=vote_output,
|
178 |
)
|