import gradio as gr from transformers import pipeline from transformers import AutoModelForCausalLM, AutoTokenizer model_path = "finetuned_phi2" model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True) tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) def generate(question): system_message = "You are a question answering chatbot. Provide a clear and detailed explanation" prompt = f"[INST] <>\n{system_message}\n<>\n\n {question} [/INST]" # replace the command here with something relevant to your task num_new_tokens = 500 # 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)