CodeNine commited on
Commit
4910d49
Β·
verified Β·
1 Parent(s): b72d44d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
app.py CHANGED
@@ -2,43 +2,47 @@ import streamlit as st
2
  import openai
3
  import os
4
 
5
- # Load OpenAI API key from Hugging Face Secrets
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
- # Page setup
9
- st.set_page_config(page_title="AI World Builder", page_icon="🌍")
10
- st.title("🌍 AI-Generated Fantasy World Explorer")
11
- st.markdown("Click the button below to explore a randomly generated region, complete with civilization and quest!")
12
-
13
- # Check for missing API key
14
  if not openai.api_key:
15
- st.error("❌ OPENAI_API_KEY not found. Go to 'Settings β†’ Secrets' in your Hugging Face Space and add it.")
16
  st.stop()
17
 
18
- # Button to trigger generation
19
- if st.button("🧭 Explore New Region"):
20
- with st.spinner("Generating region with AI..."):
21
- prompt = """
22
- You are a fantasy world generator.
23
- Create a unique region in a fantasy world. Include the following:
24
- 1. Region name
25
- 2. Description of the landscape
26
- 3. Civilization name and backstory
27
- 4. Culture (food, clothing, beliefs)
28
- 5. One creative quest the player can take
29
- Format the result clearly.
 
 
 
 
 
 
 
 
 
30
  """
31
 
32
  try:
33
  response = openai.ChatCompletion.create(
34
  model="gpt-4o",
35
  messages=[{"role": "user", "content": prompt}],
36
- temperature=0.8,
37
- max_tokens=800
38
  )
39
- content = response.choices[0].message.content
40
- st.success("πŸ—ΊοΈ Region generated!")
41
- st.markdown(f"```markdown\n{content}\n```")
42
 
43
  except Exception as e:
44
  st.error(f"⚠️ Error: {e}")
 
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}")