Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,62 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Load lightweight
|
| 6 |
-
model_id = "microsoft/
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
-
# Persona
|
| 11 |
PERSONA = """
|
| 12 |
-
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI.
|
| 13 |
-
You speak like a
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
for user, bot in history[-3:]:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
outputs = model.generate(
|
| 29 |
-
|
| 30 |
max_new_tokens=50,
|
| 31 |
temperature=0.9,
|
| 32 |
-
top_k=
|
| 33 |
do_sample=True,
|
| 34 |
pad_token_id=tokenizer.eos_token_id
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
return history, history
|
| 41 |
|
| 42 |
-
# Gradio
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("# π΄ ππ
|
| 45 |
-
chatbot = gr.Chatbot()
|
| 46 |
-
msg = gr.Textbox(placeholder="Type
|
| 47 |
state = gr.State([])
|
| 48 |
|
| 49 |
-
msg.submit(
|
|
|
|
| 50 |
|
| 51 |
-
demo.queue()
|
| 52 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load lightweight DialoGPT
|
| 6 |
+
model_id = "microsoft/DialoGPT-medium"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
+
# Persona
|
| 11 |
PERSONA = """
|
| 12 |
+
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI.
|
| 13 |
+
You speak like a real person, not a robot. Keep it under 15 words. ππ]
|
| 14 |
"""
|
| 15 |
|
| 16 |
+
# Format history
|
| 17 |
+
def format_context(history):
|
| 18 |
+
context = PERSONA + "\n"
|
| 19 |
for user, bot in history[-3:]:
|
| 20 |
+
context += f"You: {user}\n"
|
| 21 |
+
context += f"π΄ ππ πππ: {bot}\n"
|
| 22 |
+
return context
|
| 23 |
+
|
| 24 |
+
# Humanize response
|
| 25 |
+
def enhance_response(resp, message):
|
| 26 |
+
if any(x in message for x in ["?", "think", "why"]):
|
| 27 |
+
resp += " π€"
|
| 28 |
+
elif any(x in resp.lower() for x in ["cool", "great", "love", "fun"]):
|
| 29 |
+
resp += " π"
|
| 30 |
+
return " ".join(resp.split()[:15]) # Limit to 15 words
|
| 31 |
+
|
| 32 |
+
# Generate AI reply
|
| 33 |
+
def chat(user_input, history):
|
| 34 |
+
context = format_context(history) + f"You: {user_input}\nπ΄ ππ πππ:"
|
| 35 |
+
inputs = tokenizer.encode(context, return_tensors="pt", truncation=True, max_length=1024)
|
| 36 |
+
|
| 37 |
outputs = model.generate(
|
| 38 |
+
inputs,
|
| 39 |
max_new_tokens=50,
|
| 40 |
temperature=0.9,
|
| 41 |
+
top_k=40,
|
| 42 |
do_sample=True,
|
| 43 |
pad_token_id=tokenizer.eos_token_id
|
| 44 |
)
|
| 45 |
+
|
| 46 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
+
response = full_text.split("π΄ ππ πππ:")[-1].split("\nYou:")[0].strip()
|
| 48 |
+
response = enhance_response(response, user_input)
|
| 49 |
+
|
| 50 |
+
history.append((user_input, response))
|
| 51 |
return history, history
|
| 52 |
|
| 53 |
+
# Gradio interface
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# π΄ ππ πππ\n*Smooth β’ Chill β’ Emotional*")
|
| 56 |
+
chatbot = gr.Chatbot(label="Chat").style(height=400)
|
| 57 |
+
msg = gr.Textbox(placeholder="Type somethingβ¦", show_label=False)
|
| 58 |
state = gr.State([])
|
| 59 |
|
| 60 |
+
msg.submit(chat, [msg, state], [chatbot, state])
|
| 61 |
+
gr.Button("Reset").click(lambda: ([], []), None, [chatbot, state])
|
| 62 |
|
|
|
|
| 63 |
demo.launch()
|