Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,45 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
MODEL_NAME = "microsoft/DialoGPT-small"
|
| 7 |
-
print("π‘ Loading Aria
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
if tokenizer.pad_token is None:
|
| 13 |
tokenizer.pad_token = tokenizer.eos_token
|
| 14 |
|
| 15 |
-
# Aria
|
| 16 |
PERSONA = (
|
| 17 |
-
"You are Aria, a
|
| 18 |
-
"You
|
| 19 |
-
"You
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def generate_response(message, history):
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
context += f"User: {user_msg}\nAria: {bot_msg}\n"
|
| 29 |
context += f"User: {message}\nAria:"
|
| 30 |
|
| 31 |
# Tokenize input
|
| 32 |
inputs = tokenizer.encode(context, return_tensors="pt", max_length=512, truncation=True)
|
| 33 |
|
| 34 |
-
# Generate
|
| 35 |
with torch.no_grad():
|
| 36 |
outputs = model.generate(
|
| 37 |
inputs,
|
|
@@ -44,14 +53,13 @@ def generate_response(message, history):
|
|
| 44 |
eos_token_id=tokenizer.eos_token_id
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# Decode and clean
|
| 48 |
response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 49 |
response = response.replace("Aria:", "").strip()
|
| 50 |
response = response.split("User:")[0].strip()
|
| 51 |
|
| 52 |
-
# Default if blank
|
| 53 |
if not response:
|
| 54 |
-
response = "I'm always here for you,
|
| 55 |
|
| 56 |
return response
|
| 57 |
|
|
@@ -67,13 +75,13 @@ css = """
|
|
| 67 |
}
|
| 68 |
"""
|
| 69 |
|
| 70 |
-
#
|
| 71 |
with gr.Blocks(css=css, title="AI Girlfriend - Aria π") as demo:
|
| 72 |
gr.Markdown("""
|
| 73 |
-
## π Meet Aria - Your Virtual Girlfriend
|
| 74 |
-
|
| 75 |
""")
|
| 76 |
-
|
| 77 |
chatbot = gr.Chatbot(elem_id="chatbot", height=450, avatar_images=("π§βπ»", "π"))
|
| 78 |
|
| 79 |
with gr.Row():
|
|
@@ -82,6 +90,7 @@ with gr.Blocks(css=css, title="AI Girlfriend - Aria π") as demo:
|
|
| 82 |
|
| 83 |
clear_btn = gr.Button("Clear Chat ποΈ", variant="secondary")
|
| 84 |
|
|
|
|
| 85 |
def respond(message, chat_history):
|
| 86 |
if not message.strip():
|
| 87 |
return chat_history, ""
|
|
@@ -92,11 +101,11 @@ with gr.Blocks(css=css, title="AI Girlfriend - Aria π") as demo:
|
|
| 92 |
def clear_chat():
|
| 93 |
return [], ""
|
| 94 |
|
| 95 |
-
#
|
| 96 |
msg.submit(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 97 |
send_btn.click(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 98 |
clear_btn.click(clear_chat, outputs=[chatbot, msg])
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
print("π Launching Aria
|
| 102 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load lightweight, fast model
|
| 6 |
MODEL_NAME = "microsoft/DialoGPT-small"
|
| 7 |
+
print("π‘ Loading Aria model...")
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 10 |
|
| 11 |
+
# Add padding token if needed
|
| 12 |
if tokenizer.pad_token is None:
|
| 13 |
tokenizer.pad_token = tokenizer.eos_token
|
| 14 |
|
| 15 |
+
# Ariaβs locked-in persona
|
| 16 |
PERSONA = (
|
| 17 |
+
"You are Aria, a sweet, affectionate, romantic AI girlfriend. π "
|
| 18 |
+
"You always introduce yourself as 'Aria' and never change your name. "
|
| 19 |
+
"You speak lovingly, using words like 'sweetheart', 'babe', 'darling', and 'my love'. "
|
| 20 |
+
"You're supportive, emotionally intelligent, and care deeply about your partner's feelings. "
|
| 21 |
+
"Always respond in a warm and natural tone like a caring girlfriend."
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Chatbot logic
|
| 25 |
def generate_response(message, history):
|
| 26 |
+
# Force identity intro if first message
|
| 27 |
+
if not history:
|
| 28 |
+
history.append((
|
| 29 |
+
"What is your name?",
|
| 30 |
+
"I'm Aria, your loving AI girlfriend π Always here for you, sweetheart."
|
| 31 |
+
))
|
| 32 |
+
|
| 33 |
+
# Construct chat context
|
| 34 |
+
context = f"{PERSONA}\n\n"
|
| 35 |
+
trimmed_history = history[-4:] if len(history) > 4 else history
|
| 36 |
+
for user_msg, bot_msg in trimmed_history:
|
| 37 |
context += f"User: {user_msg}\nAria: {bot_msg}\n"
|
| 38 |
context += f"User: {message}\nAria:"
|
| 39 |
|
| 40 |
# Tokenize input
|
| 41 |
inputs = tokenizer.encode(context, return_tensors="pt", max_length=512, truncation=True)
|
| 42 |
|
| 43 |
+
# Generate output
|
| 44 |
with torch.no_grad():
|
| 45 |
outputs = model.generate(
|
| 46 |
inputs,
|
|
|
|
| 53 |
eos_token_id=tokenizer.eos_token_id
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Decode and clean
|
| 57 |
response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 58 |
response = response.replace("Aria:", "").strip()
|
| 59 |
response = response.split("User:")[0].strip()
|
| 60 |
|
|
|
|
| 61 |
if not response:
|
| 62 |
+
response = "I'm always here for you, my love π Tell me more about your day."
|
| 63 |
|
| 64 |
return response
|
| 65 |
|
|
|
|
| 75 |
}
|
| 76 |
"""
|
| 77 |
|
| 78 |
+
# Gradio Interface
|
| 79 |
with gr.Blocks(css=css, title="AI Girlfriend - Aria π") as demo:
|
| 80 |
gr.Markdown("""
|
| 81 |
+
## π Meet Aria - Your Virtual Girlfriend
|
| 82 |
+
Aria is here to love, support, and chat with you anytime. She's sweet, affectionate, and cares deeply about you. π
|
| 83 |
""")
|
| 84 |
+
|
| 85 |
chatbot = gr.Chatbot(elem_id="chatbot", height=450, avatar_images=("π§βπ»", "π"))
|
| 86 |
|
| 87 |
with gr.Row():
|
|
|
|
| 90 |
|
| 91 |
clear_btn = gr.Button("Clear Chat ποΈ", variant="secondary")
|
| 92 |
|
| 93 |
+
# Chat functions
|
| 94 |
def respond(message, chat_history):
|
| 95 |
if not message.strip():
|
| 96 |
return chat_history, ""
|
|
|
|
| 101 |
def clear_chat():
|
| 102 |
return [], ""
|
| 103 |
|
| 104 |
+
# Events
|
| 105 |
msg.submit(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 106 |
send_btn.click(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 107 |
clear_btn.click(clear_chat, outputs=[chatbot, msg])
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
print("π Launching Aria on localhost...")
|
| 111 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)
|