gba16326553 commited on
Commit
7b189ae
·
verified ·
1 Parent(s): ac6325b

Create app.py

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