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