Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,61 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
if not GROQ_API_KEY:
|
| 10 |
-
st.error("β GROQ_API_KEY
|
| 11 |
st.stop()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
st.set_page_config(page_title="Groq Battle Arena", page_icon="βοΈ")
|
| 15 |
-
st.title("βοΈ Groq Battle Arena")
|
| 16 |
-
st.markdown("Choose your fighter and generate an epic AI battle scene powered by LLaMA 3!")
|
| 17 |
-
|
| 18 |
-
# Character selection
|
| 19 |
classes = ["π₯ Fire Knight", "βοΈ Ice Mage", "π©Έ Shadow Assassin", "β‘ Storm Archer", "π Dragon Tamer"]
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
if st.button("
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set up Streamlit UI
|
| 6 |
+
st.set_page_config(page_title="Groq Battle Game", page_icon="βοΈ")
|
| 7 |
+
st.title("βοΈ Groq Battle Game")
|
| 8 |
+
st.markdown("Welcome, warrior! Choose your hero and enter the arena!")
|
| 9 |
+
|
| 10 |
+
# Get Groq API key from Hugging Face Secrets
|
| 11 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 12 |
|
| 13 |
+
# Show error if API key not found
|
| 14 |
if not GROQ_API_KEY:
|
| 15 |
+
st.error("β GROQ_API_KEY is missing. Go to your Hugging Face Space β Settings β Secrets and add it.")
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
+
# Hero Class Dropdown
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
classes = ["π₯ Fire Knight", "βοΈ Ice Mage", "π©Έ Shadow Assassin", "β‘ Storm Archer", "π Dragon Tamer"]
|
| 20 |
+
hero = st.selectbox("Choose your hero class:", classes)
|
| 21 |
+
|
| 22 |
+
# Button to generate story
|
| 23 |
+
if st.button("Enter Battle"):
|
| 24 |
+
st.info("βοΈ Generating your battle scene using Groq...")
|
| 25 |
+
|
| 26 |
+
# Prompt for Groq API
|
| 27 |
+
prompt = f"""
|
| 28 |
+
Write a dramatic fantasy battle story where the hero is a {hero}.
|
| 29 |
+
The story should include:
|
| 30 |
+
- An intense setting
|
| 31 |
+
- A powerful enemy
|
| 32 |
+
- Use of special abilities
|
| 33 |
+
- A heroic or surprising ending
|
| 34 |
+
Write it like a fantasy novel, with vivid descriptions.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
headers = {
|
| 38 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 39 |
+
"Content-Type": "application/json"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
data = {
|
| 43 |
+
"model": "llama3-8b-8192",
|
| 44 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 45 |
+
"temperature": 0.9,
|
| 46 |
+
"max_tokens": 700
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
|
| 51 |
+
result = response.json()
|
| 52 |
+
story = result['choices'][0]['message']['content']
|
| 53 |
+
st.success("π Your battle story is ready!")
|
| 54 |
+
st.markdown(f"```\n{story}\n```")
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
st.error(f"π¨ Error: {e}")
|
| 58 |
+
|
| 59 |
+
# Footer
|
| 60 |
+
st.markdown("---")
|
| 61 |
+
st.markdown("Made with β€οΈ using Groq + Streamlit on Hugging Face Spaces")
|