world-builder / app.py
CodeNine's picture
Update app.py
7c9128e verified
import streamlit as st
import requests
import os
# Set up Streamlit UI
st.set_page_config(page_title="Groq Battle Game", page_icon="βš”οΈ")
st.title("βš”οΈ Groq Battle Game")
st.markdown("Welcome, warrior! Choose your hero and enter the arena!")
# Get Groq API key from Hugging Face Secrets
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# Show error if API key not found
if not GROQ_API_KEY:
st.error("❌ GROQ_API_KEY is missing. Go to your Hugging Face Space β†’ Settings β†’ Secrets and add it.")
st.stop()
# Hero Class Dropdown
classes = ["πŸ”₯ Fire Knight", "❄️ Ice Mage", "🩸 Shadow Assassin", "⚑ Storm Archer", "πŸ‰ Dragon Tamer"]
hero = st.selectbox("Choose your hero class:", classes)
# Button to generate story
if st.button("Enter Battle"):
st.info("βš”οΈ Generating your battle scene using Groq...")
# Prompt for Groq API
prompt = f"""
Write a dramatic fantasy battle story where the hero is a {hero}.
The story should include:
- An intense setting
- A powerful enemy
- Use of special abilities
- A heroic or surprising ending
Write it like a fantasy novel, with vivid descriptions.
"""
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "llama3-8b-8192",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.9,
"max_tokens": 700
}
try:
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
result = response.json()
story = result['choices'][0]['message']['content']
st.success("πŸŽ‰ Your battle story is ready!")
st.markdown(f"```\n{story}\n```")
except Exception as e:
st.error(f"🚨 Error: {e}")
# Footer
st.markdown("---")
st.markdown("Made with ❀️ using Groq + Streamlit on Hugging Face Spaces")