CodeNine commited on
Commit
b72d44d
Β·
verified Β·
1 Parent(s): 731a90d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -2,34 +2,43 @@ import streamlit as st
2
  import openai
3
  import os
4
 
5
- # Get your OpenAI API key (store as secret in Hugging Face later)
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
- st.set_page_config(page_title="AI World Builder Game", page_icon="🌍")
 
9
  st.title("🌍 AI-Generated Fantasy World Explorer")
 
10
 
11
- st.markdown("Explore unique regions generated by AI. Every click reveals a new civilization, its culture, and quests!")
 
 
 
12
 
 
13
  if st.button("🧭 Explore New Region"):
14
- with st.spinner("Generating region..."):
15
  prompt = """
16
- Create a fantasy world region. Include:
17
- 1. Name of the region
18
- 2. Civilization name and a short backstory
19
- 3. Details about the culture (food, clothing, beliefs)
20
- 4. One interesting quest in that area
21
- Format nicely for display.
 
 
22
  """
23
 
24
  try:
25
- response = openai.ChatCompletion.create(
26
  model="gpt-4o",
27
  messages=[{"role": "user", "content": prompt}],
28
- temperature=0.8
 
29
  )
30
- story = response.choices[0].message.content
31
- st.success("New region discovered!")
32
- st.markdown(f"```markdown\n{story}\n```")
33
 
34
  except Exception as e:
35
- st.error(f"Error: {e}")
 
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}")