acarrillo48 commited on
Commit
41106d5
·
verified ·
1 Parent(s): 318a674

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+
5
+
6
+ title = "Falcon ChatBot"
7
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
8
+ examples = [["How are you?"]]
9
+ model_name = "ybelkada/falcon-7b-sharded-bf16"
10
+
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForCausalLM.from_pretrained(model_name)
14
+
15
+
16
+ def predict(input, history=[]):
17
+ # tokenize the new input sentence
18
+ new_user_input_ids = tokenizer.encode(
19
+ input + tokenizer.eos_token, return_tensors="pt"
20
+ )
21
+
22
+ # append the new user input tokens to the chat history
23
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
24
+
25
+ # generate a response
26
+ history = model.generate(
27
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
28
+ ).tolist()
29
+
30
+ # convert the tokens to text, and then split the responses into lines
31
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
32
+ # print('decoded_response-->>'+str(response))
33
+ response = [
34
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
35
+ ] # convert to tuples of list
36
+ # print('response-->>'+str(response))
37
+ return response, history
38
+
39
+
40
+ gr.Interface(
41
+ fn=predict,
42
+ title=title,
43
+ description=description,
44
+ examples=examples,
45
+ inputs=["text", "state"],
46
+ outputs=["chatbot", "state"],
47
+ theme="finlaymacklon/boxy_violet",
48
+ ).launch()