prompt-roulette / app.py
Nathan Chang
tiny refactor for more stable logging
191c0da
Raw History Blame
3.99 kB
import gradio as gr
import os
import logging
from openai import OpenAI
css = """
.gradio-container {
max-width: min(90vw, 800px) !important;
margin: 0 auto !important;
display: flex !important;
}
@keyframes rainbow {
0% { background: hsl(0, 60%, 70%); }
16% { background: hsl(60, 60%, 70%); }
33% { background: hsl(120, 60%, 70%); }
50% { background: hsl(180, 60%, 70%); }
66% { background: hsl(240, 60%, 70%); }
83% { background: hsl(300, 60%, 70%); }
100% { background: hsl(360, 60%, 70%); }
}
#rainbow-btn {
background: #e4e4e7;
color: #27272a !important;
}
#rainbow-btn:hover {
animation: rainbow 3s linear infinite !important;
}
h1 {
color: #1f2937 !important;
font-size: 2.5em !important;
font-weight: 600 !important;
font-family: "Helvetica Neue", Arial, "Segoe UI", Roboto, sans-serif !important;
text-align: center !important;
margin: 20px 0 !important;
border-radius: 0 !important;
background: transparent !important;
text-shadow: none !important;
}
"""
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def generate_prompt(request: gr.Request):
try:
session_id = request.session_hash
# Read the system prompt from file
system_prompt = os.environ.get("SYSTEM_PROMPT")
api_key = os.environ.get("OPENAI_API_KEY")
# Validate required environment variables
if not system_prompt:
logger.error("SYSTEM_PROMPT environment variable not set")
return "⚠️ Service temporarily unavailable. Please try again later."
if not api_key:
logger.error("OPENAI_API_KEY environment variable not set")
return "⚠️ Service temporarily unavailable. Please try again later."
# Initialize OpenAI client
client = OpenAI(api_key=api_key)
# Make API call
logger.info(f"Making OpenAI API request - Session: {session_id}")
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": "Generate a new system prompt."}
],
max_tokens=500,
temperature=0.9
)
logger.info(f"API request successful - Session: {session_id} - tokens used: {response.usage.total_tokens}")
return response.choices[0].message.content
except Exception as e:
logger.error(f"API request failed - Session: {session_id} - Error: {str(e)}")
return "⚠️ Could not generate a prompt. No fish today."
with gr.Blocks(
title="Prompt Roulette",
css=css
) as demo:
gr.Markdown("# PROMPT ROULETTE")
gr.Markdown(
"Tired of talking to the same boring AI every day?\n\n"
"Tired of friendly exclamation points (!), 👋 emojis, and bullet point lists?\n\n"
)
gr.Markdown(
"**Try a new system prompt!**\n\n"
"Click the button below to discover a new side of your favorite AI assistant. "
"Once you find a prompt you like, paste it at the top of your next AI conversation to bring it to life!"
)
generate_btn = gr.Button("Spin the wheel!", variant="primary", elem_id="rainbow-btn")
output_text = gr.Textbox(
label="Your New Prompt",
lines=8,
max_lines=15,
interactive=False,
show_copy_button=True
)
generate_btn.click(
fn=generate_prompt,
outputs=[output_text]
)
gr.Markdown(
"**MCP-compatible!** Simply add `n8cha/prompt-roulette` to your list in https://hf.co/settings/mcp to expose it as a convenient tool for your AI conversation partner."
)
if __name__ == "__main__":
demo.queue(
max_size=5, # Maximum number of requests in queue
default_concurrency_limit=1 # Maximum concurrent API calls
)
demo.launch(mcp_server=True)