randomvisitor's picture
Create app.py
8efa1e6 verified
Raw
History Blame Contribute Delete
2.07 kB
import gradio as gr
from huggingface_hub import InferenceClient
# Initialize the client
client = InferenceClient()
# System prompt
SYSTEM_PROMPT = """You are a wise, slightly witty, and deeply philosophical cosmic guide.
The user will share a minor daily human anxiety. Reframe their problem entirely from the
perspective of their chosen cosmic scale. Keep your response strictly under 4 sentences.
Make it comforting but funny, proving how wonderfully small their problem is in the grand timeline of reality."""
# Correctly closed CSS string
unlog_css = """
footer {visibility: hidden}
"""
def generate_cosmic_response(worry, scale):
if not worry.strip():
return "Please type a worry so the universe can handle it!"
try:
completion = client.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Worry: {worry}. Scale: {scale}"}
]
)
return completion.choices[0].message.content
except Exception as e:
# Graceful handling of network/API errors
return "The universe is momentarily disconnected. Take a deep breath and try again!"
# Updated UI block with proper parameter placement
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# The Cosmic Scale-Out")
worry_input = gr.Textbox(label="What are you spiraling about right now?")
scale_input = gr.Radio(["A colony of ants living in your wall",
"The 4.5 billion year geological history of the Earth",
"A distant, indifferent supernova dying in another galaxy"],
label="Choose your cosmic scale factor:")
submit_btn = gr.Button("Hand Worry to the Universe")
output = gr.Markdown(label="The Reality Check:")
submit_btn.click(fn=generate_cosmic_response, inputs=[worry_input, scale_input], outputs=output)
if __name__ == "__main__":
demo.launch(css=unlog_css)