app.py
CHANGED
|
@@ -11,7 +11,7 @@ def create_character(name, age, gender, personality, backstory):
|
|
| 11 |
"personality": personality,
|
| 12 |
"backstory": backstory,
|
| 13 |
}
|
| 14 |
-
return f"Character {name} created successfully!"
|
| 15 |
|
| 16 |
# Function to handle personality-based responses
|
| 17 |
def respond_with_personality(character_name, message, history):
|
|
@@ -36,31 +36,6 @@ def respond_with_personality(character_name, message, history):
|
|
| 36 |
history.append((message, response))
|
| 37 |
return history, response
|
| 38 |
|
| 39 |
-
# Function to get available characters for switching
|
| 40 |
-
def available_characters():
|
| 41 |
-
return list(characters.keys())
|
| 42 |
-
|
| 43 |
-
# Gradio interface to create characters
|
| 44 |
-
def create_character_interface():
|
| 45 |
-
with gr.Row():
|
| 46 |
-
name_input = gr.Textbox(label="Character Name", placeholder="Enter character name")
|
| 47 |
-
age_input = gr.Number(label="Character Age", value=25, step=1)
|
| 48 |
-
gender_input = gr.Dropdown(choices=["Male", "Female", "Other"], label="Character Gender")
|
| 49 |
-
personality_input = gr.Dropdown(
|
| 50 |
-
choices=["Heroic", "Sarcastic", "Optimistic", "Empathetic"],
|
| 51 |
-
label="Character Personality"
|
| 52 |
-
)
|
| 53 |
-
backstory_input = gr.Textbox(label="Character Backstory", placeholder="Enter backstory")
|
| 54 |
-
|
| 55 |
-
create_button = gr.Button("Create Character")
|
| 56 |
-
create_output = gr.Textbox(label="Output", interactive=False)
|
| 57 |
-
|
| 58 |
-
create_button.click(create_character,
|
| 59 |
-
inputs=[name_input, age_input, gender_input, personality_input, backstory_input],
|
| 60 |
-
outputs=[create_output])
|
| 61 |
-
|
| 62 |
-
return name_input, age_input, gender_input, personality_input, backstory_input, create_button, create_output
|
| 63 |
-
|
| 64 |
# Main chat function that handles conversation with multiple characters
|
| 65 |
def chat(character_name, message, history):
|
| 66 |
if character_name not in characters:
|
|
@@ -77,11 +52,32 @@ def build_interface():
|
|
| 77 |
|
| 78 |
# Character creation UI
|
| 79 |
with gr.Tab("Create Character"):
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Chat UI
|
| 83 |
with gr.Tab("Chat"):
|
| 84 |
-
character_selector = gr.Dropdown(choices=available_characters(), label="Select Character")
|
| 85 |
chat_box = gr.Chatbot(label="Chat History")
|
| 86 |
message_input = gr.Textbox(label="Your Message", placeholder="Type a message...")
|
| 87 |
|
|
|
|
| 11 |
"personality": personality,
|
| 12 |
"backstory": backstory,
|
| 13 |
}
|
| 14 |
+
return f"Character {name} created successfully!", list(characters.keys())
|
| 15 |
|
| 16 |
# Function to handle personality-based responses
|
| 17 |
def respond_with_personality(character_name, message, history):
|
|
|
|
| 36 |
history.append((message, response))
|
| 37 |
return history, response
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Main chat function that handles conversation with multiple characters
|
| 40 |
def chat(character_name, message, history):
|
| 41 |
if character_name not in characters:
|
|
|
|
| 52 |
|
| 53 |
# Character creation UI
|
| 54 |
with gr.Tab("Create Character"):
|
| 55 |
+
name_input = gr.Textbox(label="Character Name", placeholder="Enter character name")
|
| 56 |
+
age_input = gr.Number(label="Character Age", value=25, step=1)
|
| 57 |
+
gender_input = gr.Dropdown(choices=["Male", "Female", "Other"], label="Character Gender")
|
| 58 |
+
personality_input = gr.Dropdown(
|
| 59 |
+
choices=["Heroic", "Sarcastic", "Optimistic", "Empathetic"],
|
| 60 |
+
label="Character Personality"
|
| 61 |
+
)
|
| 62 |
+
backstory_input = gr.Textbox(label="Character Backstory", placeholder="Enter backstory")
|
| 63 |
+
|
| 64 |
+
create_button = gr.Button("Create Character")
|
| 65 |
+
create_output = gr.Textbox(label="Output", interactive=False)
|
| 66 |
+
character_selector = gr.Dropdown(choices=[], label="Select Character")
|
| 67 |
+
|
| 68 |
+
# Update the dropdown after character creation
|
| 69 |
+
def create_and_update(name, age, gender, personality, backstory):
|
| 70 |
+
output_message, updated_character_list = create_character(name, age, gender, personality, backstory)
|
| 71 |
+
return output_message, gr.update(choices=updated_character_list)
|
| 72 |
+
|
| 73 |
+
create_button.click(
|
| 74 |
+
create_and_update,
|
| 75 |
+
inputs=[name_input, age_input, gender_input, personality_input, backstory_input],
|
| 76 |
+
outputs=[create_output, character_selector]
|
| 77 |
+
)
|
| 78 |
|
| 79 |
# Chat UI
|
| 80 |
with gr.Tab("Chat"):
|
|
|
|
| 81 |
chat_box = gr.Chatbot(label="Chat History")
|
| 82 |
message_input = gr.Textbox(label="Your Message", placeholder="Type a message...")
|
| 83 |
|