Spaces:
Runtime error
Runtime error
Commit
·
2655ad8
1
Parent(s):
9ed8bcd
update logging
Browse files
app.py
CHANGED
|
@@ -2,39 +2,28 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
from theme import TufteInspired
|
| 5 |
-
import glob
|
| 6 |
-
import os
|
| 7 |
import uuid
|
| 8 |
-
from pathlib import Path
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
from openai import OpenAI
|
| 11 |
-
from huggingface_hub import get_token
|
| 12 |
-
from huggingface_hub import CommitScheduler, hf_hub_download, login
|
| 13 |
-
|
| 14 |
from prompts import detailed_genre_description_prompt, basic_prompt
|
| 15 |
import random
|
| 16 |
-
|
| 17 |
|
|
|
|
| 18 |
login(get_token())
|
| 19 |
-
# TODOs
|
| 20 |
-
# 1. Add a login button
|
| 21 |
-
# 2. Prompt library expand
|
| 22 |
-
# 3. log user if logged in
|
| 23 |
-
|
| 24 |
|
| 25 |
client = OpenAI(
|
| 26 |
base_url="https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct/v1",
|
| 27 |
api_key=get_token(),
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
def generate_prompt():
|
| 32 |
if random.choice([True, False]):
|
| 33 |
return detailed_genre_description_prompt()
|
| 34 |
else:
|
| 35 |
return basic_prompt()
|
| 36 |
|
| 37 |
-
|
| 38 |
def generate_blurb():
|
| 39 |
max_tokens = random.randint(100, 1000)
|
| 40 |
prompt = generate_prompt()
|
|
@@ -52,15 +41,23 @@ def generate_blurb():
|
|
| 52 |
full_text += message.choices[0].delta.content
|
| 53 |
yield full_text
|
| 54 |
|
| 55 |
-
|
| 56 |
# Function to log blurb and vote
|
| 57 |
-
def log_blurb_and_vote(blurb, vote):
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with open("blurb_log.jsonl", "a") as f:
|
| 60 |
f.write(json.dumps(log_entry) + "\n")
|
| 61 |
-
|
| 62 |
-
return f"Logged: {vote}"
|
| 63 |
-
|
| 64 |
|
| 65 |
# Create custom theme
|
| 66 |
tufte_theme = TufteInspired()
|
|
@@ -71,10 +68,11 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
| 71 |
gr.Markdown(
|
| 72 |
"""<p style='text-align: center;'>Looking for your next summer read?
|
| 73 |
Would you read a book based on this LLM generated blurb? <br> Your vote will be added to <a href="https://example.com">this</a> Hugging Face dataset</p>"""
|
| 74 |
-
+ """"""
|
| 75 |
)
|
| 76 |
-
|
| 77 |
-
#
|
|
|
|
|
|
|
| 78 |
with gr.Row():
|
| 79 |
generate_btn = gr.Button("Create a book", variant="primary")
|
| 80 |
blurb_output = gr.Markdown(label="Book blurb")
|
|
@@ -90,16 +88,15 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
| 90 |
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
| 91 |
)
|
| 92 |
upvote_btn.click(
|
| 93 |
-
|
| 94 |
-
inputs=blurb_output,
|
| 95 |
outputs=vote_output,
|
| 96 |
)
|
| 97 |
downvote_btn.click(
|
| 98 |
-
|
| 99 |
-
inputs=blurb_output,
|
| 100 |
outputs=vote_output,
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
if __name__ == "__main__":
|
| 105 |
-
demo.launch()
|
|
|
|
| 2 |
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())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
client = OpenAI(
|
| 17 |
base_url="https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct/v1",
|
| 18 |
api_key=get_token(),
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
def generate_prompt():
|
| 22 |
if random.choice([True, False]):
|
| 23 |
return detailed_genre_description_prompt()
|
| 24 |
else:
|
| 25 |
return basic_prompt()
|
| 26 |
|
|
|
|
| 27 |
def generate_blurb():
|
| 28 |
max_tokens = random.randint(100, 1000)
|
| 29 |
prompt = generate_prompt()
|
|
|
|
| 41 |
full_text += message.choices[0].delta.content
|
| 42 |
yield full_text
|
| 43 |
|
|
|
|
| 44 |
# Function to log blurb and vote
|
| 45 |
+
def log_blurb_and_vote(blurb, vote, user_info: gr.OAuthProfile | None, *args):
|
| 46 |
+
if user_info is not None:
|
| 47 |
+
user_id = user_info.username
|
| 48 |
+
else:
|
| 49 |
+
user_id = str(uuid.uuid4())
|
| 50 |
+
|
| 51 |
+
log_entry = {
|
| 52 |
+
"timestamp": datetime.now().isoformat(),
|
| 53 |
+
"blurb": blurb,
|
| 54 |
+
"vote": vote,
|
| 55 |
+
"user_id": user_id
|
| 56 |
+
}
|
| 57 |
with open("blurb_log.jsonl", "a") as f:
|
| 58 |
f.write(json.dumps(log_entry) + "\n")
|
| 59 |
+
gr.Info("Thank you for voting!")
|
| 60 |
+
return f"Logged: {vote} by user {user_id}"
|
|
|
|
| 61 |
|
| 62 |
# Create custom theme
|
| 63 |
tufte_theme = TufteInspired()
|
|
|
|
| 68 |
gr.Markdown(
|
| 69 |
"""<p style='text-align: center;'>Looking for your next summer read?
|
| 70 |
Would you read a book based on this LLM generated blurb? <br> Your vote will be added to <a href="https://example.com">this</a> Hugging Face dataset</p>"""
|
|
|
|
| 71 |
)
|
| 72 |
+
|
| 73 |
+
# Add the login button
|
| 74 |
+
login_btn = gr.LoginButton()
|
| 75 |
+
|
| 76 |
with gr.Row():
|
| 77 |
generate_btn = gr.Button("Create a book", variant="primary")
|
| 78 |
blurb_output = gr.Markdown(label="Book blurb")
|
|
|
|
| 88 |
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
| 89 |
)
|
| 90 |
upvote_btn.click(
|
| 91 |
+
log_blurb_and_vote,
|
| 92 |
+
inputs=[blurb_output, gr.Textbox(value="upvote", visible=False), login_btn],
|
| 93 |
outputs=vote_output,
|
| 94 |
)
|
| 95 |
downvote_btn.click(
|
| 96 |
+
log_blurb_and_vote,
|
| 97 |
+
inputs=[blurb_output, gr.Textbox(value="downvote", visible=False), login_btn],
|
| 98 |
outputs=vote_output,
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch(debug=True)
|