Spaces:
Running
Running
Commit
Β·
f921425
1
Parent(s):
b0ec887
update to log prompt
Browse files
app.py
CHANGED
@@ -26,10 +26,14 @@ def generate_prompt():
|
|
26 |
return basic_prompt()
|
27 |
|
28 |
|
29 |
-
def
|
30 |
-
max_tokens = random.randint(100, 1000)
|
31 |
prompt = generate_prompt()
|
32 |
-
print(prompt)
|
|
|
|
|
|
|
|
|
|
|
33 |
chat_completion = client.chat.completions.create(
|
34 |
model="tgi",
|
35 |
messages=[
|
@@ -45,10 +49,11 @@ def generate_blurb():
|
|
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,
|
@@ -75,26 +80,46 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
75 |
|
76 |
with gr.Row():
|
77 |
generate_btn = gr.Button("Create a book", variant="primary")
|
|
|
|
|
78 |
blurb_output = gr.Markdown(label="Book blurb")
|
|
|
79 |
with gr.Row(visible=False) as voting_row:
|
80 |
upvote_btn = gr.Button("π would read")
|
81 |
downvote_btn = gr.Button("π wouldn't read")
|
|
|
82 |
vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=False)
|
83 |
|
|
|
|
|
|
|
84 |
def show_voting_buttons(blurb):
|
85 |
return blurb, gr.Row(visible=True)
|
86 |
|
87 |
-
generate_btn.click(
|
|
|
|
|
88 |
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
89 |
)
|
|
|
90 |
upvote_btn.click(
|
91 |
log_blurb_and_vote,
|
92 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
93 |
outputs=vote_output,
|
94 |
)
|
95 |
downvote_btn.click(
|
96 |
log_blurb_and_vote,
|
97 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
98 |
outputs=vote_output,
|
99 |
)
|
100 |
|
|
|
26 |
return basic_prompt()
|
27 |
|
28 |
|
29 |
+
def get_and_store_prompt():
|
|
|
30 |
prompt = generate_prompt()
|
31 |
+
print(prompt) # Keep this for debugging
|
32 |
+
return prompt
|
33 |
+
|
34 |
+
|
35 |
+
def generate_blurb(prompt):
|
36 |
+
max_tokens = random.randint(100, 1000)
|
37 |
chat_completion = client.chat.completions.create(
|
38 |
model="tgi",
|
39 |
messages=[
|
|
|
49 |
|
50 |
|
51 |
# Function to log blurb and vote
|
52 |
+
def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *args):
|
53 |
user_id = user_info.username if user_info is not None else str(uuid.uuid4())
|
54 |
log_entry = {
|
55 |
"timestamp": datetime.now().isoformat(),
|
56 |
+
"prompt": prompt,
|
57 |
"blurb": blurb,
|
58 |
"vote": vote,
|
59 |
"user_id": user_id,
|
|
|
80 |
|
81 |
with gr.Row():
|
82 |
generate_btn = gr.Button("Create a book", variant="primary")
|
83 |
+
|
84 |
+
prompt_state = gr.State()
|
85 |
blurb_output = gr.Markdown(label="Book blurb")
|
86 |
+
|
87 |
with gr.Row(visible=False) as voting_row:
|
88 |
upvote_btn = gr.Button("π would read")
|
89 |
downvote_btn = gr.Button("π wouldn't read")
|
90 |
+
|
91 |
vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=False)
|
92 |
|
93 |
+
def generate_and_show(prompt):
|
94 |
+
return gr.Markdown.update(value="Generating..."), gr.Row(visible=False)
|
95 |
+
|
96 |
def show_voting_buttons(blurb):
|
97 |
return blurb, gr.Row(visible=True)
|
98 |
|
99 |
+
generate_btn.click(get_and_store_prompt, outputs=prompt_state).then(
|
100 |
+
generate_and_show, inputs=prompt_state, outputs=[blurb_output, voting_row]
|
101 |
+
).then(generate_blurb, inputs=prompt_state, outputs=blurb_output).then(
|
102 |
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
103 |
)
|
104 |
+
|
105 |
upvote_btn.click(
|
106 |
log_blurb_and_vote,
|
107 |
+
inputs=[
|
108 |
+
prompt_state,
|
109 |
+
blurb_output,
|
110 |
+
gr.Textbox(value="upvote", visible=False),
|
111 |
+
login_btn,
|
112 |
+
],
|
113 |
outputs=vote_output,
|
114 |
)
|
115 |
downvote_btn.click(
|
116 |
log_blurb_and_vote,
|
117 |
+
inputs=[
|
118 |
+
prompt_state,
|
119 |
+
blurb_output,
|
120 |
+
gr.Textbox(value="downvote", visible=False),
|
121 |
+
login_btn,
|
122 |
+
],
|
123 |
outputs=vote_output,
|
124 |
)
|
125 |
|