Update app.py
Browse files
app.py
CHANGED
|
@@ -2,87 +2,49 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
PERSONA = """
|
| 12 |
-
[System: You are π΄ ππ
|
| 13 |
-
Speak like a witty friend, not a robot.
|
| 14 |
-
Keep replies natural, short (<15 words), human, and with emotional vibes: π π π€]
|
| 15 |
"""
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
for user, bot in history[-3:]:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
elif "?" in user_msg or any(w in response.lower() for w in ["think", "why", "how"]):
|
| 30 |
-
response += " π€"
|
| 31 |
-
if "?" in user_msg and not response.endswith("?"):
|
| 32 |
-
if len(response.split()) < 10:
|
| 33 |
-
response += " What do you think?"
|
| 34 |
-
|
| 35 |
-
# Make more human
|
| 36 |
-
response = response.replace("I am", "I'm").replace("You are", "You're")
|
| 37 |
-
words = response.split()
|
| 38 |
-
response = " ".join(words[:15]) if len(words) > 15 else response
|
| 39 |
-
if response and response[-1] not in {".", "!", "?", "..."}:
|
| 40 |
-
response += "." if len(response) > 20 else "..."
|
| 41 |
-
return response
|
| 42 |
-
|
| 43 |
-
# Generate the bot's reply
|
| 44 |
-
def generate_response(message, history):
|
| 45 |
-
context = format_context(history) + f"You: {message}\nπ΄ ππ πππ:"
|
| 46 |
-
inputs = tokenizer(context, return_tensors="pt")
|
| 47 |
outputs = model.generate(
|
| 48 |
**inputs,
|
| 49 |
max_new_tokens=48,
|
| 50 |
temperature=0.9,
|
| 51 |
-
top_k=
|
| 52 |
do_sample=True,
|
| 53 |
-
repetition_penalty=1.1,
|
| 54 |
pad_token_id=tokenizer.eos_token_id
|
| 55 |
)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
-
with gr.Blocks(
|
| 64 |
-
gr.Markdown("# π΄ ππ πππ
|
| 65 |
-
chatbot = gr.Chatbot(
|
| 66 |
-
msg = gr.Textbox(placeholder="
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
return "", history + [[message, None]]
|
| 72 |
-
|
| 73 |
-
def bot_reply(history):
|
| 74 |
-
user_msg = history[-1][0]
|
| 75 |
-
reply = generate_response(user_msg, history[:-1])
|
| 76 |
-
return history[:-1] + [[user_msg, reply]]
|
| 77 |
-
|
| 78 |
-
def clear_chat():
|
| 79 |
-
return []
|
| 80 |
-
|
| 81 |
-
msg.submit(user_input, [msg, history_state], [msg, history_state]).then(
|
| 82 |
-
bot_reply, history_state, [chatbot, history_state]
|
| 83 |
-
)
|
| 84 |
-
clear.click(clear_chat, None, [chatbot, history_state])
|
| 85 |
-
demo.load(lambda: [], None, history_state)
|
| 86 |
-
|
| 87 |
demo.queue()
|
| 88 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load model (CPU-friendly)
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
|
|
|
|
| 8 |
|
| 9 |
+
# Style prompt
|
| 10 |
PERSONA = """
|
| 11 |
+
[System: You are π΄ ππ πππ, a smooth, chill AI who replies with emotion and charm in under 15 words.]
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
|
| 14 |
+
# Function to format past conversation
|
| 15 |
+
def format_prompt(message, history):
|
| 16 |
+
prompt = PERSONA
|
| 17 |
for user, bot in history[-3:]:
|
| 18 |
+
prompt += f"\nYou: {user}\nπ΄ ππ πππ: {bot}"
|
| 19 |
+
prompt += f"\nYou: {message}\nπ΄ ππ πππ:"
|
| 20 |
+
return prompt
|
| 21 |
+
|
| 22 |
+
# Chat function
|
| 23 |
+
def chat(message, history):
|
| 24 |
+
prompt = format_prompt(message, history)
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
outputs = model.generate(
|
| 27 |
**inputs,
|
| 28 |
max_new_tokens=48,
|
| 29 |
temperature=0.9,
|
| 30 |
+
top_k=50,
|
| 31 |
do_sample=True,
|
|
|
|
| 32 |
pad_token_id=tokenizer.eos_token_id
|
| 33 |
)
|
| 34 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
reply = result.split("π΄ ππ πππ:")[-1].split("\n")[0].strip()
|
| 36 |
+
reply = reply[:80] + " π" if len(reply.split()) < 15 else reply[:80]
|
| 37 |
+
history.append((message, reply))
|
| 38 |
+
return history, history
|
| 39 |
+
|
| 40 |
+
# UI
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# π΄ ππ πππ π€β¨")
|
| 43 |
+
chatbot = gr.Chatbot()
|
| 44 |
+
msg = gr.Textbox(placeholder="Drop something smooth...", show_label=False)
|
| 45 |
+
state = gr.State([])
|
| 46 |
+
|
| 47 |
+
msg.submit(chat, [msg, state], [chatbot, state])
|
| 48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
demo.queue()
|
| 50 |
demo.launch()
|