File size: 1,984 Bytes
ac74a45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
description = """
<p>
<center>
This bot was trained on a dataset of 1000 movie reviews from IMDB. It can suggest movies similar to the one you liked!
<img src="https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot/resolve/main/img/rick.png" alt="rick" width="200"/>
</center>
</p>
"""
model = pipeline("text-generation", model="charoori/llm4movies")

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

base_model_id = "mistralai/Mistral-7B-v0.1"
tokenizer = AutoTokenizer.from_pretrained(
    base_model_id,
    add_bos_token=True,
)
model = AutoModelForCausalLM.from_pretrained("charoori/llm4movies")

def predict(input, history=[]):
    # tokenize the new input sentence
    new_user_input_ids = tokenizer.encode(input, return_tensors='pt')

    # append the new user input tokens to the chat history
    bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)

    # generate a response 
    # model_input = tokenizer(eval_prompt, return_tensors="pt")
    history = model.generate(bot_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id).tolist()

    # convert the tokens to text, and then split the responses into lines
    response = tokenizer.decode(model.generate(**bot_input_ids, max_new_tokens=256, repetition_penalty=1.15)[0], skip_special_tokens=True)

    # response = tokenizer.decode(history[0]).split("<|endoftext|>")
    #print('decoded_response-->>'+str(response))
    response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)]  # convert to tuples of list
    #print('response-->>'+str(response))
    return response, history


interface = gr.Interface(
    fn=predict,
	title = "Find your next movie!",
    inputs="textbox",
    outputs="text",
    description=description,
    examples=[["I liked the movie Matrix because it was very interesting and had a great story. Suggest something similar"]]
)
interface.launch()