Spaces:
Runtime error
Runtime error
Commit
·
b0ec887
1
Parent(s):
2655ad8
Refactor app.py for improved readability and maintainability
Browse files
app.py
CHANGED
|
@@ -18,12 +18,14 @@ client = OpenAI(
|
|
| 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,24 +43,22 @@ def generate_blurb():
|
|
| 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()
|
| 64 |
|
|
@@ -69,10 +69,10 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
| 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")
|
|
@@ -99,4 +99,4 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
demo.launch(debug=True)
|
|
|
|
| 18 |
api_key=get_token(),
|
| 19 |
)
|
| 20 |
|
| 21 |
+
|
| 22 |
def generate_prompt():
|
| 23 |
if random.choice([True, False]):
|
| 24 |
return detailed_genre_description_prompt()
|
| 25 |
else:
|
| 26 |
return basic_prompt()
|
| 27 |
|
| 28 |
+
|
| 29 |
def generate_blurb():
|
| 30 |
max_tokens = random.randint(100, 1000)
|
| 31 |
prompt = generate_prompt()
|
|
|
|
| 43 |
full_text += message.choices[0].delta.content
|
| 44 |
yield full_text
|
| 45 |
|
| 46 |
+
|
| 47 |
# Function to log blurb and vote
|
| 48 |
def log_blurb_and_vote(blurb, vote, user_info: gr.OAuthProfile | None, *args):
|
| 49 |
+
user_id = user_info.username if user_info is not None else str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
log_entry = {
|
| 51 |
"timestamp": datetime.now().isoformat(),
|
| 52 |
"blurb": blurb,
|
| 53 |
"vote": vote,
|
| 54 |
+
"user_id": user_id,
|
| 55 |
}
|
| 56 |
with open("blurb_log.jsonl", "a") as f:
|
| 57 |
f.write(json.dumps(log_entry) + "\n")
|
| 58 |
gr.Info("Thank you for voting!")
|
| 59 |
return f"Logged: {vote} by user {user_id}"
|
| 60 |
|
| 61 |
+
|
| 62 |
# Create custom theme
|
| 63 |
tufte_theme = TufteInspired()
|
| 64 |
|
|
|
|
| 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")
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch(debug=True)
|