app.py
CHANGED
|
@@ -1,114 +1,99 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
personality
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return "
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
elif personality == "Sarcastic":
|
| 26 |
-
|
| 27 |
-
elif personality == "Inquisitive":
|
| 28 |
-
return "You are an inquisitive assistant who loves to ask follow-up questions and dig deeper into every topic."
|
| 29 |
elif personality == "Optimistic":
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
else:
|
| 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 |
-
response =
|
| 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 |
-
value="Friendly",
|
| 98 |
-
label="Select Chatbot Personality",
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
# ChatInterface with personality adjustment
|
| 102 |
-
demo = gr.ChatInterface(
|
| 103 |
-
respond_with_personality,
|
| 104 |
-
additional_inputs=[
|
| 105 |
-
personality_selector,
|
| 106 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 107 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 108 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 109 |
-
],
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# Dictionary to store characters and their attributes
|
| 4 |
+
characters = {}
|
| 5 |
+
|
| 6 |
+
# Function to create a character
|
| 7 |
+
def create_character(name, age, gender, personality, backstory):
|
| 8 |
+
characters[name] = {
|
| 9 |
+
"age": age,
|
| 10 |
+
"gender": gender,
|
| 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):
|
| 18 |
+
if character_name not in characters:
|
| 19 |
+
return f"Character {character_name} not found!", history
|
| 20 |
+
|
| 21 |
+
character = characters[character_name]
|
| 22 |
+
personality = character['personality']
|
| 23 |
+
|
| 24 |
+
if personality == "Heroic":
|
| 25 |
+
response = f"{character_name}: Fear not! With bravery, you can overcome any challenge!"
|
| 26 |
elif personality == "Sarcastic":
|
| 27 |
+
response = f"{character_name}: Oh really? Facing a tough day? Why not just take a nap? That usually works."
|
|
|
|
|
|
|
| 28 |
elif personality == "Optimistic":
|
| 29 |
+
response = f"{character_name}: Don't worry, things will turn out great! Keep your chin up!"
|
| 30 |
+
elif personality == "Empathetic":
|
| 31 |
+
response = f"{character_name}: I totally understand what you're going through. That must be really tough."
|
| 32 |
else:
|
| 33 |
+
response = f"{character_name}: {message}? I think you got this!"
|
| 34 |
+
|
| 35 |
+
# Add user message and character response to 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:
|
| 67 |
+
return history, f"Character {character_name} doesn't exist!"
|
| 68 |
+
|
| 69 |
+
# Generate a response from the selected character
|
| 70 |
+
history, response = respond_with_personality(character_name, message, history)
|
| 71 |
+
return history, response
|
| 72 |
+
|
| 73 |
+
# Main interface with character creation and chat functionality
|
| 74 |
+
def build_interface():
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("# AI Roleplay with Customizable Characters")
|
| 77 |
+
|
| 78 |
+
# Character creation UI
|
| 79 |
+
with gr.Tab("Create Character"):
|
| 80 |
+
create_character_interface()
|
| 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 |
+
|
| 88 |
+
send_button = gr.Button("Send Message")
|
| 89 |
+
send_button.click(chat,
|
| 90 |
+
inputs=[character_selector, message_input, chat_box],
|
| 91 |
+
outputs=[chat_box, message_input])
|
| 92 |
+
|
| 93 |
+
return demo
|
| 94 |
+
|
| 95 |
+
# Run the application
|
| 96 |
+
demo = build_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|