Spaces:
Sleeping
Sleeping
File size: 1,931 Bytes
7c9128e 69c5d57 3274a84 7c9128e 69c5d57 3274a84 7c9128e 69c5d57 7c9128e b72d44d 3274a84 7c9128e 69c5d57 7c9128e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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")
|