Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,47 @@ import streamlit as st
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
-
#
|
| 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.
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
try:
|
| 33 |
response = openai.ChatCompletion.create(
|
| 34 |
model="gpt-4o",
|
| 35 |
messages=[{"role": "user", "content": prompt}],
|
| 36 |
-
temperature=0.
|
| 37 |
-
max_tokens=
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
st.success("
|
| 41 |
-
st.markdown(f"```markdown\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}")
|