Spaces:
Sleeping
Sleeping
Commit
Β·
3ef94a5
1
Parent(s):
0cd2bb2
fix
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ from datetime import datetime
|
|
7 |
from pathlib import Path
|
8 |
|
9 |
import gradio as gr
|
10 |
-
from huggingface_hub import CommitScheduler,
|
11 |
from openai import OpenAI
|
12 |
|
13 |
from prompts import basic_prompt, detailed_genre_description_prompt
|
@@ -16,6 +16,8 @@ from theme import TufteInspired
|
|
16 |
# Ensure you're logged in to Hugging Face
|
17 |
login(os.getenv("HF_TOKEN"))
|
18 |
|
|
|
|
|
19 |
client = OpenAI(
|
20 |
base_url="https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct/v1",
|
21 |
api_key=get_token(),
|
@@ -25,13 +27,11 @@ client = OpenAI(
|
|
25 |
dataset_folder = Path("dataset")
|
26 |
dataset_folder.mkdir(exist_ok=True)
|
27 |
|
28 |
-
|
29 |
# Function to get the latest dataset file
|
30 |
def get_latest_dataset_file():
|
31 |
files = list(dataset_folder.glob("data_*.jsonl"))
|
32 |
return max(files, key=os.path.getctime) if files else None
|
33 |
|
34 |
-
|
35 |
# Check for existing dataset and create or append to it
|
36 |
if latest_file := get_latest_dataset_file():
|
37 |
dataset_file = latest_file
|
@@ -47,26 +47,23 @@ scheduler = CommitScheduler(
|
|
47 |
repo_type="dataset",
|
48 |
folder_path=dataset_folder,
|
49 |
path_in_repo="data",
|
50 |
-
every=1, # Upload every
|
51 |
)
|
52 |
|
53 |
-
#
|
54 |
votes = {}
|
55 |
|
56 |
-
|
57 |
def generate_prompt():
|
58 |
if random.choice([True, False]):
|
59 |
return detailed_genre_description_prompt()
|
60 |
else:
|
61 |
return basic_prompt()
|
62 |
|
63 |
-
|
64 |
def get_and_store_prompt():
|
65 |
prompt = generate_prompt()
|
66 |
print(prompt) # Keep this for debugging
|
67 |
return prompt
|
68 |
|
69 |
-
|
70 |
def generate_blurb(prompt):
|
71 |
max_tokens = random.randint(100, 1000)
|
72 |
chat_completion = client.chat.completions.create(
|
@@ -82,19 +79,16 @@ def generate_blurb(prompt):
|
|
82 |
full_text += message.choices[0].delta.content
|
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 |
gr.Info("You've already voted on this blurb!")
|
|
|
98 |
|
99 |
votes[vote_id] = vote
|
100 |
|
@@ -108,9 +102,9 @@ def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *
|
|
108 |
with scheduler.lock:
|
109 |
with dataset_file.open("a") as f:
|
110 |
f.write(json.dumps(log_entry) + "\n")
|
|
|
111 |
gr.Info("Thank you for voting! Your feedback will be synced to the dataset.")
|
112 |
-
return f"Logged: {vote} by user {user_id}"
|
113 |
-
|
114 |
|
115 |
# Create custom theme
|
116 |
tufte_theme = TufteInspired()
|
@@ -123,7 +117,6 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
123 |
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>"""
|
124 |
)
|
125 |
|
126 |
-
# Add the login button
|
127 |
with gr.Row():
|
128 |
login_btn = gr.LoginButton(size="sm")
|
129 |
with gr.Row():
|
@@ -137,12 +130,12 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
137 |
upvote_btn = gr.Button("π would read")
|
138 |
downvote_btn = gr.Button("π wouldn't read")
|
139 |
|
140 |
-
vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=
|
141 |
|
142 |
def generate_and_show(prompt, user_info):
|
143 |
-
#
|
144 |
-
global votes
|
145 |
-
votes = {}
|
146 |
return "Generating...", gr.Row.update(visible=False), user_info
|
147 |
|
148 |
def show_voting_buttons(blurb):
|
@@ -164,7 +157,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
164 |
gr.Textbox(value="upvote", visible=False),
|
165 |
user_state,
|
166 |
],
|
167 |
-
outputs=vote_output,
|
168 |
)
|
169 |
downvote_btn.click(
|
170 |
log_blurb_and_vote,
|
@@ -174,7 +167,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
174 |
gr.Textbox(value="downvote", visible=False),
|
175 |
user_state,
|
176 |
],
|
177 |
-
outputs=vote_output,
|
178 |
)
|
179 |
|
180 |
if __name__ == "__main__":
|
|
|
7 |
from pathlib import Path
|
8 |
|
9 |
import gradio as gr
|
10 |
+
from huggingface_hub import CommitScheduler, get_token, login
|
11 |
from openai import OpenAI
|
12 |
|
13 |
from prompts import basic_prompt, detailed_genre_description_prompt
|
|
|
16 |
# Ensure you're logged in to Hugging Face
|
17 |
login(os.getenv("HF_TOKEN"))
|
18 |
|
19 |
+
|
20 |
+
|
21 |
client = OpenAI(
|
22 |
base_url="https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct/v1",
|
23 |
api_key=get_token(),
|
|
|
27 |
dataset_folder = Path("dataset")
|
28 |
dataset_folder.mkdir(exist_ok=True)
|
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 |
# Check for existing dataset and create or append to it
|
36 |
if latest_file := get_latest_dataset_file():
|
37 |
dataset_file = latest_file
|
|
|
47 |
repo_type="dataset",
|
48 |
folder_path=dataset_folder,
|
49 |
path_in_repo="data",
|
50 |
+
every=1, # Upload every minute
|
51 |
)
|
52 |
|
53 |
+
# Global dictionary to store votes
|
54 |
votes = {}
|
55 |
|
|
|
56 |
def generate_prompt():
|
57 |
if random.choice([True, False]):
|
58 |
return detailed_genre_description_prompt()
|
59 |
else:
|
60 |
return basic_prompt()
|
61 |
|
|
|
62 |
def get_and_store_prompt():
|
63 |
prompt = generate_prompt()
|
64 |
print(prompt) # Keep this for debugging
|
65 |
return prompt
|
66 |
|
|
|
67 |
def generate_blurb(prompt):
|
68 |
max_tokens = random.randint(100, 1000)
|
69 |
chat_completion = client.chat.completions.create(
|
|
|
79 |
full_text += message.choices[0].delta.content
|
80 |
yield full_text
|
81 |
|
|
|
82 |
def generate_vote_id(user_id, blurb):
|
|
|
83 |
return hashlib.md5(f"{user_id}:{blurb}".encode()).hexdigest()
|
84 |
|
|
|
|
|
85 |
def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *args):
|
86 |
user_id = user_info.username if user_info is not None else str(uuid.uuid4())
|
87 |
vote_id = generate_vote_id(user_id, blurb)
|
88 |
|
89 |
if vote_id in votes:
|
90 |
gr.Info("You've already voted on this blurb!")
|
91 |
+
return None, gr.Row.update(visible=False)
|
92 |
|
93 |
votes[vote_id] = vote
|
94 |
|
|
|
102 |
with scheduler.lock:
|
103 |
with dataset_file.open("a") as f:
|
104 |
f.write(json.dumps(log_entry) + "\n")
|
105 |
+
|
106 |
gr.Info("Thank you for voting! Your feedback will be synced to the dataset.")
|
107 |
+
return f"Logged: {vote} by user {user_id}", gr.Row.update(visible=False)
|
|
|
108 |
|
109 |
# Create custom theme
|
110 |
tufte_theme = TufteInspired()
|
|
|
117 |
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>"""
|
118 |
)
|
119 |
|
|
|
120 |
with gr.Row():
|
121 |
login_btn = gr.LoginButton(size="sm")
|
122 |
with gr.Row():
|
|
|
130 |
upvote_btn = gr.Button("π would read")
|
131 |
downvote_btn = gr.Button("π wouldn't read")
|
132 |
|
133 |
+
vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=True)
|
134 |
|
135 |
def generate_and_show(prompt, user_info):
|
136 |
+
# Optionally clear votes for the previous blurb if needed
|
137 |
+
# global votes
|
138 |
+
# votes = {k: v for k, v in votes.items() if not k.endswith(hash(previous_blurb))}
|
139 |
return "Generating...", gr.Row.update(visible=False), user_info
|
140 |
|
141 |
def show_voting_buttons(blurb):
|
|
|
157 |
gr.Textbox(value="upvote", visible=False),
|
158 |
user_state,
|
159 |
],
|
160 |
+
outputs=[vote_output, voting_row],
|
161 |
)
|
162 |
downvote_btn.click(
|
163 |
log_blurb_and_vote,
|
|
|
167 |
gr.Textbox(value="downvote", visible=False),
|
168 |
user_state,
|
169 |
],
|
170 |
+
outputs=[vote_output, voting_row],
|
171 |
)
|
172 |
|
173 |
if __name__ == "__main__":
|