import gradio as gr import json from datetime import datetime from theme import TufteInspired import uuid from huggingface_hub import InferenceClient from openai import OpenAI from huggingface_hub import get_token, login from prompts import detailed_genre_description_prompt, basic_prompt import random import os # Ensure you're logged in to Hugging Face login(get_token()) client = OpenAI( base_url="https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct/v1", api_key=get_token(), ) def generate_prompt(): if random.choice([True, False]): return detailed_genre_description_prompt() else: return basic_prompt() def generate_blurb(): max_tokens = random.randint(100, 1000) prompt = generate_prompt() print(prompt) chat_completion = client.chat.completions.create( model="tgi", messages=[ {"role": "user", "content": prompt}, ], stream=True, max_tokens=max_tokens, ) full_text = "" for message in chat_completion: full_text += message.choices[0].delta.content yield full_text # Function to log blurb and vote def log_blurb_and_vote(blurb, vote, user_info: gr.OAuthProfile | None, *args): if user_info is not None: user_id = user_info.username else: user_id = str(uuid.uuid4()) log_entry = { "timestamp": datetime.now().isoformat(), "blurb": blurb, "vote": vote, "user_id": user_id } with open("blurb_log.jsonl", "a") as f: f.write(json.dumps(log_entry) + "\n") gr.Info("Thank you for voting!") return f"Logged: {vote} by user {user_id}" # Create custom theme tufte_theme = TufteInspired() # Create Gradio interface with gr.Blocks(theme=tufte_theme) as demo: gr.Markdown("
Looking for your next summer read?
Would you read a book based on this LLM generated blurb?
Your vote will be added to this Hugging Face dataset