Spaces:
Sleeping
Sleeping
import os | |
from groq import Groq | |
import gradio as gr | |
# Set your API key | |
# Initialize the Groq client | |
client = Groq( | |
api_key=os.environ.get("GROQ_API_KEY"), | |
) | |
def generate_game_content(protagonist, antagonist, environment, tone, style, protagonist_background, antagonist_motivation, environment_detail): | |
# Create a prompt that includes character details and environment details | |
messages = [ | |
{ | |
"role": "user", | |
"content": f"Create detailed descriptions for the following game elements in a {tone}, {style} style:\n\n\ | |
**Protagonist**: {protagonist} (Background: {protagonist_background})\n\ | |
**Antagonist**: {antagonist} (Motivation: {antagonist_motivation})\n\ | |
**Environment**: {environment} (Details: {environment_detail})\n\n\ | |
Also, provide a brief plot for the game." | |
} | |
] | |
try: | |
chat_completion = client.chat.completions.create( | |
messages=messages, | |
model="llama3-8b-8192", | |
) | |
response = chat_completion.choices[0].message.content | |
protagonist_desc = "Protagonist Description:\n" + response.split("Antagonist Description:")[0].strip() if "Antagonist Description:" in response else response | |
antagonist_desc = "Antagonist Description:\n" + response.split("Antagonist Description:")[1].split("Environment Description:")[0].strip() if "Environment Description:" in response else "" | |
environment_desc = "Environment Description:\n" + response.split("Environment Description:")[1].split("Story Plot:")[0].strip() if "Story Plot:" in response else "" | |
story_plot = "Story Plot:\n" + response.split("Story Plot:")[1].strip() if "Story Plot:" in response else "" | |
except Exception as e: | |
protagonist_desc = "An error occurred while generating the content." | |
antagonist_desc = "" | |
environment_desc = "" | |
story_plot = "" | |
return protagonist_desc, antagonist_desc, environment_desc, story_plot | |
# Gradio interface | |
def gradio_interface(): | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
gr.Markdown("<h2 style='text-align: center;'>🕹️ PixelPlotter</h2>") | |
with gr.Row(): | |
gr.Markdown("<h4 style='text-align: center;'> Plot your next game masterpiece with PixelPlotter, the ultimate tool for generating game narratives, characters, and environments.</h4>") | |
with gr.Row(): | |
with gr.Column(scale=1, min_width=150): | |
gr.Markdown("<h3 style='font-size: 18px; font-weight: bold;'>Options</h3>") | |
protagonist_background = gr.Dropdown(label="Protagonist Background", choices=["Orphaned hero", "Reluctant leader", "Chosen one with hidden powers", "Hardened warrior","Random"], value="Orphaned hero") | |
antagonist_motivation = gr.Dropdown(label="Antagonist Motivation", choices=["Revenge", "Thirst for power", "Ideological conflict", "Desire for chaos","Random"], value="Revenge") | |
environment = gr.Textbox(label="Game Story (Theme about Plot") | |
environment_detail = gr.Dropdown(label="Environment Details", choices=["Stormy mountains", "Lush forests with hidden dangers", "Desolate desert with ancient ruins", "Frozen tundra with mystical creatures","random environment"], value="Random Environment") | |
with gr.Column(scale=4): | |
with gr.Row(): | |
tone = gr.Dropdown(label="Tone", choices=["dark", "humorous", "epic", "light-hearted","Random"], value="epic") | |
style = gr.Dropdown(label="Style", choices=["formal", "casual", "dramatic", "whimsical","Random"], value="formal") | |
with gr.Row(): | |
protagonist = gr.Textbox(label="Protagonist (Hero) ") | |
with gr.Row(): | |
antagonist = gr.Textbox(label="Antagonist (Villain)") | |
generate_button = gr.Button("Generate", elem_id="generate-button") | |
protagonist_output = gr.Markdown() | |
antagonist_output = gr.Markdown() | |
environment_output = gr.Markdown() | |
story_plot_output = gr.Markdown() | |
generate_button.click( | |
fn=generate_game_content, | |
inputs=[protagonist, antagonist, environment, tone, style, protagonist_background, antagonist_motivation, environment_detail], | |
outputs=[protagonist_output, antagonist_output, environment_output, story_plot_output] | |
) | |
# Add inline CSS to style elements | |
gr.Markdown(""" | |
<style> | |
#generate-button { | |
background-color: #ff7f00; | |
color: white; | |
border-radius: 5px; | |
padding: 10px; | |
font-size: 16px; | |
} | |
</style> | |
""") | |
demo.launch() | |
gradio_interface() | |