Spaces:
Sleeping
Sleeping
| 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") | |