|
|
import gradio as gr |
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
|
import torch |
|
|
|
|
|
|
|
|
model_id = "microsoft/phi-2" |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
model = AutoModelForCausalLM.from_pretrained(model_id) |
|
|
|
|
|
|
|
|
PERSONA = """ |
|
|
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI. |
|
|
You speak like a chill human, not a bot. Keep replies under 15 words, natural, clever, emotional.] |
|
|
""" |
|
|
|
|
|
|
|
|
def build_prompt(message, history): |
|
|
prompt = PERSONA |
|
|
for user, bot in history[-3:]: |
|
|
prompt += f"\nYou: {user}\nπ΄ ππ πππ: {bot}" |
|
|
prompt += f"\nYou: {message}\nπ΄ ππ πππ:" |
|
|
return prompt |
|
|
|
|
|
|
|
|
def generate(message, history): |
|
|
prompt = build_prompt(message, history) |
|
|
inputs = tokenizer(prompt, return_tensors="pt") |
|
|
outputs = model.generate( |
|
|
**inputs, |
|
|
max_new_tokens=50, |
|
|
temperature=0.9, |
|
|
top_k=50, |
|
|
do_sample=True, |
|
|
pad_token_id=tokenizer.eos_token_id |
|
|
) |
|
|
result = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
reply = result.split("π΄ ππ πππ:")[-1].split("\n")[0].strip() |
|
|
reply = " ".join(reply.split()[:15]) |
|
|
history.append((message, reply)) |
|
|
return history, history |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# π΄ ππ πππ π€\n*Vibing on CPU only - Hugging Face Free Tier*") |
|
|
chatbot = gr.Chatbot() |
|
|
msg = gr.Textbox(placeholder="Type your vibe...", show_label=False) |
|
|
state = gr.State([]) |
|
|
|
|
|
msg.submit(generate, [msg, state], [chatbot, state]) |
|
|
|
|
|
demo.queue() |
|
|
demo.launch() |