world-builder / app.py
CodeNine's picture
Update app.py
4250f66 verified
raw
history blame
1.98 kB
import streamlit as st
import requests
import os
# Load Groq API key from Hugging Face Secrets
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# Check API key
if not GROQ_API_KEY:
st.error("❌ GROQ_API_KEY not found! Add it in Settings β†’ Secrets.")
st.stop()
# App UI
st.set_page_config(page_title="Groq Battle Arena", page_icon="βš”οΈ")
st.title("βš”οΈ Groq Battle Arena")
st.markdown("Choose your fighter and generate an epic AI battle scene powered by LLaMA 3!")
# Character selection
classes = ["πŸ”₯ Fire Knight", "❄️ Ice Mage", "🩸 Shadow Assassin", "⚑ Storm Archer", "πŸ‰ Dragon Tamer"]
selected_class = st.selectbox("Select Your Warrior:", classes)
# Generate battle on button click
if st.button("βš”οΈ Enter the Arena"):
with st.spinner("Summoning your battle..."):
prompt = f"""
Write a cinematic fantasy battle scene for a hero who is a {selected_class}.
Include:
- Setting of the battle
- Description of the enemy
- Special powers used
- Epic conclusion
Write like a fantasy novel, in storytelling tone.
"""
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
json_data = {
"model": "llama3-8b-8192",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.9,
"max_tokens": 700,
"top_p": 1
}
try:
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=json_data)
output = response.json()['choices'][0]['message']['content']
st.success("βš”οΈ Battle Generated!")
st.markdown(f"```markdown\n{output}\n```")
except Exception as e:
st.error(f"❌ Error: {e}")
# Optional bottom message to help Hugging Face recognize app
if __name__ == "__main__":
st.write("βœ… App initialized.")