CodeNine commited on
Commit
7c9128e
Β·
verified Β·
1 Parent(s): 7dba4ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -51
app.py CHANGED
@@ -1,60 +1,61 @@
1
- import streamlit as st
2
  import requests
3
  import os
4
 
5
- # Load Groq API key from Hugging Face Secrets
 
 
 
 
 
6
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
7
 
8
- # Check API key
9
  if not GROQ_API_KEY:
10
- st.error("❌ GROQ_API_KEY not found! Add it in Settings β†’ Secrets.")
11
  st.stop()
12
 
13
- # App UI
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
- selected_class = st.selectbox("Select Your Warrior:", classes)
21
-
22
- # Generate battle on button click
23
- if st.button("βš”οΈ Enter the Arena"):
24
- with st.spinner("Summoning your battle..."):
25
-
26
- prompt = f"""
27
- Write a cinematic fantasy battle scene for a hero who is a {selected_class}.
28
- Include:
29
- - Setting of the battle
30
- - Description of the enemy
31
- - Special powers used
32
- - Epic conclusion
33
- Write like a fantasy novel, in storytelling tone.
34
- """
35
-
36
- headers = {
37
- "Authorization": f"Bearer {GROQ_API_KEY}",
38
- "Content-Type": "application/json"
39
- }
40
-
41
- json_data = {
42
- "model": "llama3-8b-8192",
43
- "messages": [{"role": "user", "content": prompt}],
44
- "temperature": 0.9,
45
- "max_tokens": 700,
46
- "top_p": 1
47
- }
48
-
49
- try:
50
- response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=json_data)
51
- output = response.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}")
57
-
58
- # Optional bottom message to help Hugging Face recognize app
59
- if __name__ == "__main__":
60
- st.write("βœ… App initialized.")
 
 
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")