leonardlin commited on
Commit
764f34e
β€’
1 Parent(s): ba913e7

mostly working

Browse files
Files changed (1) hide show
  1. app.py +62 -8
app.py CHANGED
@@ -1,12 +1,66 @@
 
1
  import gradio as gr
 
2
 
3
- description = "Story generation with GPT-2"
4
- title = "Generate your own story"
5
- examples = [["Adventurer is approached by a mysterious stranger in the tavern for a new quest."]]
6
 
7
- interface = gr.Interface.load("huggingface/pranavpsv/gpt2-genre-story-generator",
8
- description=description,
9
- examples=examples
10
- )
 
 
 
 
 
 
11
 
12
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import gradio as gr
3
+ import torch
4
 
5
+ model = "mistralai/Mistral-7B-Instruct-v0.1"
6
+ model = "TinyLlama/TinyLlama-1.1B-Chat-v0.3"
 
7
 
8
+ # Gradio
9
+ title = "Shisa 7B"
10
+ description = "Test out Shisa 7B in either English or Japanese."
11
+ placeholder = "Type Here / ここにε…₯εŠ›γ—γ¦γγ γ•γ„"
12
+ examples = [
13
+ "Hello, how are you?",
14
+ "γ“γ‚“γ«γ‘γ―γ€ε…ƒζ°—γ§γ™γ‹οΌŸ",
15
+ "γŠγ£γ™γ€ε…ƒζ°—οΌŸ",
16
+ "γ“γ‚“γ«γ‘γ―γ€γ„γ‹γŒγŠιŽγ”γ—γ§γ™γ‹οΌŸ",
17
+ ]
18
 
19
+ tokenizer = AutoTokenizer.from_pretrained(model)
20
+ model = AutoModelForCausalLM.from_pretrained(model)
21
+
22
+ def chat(input, history=[]):
23
+ input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
24
+ history = model.generate(
25
+ input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
26
+ ).tolist()
27
+
28
+ # convert the tokens to text, and then split the responses into lines
29
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
30
+ '''
31
+ # tokenize the new input sentence
32
+ new_user_input_ids = tokenizer.encode(
33
+ input + tokenizer.eos_token, return_tensors="pt"
34
+ )
35
+
36
+ # append the new user input tokens to the chat history
37
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
38
+
39
+ # generate a response
40
+ history = model.generate(
41
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
42
+ ).tolist()
43
+
44
+ # convert the tokens to text, and then split the responses into lines
45
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
46
+ # print('decoded_response-->>'+str(response))
47
+ response = [
48
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
49
+ ] # convert to tuples of list
50
+ # print('response-->>'+str(response))
51
+ '''
52
+ return response, history
53
+
54
+
55
+ gr.ChatInterface(
56
+ chat,
57
+ chatbot=gr.Chatbot(height=400),
58
+ textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
59
+ title=title,
60
+ description=description,
61
+ theme="soft",
62
+ examples=examples,
63
+ cache_examples=False,
64
+ undo_btn="Delete Previous",
65
+ clear_btn="Clear",
66
+ ).queue().launch()