Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,56 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
st.error("β OPENAI_API_KEY not found. Add it in Settings β Secrets in your Hugging Face Space.")
|
| 11 |
st.stop()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
st.set_page_config(page_title="
|
| 15 |
-
st.title("βοΈ
|
| 16 |
-
st.markdown("Choose your
|
| 17 |
|
| 18 |
-
# Character
|
| 19 |
-
classes = ["
|
| 20 |
-
selected_class = st.selectbox("
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Generate button
|
| 23 |
-
if st.button("π² Generate My Quest"):
|
| 24 |
-
with st.spinner("Summoning your quest..."):
|
| 25 |
prompt = f"""
|
| 26 |
-
|
| 27 |
Include:
|
| 28 |
-
-
|
| 29 |
-
-
|
| 30 |
-
-
|
| 31 |
-
-
|
| 32 |
-
|
| 33 |
-
Write in an engaging story style.
|
| 34 |
"""
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
max_tokens=700
|
| 42 |
-
)
|
| 43 |
-
quest = response.choices[0].message.content
|
| 44 |
-
st.success("π§ Quest Ready!")
|
| 45 |
-
st.markdown(f"```markdown\n{quest}\n```")
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
st.error(f"
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Load Groq API key
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
|
| 8 |
+
if not GROQ_API_KEY:
|
| 9 |
+
st.error("β GROQ_API_KEY not found! Please add it in Settings β Secrets.")
|
|
|
|
| 10 |
st.stop()
|
| 11 |
|
| 12 |
+
# App UI
|
| 13 |
+
st.set_page_config(page_title="Groq Battle Arena", page_icon="βοΈ")
|
| 14 |
+
st.title("βοΈ Groq Battle Arena")
|
| 15 |
+
st.markdown("Choose your fighter and generate an epic AI battle scene!")
|
| 16 |
|
| 17 |
+
# Character classes
|
| 18 |
+
classes = ["π₯ Fire Knight", "βοΈ Ice Mage", "π©Έ Shadow Assassin", "β‘ Storm Archer", "π Dragon Tamer"]
|
| 19 |
+
selected_class = st.selectbox("Select Your Warrior:", classes)
|
| 20 |
+
|
| 21 |
+
if st.button("βοΈ Enter the Arena"):
|
| 22 |
+
with st.spinner("Groq is writing your battle..."):
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
prompt = f"""
|
| 25 |
+
Write a cinematic fantasy battle scene for a hero who is a {selected_class}.
|
| 26 |
Include:
|
| 27 |
+
- Setting of the battle
|
| 28 |
+
- Description of the enemy
|
| 29 |
+
- Special powers used
|
| 30 |
+
- Epic conclusion
|
| 31 |
+
Write like a fantasy novel, in storytelling tone.
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
headers = {
|
| 35 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 36 |
+
"Content-Type": "application/json"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
json_data = {
|
| 40 |
+
"model": "llama3-8b-8192",
|
| 41 |
+
"messages": [
|
| 42 |
+
{"role": "user", "content": prompt}
|
| 43 |
+
],
|
| 44 |
+
"temperature": 0.9,
|
| 45 |
+
"max_tokens": 700,
|
| 46 |
+
"top_p": 1
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=json_data)
|
| 51 |
+
output = res.json()['choices'][0]['message']['content']
|
| 52 |
+
st.success("βοΈ Battle Generated!")
|
| 53 |
+
st.markdown(f"```markdown\n{output}\n```")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
+
st.error(f"β Error: {e}")
|