Phi2_chatbot / app.py
mkthoma's picture
app update
281c8a9
raw
history blame
No virus
1.49 kB
import gradio as gr
from transformers import pipeline
from transformers import AutoModelForCausalLM, AutoTokenizer
model_path = "finetuned_phi2"
model = AutoModelForCausalLM.from_pretrained(model_path, low_cpu_mem_usage=True, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
def generate(question, context):
system_message = "You are a question answering chatbot. Provide a clear and detailed explanation"
prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n {question} [/INST]" # replace the command here with something relevant to your task
num_new_tokens = 200 # change to the number of new tokens you want to generate
# Count the number of tokens in the prompt
num_prompt_tokens = len(tokenizer(prompt)['input_ids'])
# Calculate the maximum length for the generation
max_length = num_prompt_tokens + num_new_tokens
gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=max_length)
result = gen(prompt)
return (result[0]['generated_text'].replace(prompt, ''))
bbchatbot = gr.Chatbot(
avatar_images=["logo/user logo.png", "logo/bot logo.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
demo = gr.ChatInterface(fn=generate,
chatbot=bbchatbot,
title="🧑🏽‍💻Microsoft Phi2 Chatbot🤖"
)
demo.queue().launch(show_api=False)