Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Set your API key
|
6 |
+
os.environ['GROQ_API_KEY'] = "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"
|
7 |
+
|
8 |
+
# Initialize the Groq client
|
9 |
+
client = Groq(
|
10 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
11 |
+
)
|
12 |
+
|
13 |
+
def generate_game_content(protagonist, antagonist, environment, tone, style, protagonist_background, antagonist_motivation, environment_detail):
|
14 |
+
# Create a prompt that includes character details and environment details
|
15 |
+
messages = [
|
16 |
+
{
|
17 |
+
"role": "user",
|
18 |
+
"content": f"Create detailed descriptions for the following game elements in a {tone}, {style} style:\n\n\
|
19 |
+
**Protagonist**: {protagonist} (Background: {protagonist_background})\n\
|
20 |
+
**Antagonist**: {antagonist} (Motivation: {antagonist_motivation})\n\
|
21 |
+
**Environment**: {environment} (Details: {environment_detail})\n\n\
|
22 |
+
Also, provide a brief plot for the game."
|
23 |
+
}
|
24 |
+
]
|
25 |
+
|
26 |
+
try:
|
27 |
+
chat_completion = client.chat.completions.create(
|
28 |
+
messages=messages,
|
29 |
+
model="llama3-8b-8192",
|
30 |
+
)
|
31 |
+
|
32 |
+
response = chat_completion.choices[0].message.content
|
33 |
+
|
34 |
+
protagonist_desc = "Protagonist Description:\n" + response.split("Antagonist Description:")[0].strip() if "Antagonist Description:" in response else response
|
35 |
+
antagonist_desc = "Antagonist Description:\n" + response.split("Antagonist Description:")[1].split("Environment Description:")[0].strip() if "Environment Description:" in response else ""
|
36 |
+
environment_desc = "Environment Description:\n" + response.split("Environment Description:")[1].split("Story Plot:")[0].strip() if "Story Plot:" in response else ""
|
37 |
+
story_plot = "Story Plot:\n" + response.split("Story Plot:")[1].strip() if "Story Plot:" in response else ""
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
protagonist_desc = "An error occurred while generating the content."
|
41 |
+
antagonist_desc = ""
|
42 |
+
environment_desc = ""
|
43 |
+
story_plot = ""
|
44 |
+
|
45 |
+
return protagonist_desc, antagonist_desc, environment_desc, story_plot
|
46 |
+
|
47 |
+
# Gradio interface
|
48 |
+
def gradio_interface():
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
with gr.Row():
|
51 |
+
gr.Markdown("<h2 style='text-align: center;'><img src='/content/image.jpg' width='200' height='100'> PixelPlotter</h2>")
|
52 |
+
with gr.Row():
|
53 |
+
gr.Markdown("<h2 style='text-align: center;'>🕹️ PixelPlotter</h2>")
|
54 |
+
with gr.Row():
|
55 |
+
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>")
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column(scale=1, min_width=150):
|
61 |
+
gr.Markdown("<h3 style='font-size: 18px; font-weight: bold;'>Options</h3>")
|
62 |
+
protagonist_background = gr.Dropdown(label="Protagonist Background", choices=["Orphaned hero", "Reluctant leader", "Chosen one with hidden powers", "Hardened warrior","Random"], value="Orphaned hero")
|
63 |
+
antagonist_motivation = gr.Dropdown(label="Antagonist Motivation", choices=["Revenge", "Thirst for power", "Ideological conflict", "Desire for chaos","Random"], value="Revenge")
|
64 |
+
environment = gr.Textbox(label="Game Environment")
|
65 |
+
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="Stormy mountains")
|
66 |
+
|
67 |
+
|
68 |
+
with gr.Column(scale=4):
|
69 |
+
with gr.Row():
|
70 |
+
tone = gr.Dropdown(label="Tone", choices=["dark", "humorous", "epic", "light-hearted","Random"], value="epic")
|
71 |
+
style = gr.Dropdown(label="Style", choices=["formal", "casual", "dramatic", "whimsical","Random"], value="formal")
|
72 |
+
with gr.Row():
|
73 |
+
protagonist = gr.Textbox(label="Protagonist")
|
74 |
+
with gr.Row():
|
75 |
+
antagonist = gr.Textbox(label="Antagonist")
|
76 |
+
|
77 |
+
generate_button = gr.Button("Generate", elem_id="generate-button")
|
78 |
+
|
79 |
+
protagonist_output = gr.Markdown()
|
80 |
+
antagonist_output = gr.Markdown()
|
81 |
+
environment_output = gr.Markdown()
|
82 |
+
story_plot_output = gr.Markdown()
|
83 |
+
|
84 |
+
generate_button.click(
|
85 |
+
fn=generate_game_content,
|
86 |
+
inputs=[protagonist, antagonist, environment, tone, style, protagonist_background, antagonist_motivation, environment_detail],
|
87 |
+
outputs=[protagonist_output, antagonist_output, environment_output, story_plot_output]
|
88 |
+
)
|
89 |
+
|
90 |
+
# Add inline CSS to style elements
|
91 |
+
gr.Markdown("""
|
92 |
+
<style>
|
93 |
+
#generate-button {
|
94 |
+
background-color: #ff7f00;
|
95 |
+
color: white;
|
96 |
+
border-radius: 5px;
|
97 |
+
padding: 10px;
|
98 |
+
font-size: 16px;
|
99 |
+
}
|
100 |
+
</style>
|
101 |
+
""")
|
102 |
+
|
103 |
+
demo.launch()
|
104 |
+
|
105 |
+
gradio_interface()
|