File size: 1,493 Bytes
91be14d
 
 
 
 
281c8a9
953d7e3
91be14d
 
 
c7b0633
6ea445d
 
 
281c8a9
6ea445d
 
 
 
 
 
 
 
91be14d
 
 
cc541f7
91be14d
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)