llm4movies / app.py
charoori's picture
Upload app.py
ac74a45
raw
history blame
1.98 kB
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()