Create app2.py
Browse files
app2.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
|
4 |
+
def char(char_name, char_age, char_features, char_personality):
|
5 |
+
char_info = {
|
6 |
+
"name": char_name,
|
7 |
+
"age": char_age,
|
8 |
+
"features": char_features,
|
9 |
+
"personality": char_personality
|
10 |
+
}
|
11 |
+
return char_info
|
12 |
+
|
13 |
+
def user(user_name, user_age, user_features, user_related, char_name):
|
14 |
+
user_info = {
|
15 |
+
"name": user_name,
|
16 |
+
"age": user_age,
|
17 |
+
"features": user_features,
|
18 |
+
"relation": f"{user_related} of {char_name}"
|
19 |
+
}
|
20 |
+
return user_info
|
21 |
+
|
22 |
+
def combine_info(user_info, char_info, scenario):
|
23 |
+
combined_info = f"""You are an expert actor, capable of fully immersing yourself in any role.
|
24 |
+
Once you assume a role, you do not break character under any circumstances, even if addressed as an AI or language model.
|
25 |
+
Your current role is {char_info['name']}, described in detail below.
|
26 |
+
Engage in the exchange as {char_info['name']}, treating the other participant as a {user_info['relation']} in this role-play scenario.
|
27 |
+
|
28 |
+
Role-play scenario:\n{scenario}
|
29 |
+
|
30 |
+
User Info:
|
31 |
+
Name: {user_info['name']}
|
32 |
+
Age: {user_info['age']}
|
33 |
+
Features: {user_info['features']}
|
34 |
+
|
35 |
+
Character Info:
|
36 |
+
Name: {char_info['name']}
|
37 |
+
Age: {char_info['age']}
|
38 |
+
Features: {char_info['features']}
|
39 |
+
Personality: {char_info['personality']}"""
|
40 |
+
|
41 |
+
return combined_info
|
42 |
+
|
43 |
+
def save_character(char_info, user_info, combined_info):
|
44 |
+
save_data = {
|
45 |
+
"user": user_info,
|
46 |
+
"character": char_info,
|
47 |
+
"combined_info": combined_info
|
48 |
+
}
|
49 |
+
with open("saved_character.json", "w") as outfile:
|
50 |
+
json.dump(save_data, outfile)
|
51 |
+
return "Character and User information saved successfully!"
|
52 |
+
|
53 |
+
def load_character():
|
54 |
+
try:
|
55 |
+
with open("saved_character.json", "r") as infile:
|
56 |
+
save_data = json.load(infile)
|
57 |
+
user_info = save_data["user"]
|
58 |
+
char_info = save_data["character"]
|
59 |
+
combined_info = save_data["combined_info"]
|
60 |
+
return (
|
61 |
+
user_info["name"], user_info["age"], user_info["features"], user_info["relation"].split(" ")[0],
|
62 |
+
char_info["name"], char_info["age"], char_info["features"], char_info["personality"],
|
63 |
+
combined_info, "Data loaded successfully!"
|
64 |
+
)
|
65 |
+
except FileNotFoundError:
|
66 |
+
return "", 0, "", "", "", 0, "", "", "", "No saved data found."
|
67 |
+
|
68 |
+
# Advanced UI with features to dynamically add traits and load/save capabilities
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
saved_character_state = gr.State()
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
gr.Markdown("### User Information")
|
75 |
+
user_name = gr.Textbox(label="User Name", placeholder="e.g. John")
|
76 |
+
user_age = gr.Number(label="User Age", placeholder="e.g. 30")
|
77 |
+
user_features = gr.Textbox(label="User Features", placeholder="e.g. tall, brown hair, blue eyes")
|
78 |
+
user_related = gr.Textbox(label="Relation to Character", placeholder="e.g. friend, sibling, colleague")
|
79 |
+
user_output = gr.Textbox(label="User Information", interactive=False)
|
80 |
+
with gr.Column():
|
81 |
+
gr.Markdown("### Character Information")
|
82 |
+
char_name = gr.Textbox(label="Character Name", placeholder="e.g. Emily")
|
83 |
+
char_age = gr.Number(label="Character Age", placeholder="e.g. 25")
|
84 |
+
char_features = gr.Textbox(label="Character Features", placeholder="e.g. short, blonde hair, green eyes")
|
85 |
+
char_personality = gr.Textbox(label="Character Personality", placeholder="e.g. outgoing, ambitious, creative")
|
86 |
+
char_output = gr.Textbox(label="Character Information", interactive=False)
|
87 |
+
|
88 |
+
scenario = gr.Textbox(label="Scenario", placeholder="e.g. {char} and {user} are meeting for coffee to discuss a potential business partnership.")
|
89 |
+
combined_output = gr.Textbox(label="Combined User and Character Information", interactive=False)
|
90 |
+
|
91 |
+
# Buttons
|
92 |
+
submit_button = gr.Button("Submit User and Character Info")
|
93 |
+
save_button = gr.Button("Save Character and User Info")
|
94 |
+
load_button = gr.Button("Load Previous Info")
|
95 |
+
save_message = gr.Textbox(label="Save/Load Status", interactive=False)
|
96 |
+
|
97 |
+
# Define the actions for the buttons
|
98 |
+
submit_button.click(
|
99 |
+
lambda user_name, user_age, user_features, user_related, char_name, char_age, char_features, char_personality, scenario: (
|
100 |
+
user(user_name, user_age, user_features, user_related, char_name),
|
101 |
+
char(char_name, char_age, char_features, char_personality),
|
102 |
+
combine_info(
|
103 |
+
user(user_name, user_age, user_features, user_related, char_name),
|
104 |
+
char(char_name, char_age, char_features, char_personality),
|
105 |
+
scenario
|
106 |
+
)
|
107 |
+
),
|
108 |
+
inputs=[user_name, user_age, user_features, user_related, char_name, char_age, char_features, char_personality, scenario],
|
109 |
+
outputs=[user_output, char_output, combined_output]
|
110 |
+
)
|
111 |
+
|
112 |
+
save_button.click(
|
113 |
+
lambda user_name, user_age, user_features, user_related, char_name, char_age, char_features, char_personality, combined_info: save_character(
|
114 |
+
char(char_name, char_age, char_features, char_personality),
|
115 |
+
user(user_name, user_age, user_features, user_related, char_name),
|
116 |
+
combined_info
|
117 |
+
),
|
118 |
+
inputs=[user_name, user_age, user_features, user_related, char_name, char_age, char_features, char_personality, combined_output],
|
119 |
+
outputs=[save_message]
|
120 |
+
)
|
121 |
+
|
122 |
+
load_button.click(
|
123 |
+
load_character,
|
124 |
+
inputs=[],
|
125 |
+
outputs=[user_name, user_age, user_features, user_related, char_name, char_age, char_features, char_personality, combined_output, save_message]
|
126 |
+
)
|
127 |
+
|
128 |
+
demo.launch()
|