| import gradio as gr |
| from transformers import AutoTokenizer, AutoModelForCausalLM |
| import torch |
|
|
| |
| tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2") |
| model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2") |
|
|
| |
| PERSONA = """ |
| [System: You are π΄ ππ πππ, a smooth, chill AI who replies with emotion and charm in under 15 words.] |
| """ |
|
|
| |
| def format_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 chat(message, history): |
| prompt = format_prompt(message, history) |
| inputs = tokenizer(prompt, return_tensors="pt") |
| outputs = model.generate( |
| **inputs, |
| max_new_tokens=48, |
| 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 = reply[:80] + " π" if len(reply.split()) < 15 else reply[:80] |
| history.append((message, reply)) |
| return history, history |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# π΄ ππ πππ π€β¨") |
| chatbot = gr.Chatbot() |
| msg = gr.Textbox(placeholder="Drop something smooth...", show_label=False) |
| state = gr.State([]) |
|
|
| msg.submit(chat, [msg, state], [chatbot, state]) |
| |
| demo.queue() |
| demo.launch() |