CodeNine commited on
Commit
69c5d57
Β·
verified Β·
1 Parent(s): fc17977

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -33
app.py CHANGED
@@ -1,48 +1,56 @@
1
  import streamlit as st
2
- import openai
3
  import os
4
 
5
- # Set OpenAI API Key
6
- openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
- # Check API key
9
- if not openai.api_key:
10
- st.error("❌ OPENAI_API_KEY not found. Add it in Settings β†’ Secrets in your Hugging Face Space.")
11
  st.stop()
12
 
13
- # Page UI
14
- st.set_page_config(page_title="AI Quest Generator", page_icon="βš”οΈ")
15
- st.title("βš”οΈ AI-Powered Quest Generator")
16
- st.markdown("Choose your character class and generate a fantasy adventure!")
17
 
18
- # Character selection
19
- classes = ["πŸ›‘οΈ Warrior", "πŸ§™ Mage", "πŸ—‘οΈ Thief", "🏹 Ranger", "πŸ‰ Dragonborn"]
20
- selected_class = st.selectbox("Choose your class:", classes)
 
 
 
21
 
22
- # Generate button
23
- if st.button("🎲 Generate My Quest"):
24
- with st.spinner("Summoning your quest..."):
25
  prompt = f"""
26
- You are a game master. Create an exciting fantasy quest for a player who is a {selected_class}.
27
  Include:
28
- - Quest title
29
- - Setting description
30
- - Mission objective
31
- - Main enemy
32
- - Twist or surprise
33
- Write in an engaging story style.
34
  """
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  try:
37
- response = openai.ChatCompletion.create(
38
- model="gpt-4o",
39
- messages=[{"role": "user", "content": prompt}],
40
- temperature=0.9,
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"⚠️ Error: {e}")
 
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}")