Spaces:
Runtime error
Runtime error
File size: 5,171 Bytes
458fbe4 5b61aa1 cb7b1ec 458fbe4 4934b4e 458fbe4 cb7b1ec 458fbe4 cb7b1ec 4934b4e cb7b1ec 458fbe4 cb7b1ec 458fbe4 cb7b1ec 4934b4e cb7b1ec 458fbe4 cb7b1ec 458fbe4 cb7b1ec 4934b4e cb7b1ec 458fbe4 cb7b1ec 458fbe4 cb7b1ec 4934b4e cb7b1ec 458fbe4 5b61aa1 cb7b1ec 5b61aa1 cb7b1ec 5b61aa1 cb7b1ec 5b61aa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import os
from dotenv import load_dotenv
import gradio as gr
import anthropic
# Load environment variables from .env file
load_dotenv()
# Get the API key from environment variables
def get_client():
api_key = os.getenv("claude_api_key")
if not api_key:
raise ValueError("API key not found. Please set the ANTHROPIC_API_KEY environment variable.")
return anthropic.Anthropic(api_key=api_key)
def generate_game_environment(description_prompt):
client = get_client()
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=200,
temperature=0.7,
system="You are a creative writer specialized in generating immersive game environments.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": description_prompt
}
]
}
]
)
return response['choices'][0]['text'].strip()
def generate_protagonist(description_prompt):
client = get_client()
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=200,
temperature=0.7,
system="You are a character designer specializing in creating detailed protagonists for games.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": description_prompt
}
]
}
]
)
return response['choices'][0]['text'].strip()
def generate_antagonist(description_prompt):
client = get_client()
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=200,
temperature=0.7,
system="You are a character designer specializing in creating compelling antagonists for games.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": description_prompt
}
]
}
]
)
return response['choices'][0]['text'].strip()
def generate_game_story(environment, protagonist, antagonist):
description_prompt = (
f"Create a compelling game story based on the following details:\n"
f"Game Environment: {environment}\n"
f"Protagonist: {protagonist}\n"
f"Antagonist: {antagonist}\n"
"Please provide a narrative that ties these elements together in an engaging way."
)
client = get_client()
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=200,
temperature=0.7,
system="You are a master storyteller. Generate an engaging game story from the given details.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": description_prompt
}
]
}
]
)
return response['choices'][0]['text'].strip()
def generate_game_design(environment, protagonist, antagonist):
# Generate each part of the game design
env_description = generate_game_environment(environment)
prot_description = generate_protagonist(protagonist)
antag_description = generate_antagonist(antagonist)
game_story = generate_game_story(env_description, prot_description, antag_description)
return env_description, prot_description, antag_description, game_story
def main():
with gr.Blocks() as demo:
gr.Markdown("# StoryForge\nA tool to help you generate engaging game design ideas.")
with gr.Row():
with gr.Column():
gr.Markdown("## Game Development")
environment = gr.Textbox(label="Game Environment", placeholder="e.g., a post-apocalyptic city")
protagonist = gr.Textbox(label="Protagonist", placeholder="e.g., a young warrior with a mysterious past")
antagonist = gr.Textbox(label="Antagonist", placeholder="e.g., a dark sorcerer seeking to conquer the world")
submit_btn = gr.Button("Generate Game Design")
with gr.Column():
gr.Markdown("## Protagonist")
gr.Markdown("## Antagonist")
gr.Markdown("## Game Story")
env_output = gr.Textbox(label="Game Environment", interactive=False)
prot_output = gr.Textbox(label="Protagonist", interactive=False)
antag_output = gr.Textbox(label="Antagonist", interactive=False)
game_story_output = gr.Textbox(label="Game Story", interactive=False)
submit_btn.click(generate_game_design, inputs=[environment, protagonist, antagonist], outputs=[env_output, prot_output, antag_output, game_story_output])
demo.launch()
if __name__ == "__main__":
main()
|