Spaces:
Runtime error
Runtime error
Commit
Β·
dce9e22
1
Parent(s):
122f288
update inference logic
Browse files
app.py
CHANGED
|
@@ -6,30 +6,57 @@ import glob
|
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
from pathlib import Path
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
import
|
| 12 |
from huggingface_hub import CommitScheduler, hf_hub_download, login
|
| 13 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 14 |
-
from outlines import models, generate
|
| 15 |
-
from gradio import update
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
@spaces.GPU(duration=120)
|
| 22 |
-
def generate_blurb(history):
|
| 23 |
-
model = models.transformers(model_id)
|
| 24 |
-
generator = generate.text(model)
|
| 25 |
-
resp = generator("Write a blurb for a book")
|
| 26 |
-
return resp
|
| 27 |
|
| 28 |
# Function to log blurb and vote
|
| 29 |
def log_blurb_and_vote(blurb, vote):
|
| 30 |
log_entry = {"timestamp": datetime.now().isoformat(), "blurb": blurb, "vote": vote}
|
| 31 |
with open("blurb_log.jsonl", "a") as f:
|
| 32 |
f.write(json.dumps(log_entry) + "\n")
|
|
|
|
| 33 |
return f"Logged: {vote}"
|
| 34 |
|
| 35 |
|
|
@@ -38,19 +65,28 @@ tufte_theme = TufteInspired()
|
|
| 38 |
|
| 39 |
# Create Gradio interface
|
| 40 |
with gr.Blocks(theme=tufte_theme) as demo:
|
| 41 |
-
gr.Markdown("<h1 style='text-align: center;'>Would you read
|
| 42 |
gr.Markdown(
|
| 43 |
-
"
|
|
|
|
|
|
|
| 44 |
)
|
|
|
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
-
generate_btn = gr.Button("
|
| 47 |
-
blurb_output = gr.
|
| 48 |
-
with gr.Row():
|
| 49 |
upvote_btn = gr.Button("π would read")
|
| 50 |
downvote_btn = gr.Button("π wouldn't read")
|
| 51 |
-
vote_output = gr.Textbox(label="Vote Status", interactive=False)
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
generate_btn.click(generate_blurb, outputs=blurb_output)
|
|
|
|
|
|
|
| 54 |
upvote_btn.click(
|
| 55 |
lambda x: log_blurb_and_vote(x, "upvote"),
|
| 56 |
inputs=blurb_output,
|
|
@@ -62,5 +98,6 @@ with gr.Blocks(theme=tufte_theme) as demo:
|
|
| 62 |
outputs=vote_output,
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|
|
|
|
| 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 |
+
# TODOs
|
| 18 |
+
# 1. Add a login button
|
| 19 |
+
# 2. Prompt library expand
|
| 20 |
+
# 3. log user if logged in
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
client = OpenAI(
|
| 24 |
+
base_url="https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct/v1",
|
| 25 |
+
api_key=get_token(),
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def generate_prompt():
|
| 30 |
+
if random.choice([True, False]):
|
| 31 |
+
return detailed_genre_description_prompt()
|
| 32 |
+
else:
|
| 33 |
+
return basic_prompt()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def generate_blurb():
|
| 37 |
+
max_tokens = random.randint(100, 1000)
|
| 38 |
+
prompt = generate_prompt()
|
| 39 |
+
print(prompt)
|
| 40 |
+
chat_completion = client.chat.completions.create(
|
| 41 |
+
model="tgi",
|
| 42 |
+
messages=[
|
| 43 |
+
{"role": "user", "content": prompt},
|
| 44 |
+
],
|
| 45 |
+
stream=True,
|
| 46 |
+
max_tokens=max_tokens,
|
| 47 |
+
)
|
| 48 |
+
full_text = ""
|
| 49 |
+
for message in chat_completion:
|
| 50 |
+
full_text += message.choices[0].delta.content
|
| 51 |
+
yield full_text
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Function to log blurb and vote
|
| 55 |
def log_blurb_and_vote(blurb, vote):
|
| 56 |
log_entry = {"timestamp": datetime.now().isoformat(), "blurb": blurb, "vote": vote}
|
| 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}"
|
| 61 |
|
| 62 |
|
|
|
|
| 65 |
|
| 66 |
# Create Gradio interface
|
| 67 |
with gr.Blocks(theme=tufte_theme) as demo:
|
| 68 |
+
gr.Markdown("<h1 style='text-align: center;'>Would you read this book?</h1>")
|
| 69 |
gr.Markdown(
|
| 70 |
+
"""<p style='text-align: center;'>Looking for your next summer read?
|
| 71 |
+
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>"""
|
| 72 |
+
+ """"""
|
| 73 |
)
|
| 74 |
+
# gr.LoginButton(size="sm")
|
| 75 |
+
user_name = gr.Textbox(label="User Name", placeholder="Enter your name")
|
| 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(generate_blurb, outputs=blurb_output).then(
|
| 88 |
+
show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
|
| 89 |
+
)
|
| 90 |
upvote_btn.click(
|
| 91 |
lambda x: log_blurb_and_vote(x, "upvote"),
|
| 92 |
inputs=blurb_output,
|
|
|
|
| 98 |
outputs=vote_output,
|
| 99 |
)
|
| 100 |
|
| 101 |
+
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|