charoori commited on
Commit
ac74a45
1 Parent(s): 358041f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ description = """
4
+ <p>
5
+ <center>
6
+ This bot was trained on a dataset of 1000 movie reviews from IMDB. It can suggest movies similar to the one you liked!
7
+ <img src="https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot/resolve/main/img/rick.png" alt="rick" width="200"/>
8
+ </center>
9
+ </p>
10
+ """
11
+ model = pipeline("text-generation", model="charoori/llm4movies")
12
+
13
+ from transformers import AutoModelForCausalLM, AutoTokenizer
14
+ import torch
15
+
16
+ base_model_id = "mistralai/Mistral-7B-v0.1"
17
+ tokenizer = AutoTokenizer.from_pretrained(
18
+ base_model_id,
19
+ add_bos_token=True,
20
+ )
21
+ model = AutoModelForCausalLM.from_pretrained("charoori/llm4movies")
22
+
23
+ def predict(input, history=[]):
24
+ # tokenize the new input sentence
25
+ new_user_input_ids = tokenizer.encode(input, return_tensors='pt')
26
+
27
+ # append the new user input tokens to the chat history
28
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
29
+
30
+ # generate a response
31
+ # model_input = tokenizer(eval_prompt, return_tensors="pt")
32
+ history = model.generate(bot_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id).tolist()
33
+
34
+ # convert the tokens to text, and then split the responses into lines
35
+ response = tokenizer.decode(model.generate(**bot_input_ids, max_new_tokens=256, repetition_penalty=1.15)[0], skip_special_tokens=True)
36
+
37
+ # response = tokenizer.decode(history[0]).split("<|endoftext|>")
38
+ #print('decoded_response-->>'+str(response))
39
+ response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
40
+ #print('response-->>'+str(response))
41
+ return response, history
42
+
43
+
44
+ interface = gr.Interface(
45
+ fn=predict,
46
+ title = "Find your next movie!",
47
+ inputs="textbox",
48
+ outputs="text",
49
+ description=description,
50
+ examples=[["I liked the movie Matrix because it was very interesting and had a great story. Suggest something similar"]]
51
+ )
52
+ interface.launch()
53
+