File size: 2,926 Bytes
93c9f45
b26efc5
540c3e4
1b9d0f6
8204ebb
 
540c3e4
5177ae4
1b9d0f6
a4697a1
1b9d0f6
a4697a1
1b9d0f6
 
 
0a0731c
cdb4561
548127e
cdb4561
 
 
 
 
 
 
 
 
1b9d0f6
cdb4561
 
1b9d0f6
0a0731c
cdb4561
f3b0cae
fa240df
 
 
 
 
f1c8adb
d95ff21
cdb4561
 
e767674
1b9d0f6
85a3c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b9d0f6
90d41bb
9e70b49
93c9f45
 
cdb4561
f1824de
 
 
d535138
93c9f45
f1824de
cdb4561
 
ecc901e
cdb4561
 
 
f1824de
93c9f45
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
import spaces
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain.llms.base import LLM
from langchain.memory import ConversationBufferMemory
from langchain.chains import LLMChain, ConversationChain
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
from langchain.prompts import PromptTemplate, ChatPromptTemplate

@spaces.GPU
def initialize_model_and_tokenizer(model_name="KvrParaskevi/Llama-2-7b-Hotel-Booking-Model"):
    model = AutoModelForCausalLM.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    return model, tokenizer

@spaces.GPU
def load_pipeline():
    model, tokenizer = initialize_model_and_tokenizer()
    pipe = pipeline("text-generation", 
                    model= model, 
                    tokenizer = tokenizer,
                    max_new_tokens = 20,
                    top_k = 30, 
                    early_stopping=True,
                    num_beams = 2,
                    temperature = 0.1,
                    repetition_penalty = 1.03)

    llm = HuggingFacePipeline(pipeline = pipe)
    return llm

@spaces.GPU
def chat_interface(inputs):
    question = inputs
    chat_history_tuples = []
    for message in chat_history:
        chat_history_tuples.append((message[0], message[1]))
    
    #result = llm_chain({"input": query, "history": chat_history_tuples})
    result = llm_chain.predict(input = inputs)
    return result

llm = load_pipeline()
chat_history = []

template = """<<SYS>>
You are an AI having conversation with a human. Below is an instruction that describes a task. 
Write a response that appropriately completes the request.
Reply with the most helpful and logic answer. During the conversation you need to ask the user 
the following questions to complete the hotel booking task.
1) Where would you like to stay and when?
2) How many people are staying in the room?
3) Do you prefer any ammenities like breakfast included or gym?
4) What is your name, your email address and phone number? 
Make sure you receive a logical answer from the user from every question to complete the hotel 
booking process.
<</SYS>>
Previous conversation:
{history}
Human: {input}
AI:"""
prompt = PromptTemplate(template=template, input_variables=["history", "input"])

memory = ConversationBufferMemory(memory_key="history", llm = llm)
llm_chain = ConversationChain(prompt=prompt, llm=llm, memory = memory)

with gr.Blocks() as demo:
    gr.Markdown("Hotel Booking Assistant Chat 🤗")
    #chatbot = gr.Chatbot(label="Chat history")
    #message = gr.Textbox(label="Ask me a question!")
    #clear = gr.Button("Clear")
    #llm_chain, llm = init_chain(model, tokenizer)

    demo.chatbot_interface = gr.Interface(
        fn=chat_interface, 
        inputs=[
            gr.Textbox(lines=1, label="input")
        ], 
        outputs="text"
    )
demo.launch()