StoryScribe / app.py
sainkan's picture
Update app.py
35f46a2 verified
import os
import streamlit as st
import anthropic
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Retrieve the API key from environment variables
api_key = os.getenv("claude_api_key")
# Initialize the Anthropic client with the API key
client = anthropic.Anthropic(api_key=api_key)
# Define the functions to generate content
def generate_game_environment(environment_description):
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=150,
temperature=0.7,
system="You are an expert in world-building. Generate a detailed description of a game environment based on the input.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"Create a detailed description of a game environment based on this input: {environment_description}"
}
]
}
]
)
return message.content[0].text
def generate_protagonist(protagonist_description):
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=150,
temperature=0.7,
system="You are an expert in character creation. Generate a detailed description of a game protagonist based on the input.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"Create a detailed description of a game protagonist based on this input: {protagonist_description}"
}
]
}
]
)
return message.content[0].text
def generate_antagonist(antagonist_description):
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=150,
temperature=0.7,
system="You are an expert in villain creation. Generate a detailed description of a game antagonist based on the input.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"Create a detailed description of a game antagonist based on this input: {antagonist_description}"
}
]
}
]
)
return message.content[0].text
def generate_game_story(environment, protagonist, antagonist):
story_prompt = (f"Create a detailed game story based on the following inputs:\n"
f"Game Environment: {environment}\n"
f"Protagonist: {protagonist}\n"
f"Antagonist: {antagonist}")
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens= 150,
temperature=0.7,
system="You are a master storyteller. Generate a detailed game story based on the inputs provided.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": story_prompt
}
]
}
]
)
return message.content[0].text
# App Title with a bit of styling
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>StoryScribe</h1>", unsafe_allow_html=True)
# App Description
st.markdown("<p style='text-align: center; color: #888;'>StoryForge helps game developers generate comprehensive Game Design Documents. Input details about your game environment, protagonist, and antagonist to create a structured design document.</p>", unsafe_allow_html=True)
# Sidebar Inputs
with st.sidebar:
st.markdown("<h2 style='color: #FF6347;'>Game Details</h2>", unsafe_allow_html=True)
game_environment = st.text_input("Game Environment", "Describe the setting of your game")
protagonist = st.text_input("Protagonist", "Describe the main character")
antagonist = st.text_input("Antagonist", "Describe the main villain or opposing force")
if st.button("Generate Document"):
# Generate content based on user input
env_description = generate_game_environment(game_environment)
protagonist_description = generate_protagonist(protagonist)
antagonist_description = generate_antagonist(antagonist)
game_story = generate_game_story(game_environment, protagonist, antagonist)
# Store results in session state
st.session_state.env_description = env_description
st.session_state.protagonist_description = protagonist_description
st.session_state.antagonist_description = antagonist_description
st.session_state.game_story = game_story
# Layout with two columns
col1, col2 = st.columns(2)
# Game Environment
with col1:
st.markdown("<h2 style='color: #007BFF;'>Game Environment</h2>", unsafe_allow_html=True)
if 'env_description' in st.session_state:
st.write(st.session_state.env_description)
else:
st.write(game_environment)
# Game Story
with col2:
st.markdown("<h2 style='color: #007BFF;'>Game Story</h2>", unsafe_allow_html=True)
if 'game_story' in st.session_state:
st.write(st.session_state.game_story)
else:
st.write("Your game story will be generated based on the inputs provided.")
# Protagonist
with col1:
st.markdown("<h2 style='color: #FF4500;'>Protagonist</h2>", unsafe_allow_html=True)
if 'protagonist_description' in st.session_state:
st.write(st.session_state.protagonist_description)
else:
st.write(protagonist)
# Antagonist
with col2:
st.markdown("<h2 style='color: #FF4500;'>Antagonist</h2>", unsafe_allow_html=True)
if 'antagonist_description' in st.session_state:
st.write(st.session_state.antagonist_description)
else:
st.write(antagonist)