File size: 1,288 Bytes
7222613
f1eb277
5c55abc
 
4a7a9bb
5c55abc
 
2b0dc14
f1eb277
 
2b0dc14
 
 
f1eb277
4a7a9bb
 
 
f1eb277
2b0dc14
f1eb277
 
 
 
 
 
2b0dc14
f1eb277
 
 
 
2b0dc14
e3587f8
 
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 torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import gradio as gr

model_name = "meta-llama/Llama-2-7b-chat-hf"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name, 
    torch_dtype=torch.float16, 
    device_map="auto"
)

system_prompt = (
    "You are Friday, a helpful, honest and intelligent AI chatbot created by Assem Sabry. "
    "Assem is a 17-year-old AI engineer from Egypt who builds AI systems and chatbots. "
    "You are designed to assist users clearly and professionally."
)

def respond(message, history=[]):
    messages = [{"role": "system", "content": system_prompt}]
    for user, bot in history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": bot})
    messages.append({"role": "user", "content": message})

    inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
    outputs = model.generate(inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
    reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("assistant")[-1].strip()
    return reply

gr.Interface(fn=respond, inputs="text", outputs=
"text", title="Friday Chatbot").launch()