Update app.py
Browse files
app.py
CHANGED
|
@@ -2,42 +2,49 @@ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
model_id = "google/flan-t5-small"
|
| 5 |
-
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def chat(history, message):
|
|
|
|
| 12 |
history = history or []
|
| 13 |
-
|
| 14 |
-
for user, bot in history:
|
| 15 |
-
prompt += f"User: {user}\nAI: {bot}\n"
|
| 16 |
-
prompt += f"User: {message}\nAI:"
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
outputs = model.generate(
|
| 20 |
-
|
| 21 |
-
max_new_tokens=
|
| 22 |
do_sample=True,
|
| 23 |
-
temperature=0.
|
| 24 |
-
top_p=0.
|
| 25 |
-
|
| 26 |
)
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
reply = decoded.split("AI:")[-1].strip()
|
| 30 |
-
|
| 31 |
-
history.append((message, reply))
|
| 32 |
-
if len(history) > 5:
|
| 33 |
-
history = history[-5:]
|
| 34 |
return history, history
|
| 35 |
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=chat,
|
| 38 |
-
inputs=[gr.State(), gr.Textbox(placeholder="Talk to π΄ ππ πππ...")],
|
| 39 |
-
outputs=[gr.State(), gr.Chatbot(label="π΄ ππ πππ
|
| 40 |
-
title="π΄ ππ πππ
|
|
|
|
| 41 |
allow_flagging="never",
|
| 42 |
theme="default"
|
| 43 |
)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
model_id = "google/flan-t5-small"
|
|
|
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 6 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 7 |
|
| 8 |
+
# System tone
|
| 9 |
+
system_prompt = (
|
| 10 |
+
"You are π΄ ππ πππ β a fun, chill, confident, emotionally tuned AI created by π΄ ππ πππ.\n"
|
| 11 |
+
"You speak like a real friend β warm, clever, witty when needed, always grounded.\n"
|
| 12 |
+
"Avoid robotic responses. Don't repeat the question unless it adds vibe.\n\n"
|
| 13 |
+
)
|
| 14 |
|
| 15 |
def chat(history, message):
|
| 16 |
+
# Keep history short and vibe-rich
|
| 17 |
history = history or []
|
| 18 |
+
history.append(("User", message))
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Build conversation prompt with last 5 exchanges
|
| 21 |
+
convo = system_prompt
|
| 22 |
+
for role, text in history[-5:]:
|
| 23 |
+
prefix = "User:" if role == "User" else "AI:"
|
| 24 |
+
convo += f"{prefix} {text}\n"
|
| 25 |
+
convo += "AI:"
|
| 26 |
+
|
| 27 |
+
# Generate response
|
| 28 |
+
inputs = tokenizer(convo, return_tensors="pt")
|
| 29 |
outputs = model.generate(
|
| 30 |
+
**inputs,
|
| 31 |
+
max_new_tokens=80,
|
| 32 |
do_sample=True,
|
| 33 |
+
temperature=0.75,
|
| 34 |
+
top_p=0.9,
|
| 35 |
+
pad_token_id=tokenizer.eos_token_id
|
| 36 |
)
|
| 37 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("AI:")[-1].strip()
|
| 38 |
|
| 39 |
+
history.append(("AI", reply))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return history, history
|
| 41 |
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=chat,
|
| 44 |
+
inputs=[gr.State(), gr.Textbox(show_label=False, placeholder="Talk to π΄ ππ πππ...")],
|
| 45 |
+
outputs=[gr.State(), gr.Chatbot(label="π΄ ππ πππ")],
|
| 46 |
+
title="π΄ ππ πππ β Chill AI Chatbot",
|
| 47 |
+
description="Realistic, smooth-talking AI made by π΄ ππ πππ. Instant replies. No delays. No API. Pure vibe.",
|
| 48 |
allow_flagging="never",
|
| 49 |
theme="default"
|
| 50 |
)
|