leonardlin commited on
Commit
5835e21
β€’
1 Parent(s): bc897bf

switch to pipelines

Browse files
Files changed (1) hide show
  1. app.py +47 -52
app.py CHANGED
@@ -1,28 +1,11 @@
1
  # https://www.gradio.app/guides/using-hugging-face-integrations
2
 
3
- from transformers import pipeline
4
  import gradio as gr
5
-
6
- model = "TinyLlama/TinyLlama-1.1B-Chat-v0.3"
7
-
8
- pipe = pipeline("conversational", model=model)
9
- demo = gr.Interface.from_pipeline(pipe)
10
- demo.launch()
11
-
12
- '''
13
- demo = gr.Interface.load(model)
14
- '''
15
-
16
-
17
- """
18
- from transformers import AutoModelForCausalLM, AutoTokenizer
19
- import gradio as gr
20
- import torch
21
 
22
  model = "mistralai/Mistral-7B-Instruct-v0.1"
23
  model = "TinyLlama/TinyLlama-1.1B-Chat-v0.3"
24
 
25
- # Gradio
26
  title = "Shisa 7B"
27
  description = "Test out Shisa 7B in either English or Japanese."
28
  placeholder = "Type Here / ここにε…₯εŠ›γ—γ¦γγ γ•γ„"
@@ -33,42 +16,28 @@ examples = [
33
  "γ“γ‚“γ«γ‘γ―γ€γ„γ‹γŒγŠιŽγ”γ—γ§γ™γ‹οΌŸ",
34
  ]
35
 
36
- tokenizer = AutoTokenizer.from_pretrained(model)
37
- model = AutoModelForCausalLM.from_pretrained(model)
 
 
 
 
 
 
 
38
 
39
  def chat(input, history=[]):
40
- input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
41
- history = model.generate(
42
- input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
43
- ).tolist()
44
-
45
- # convert the tokens to text, and then split the responses into lines
46
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
47
- '''
48
- # tokenize the new input sentence
49
- new_user_input_ids = tokenizer.encode(
50
- input + tokenizer.eos_token, return_tensors="pt"
51
- )
52
-
53
- # append the new user input tokens to the chat history
54
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
55
-
56
- # generate a response
57
- history = model.generate(
58
- bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
59
- ).tolist()
60
-
61
- # convert the tokens to text, and then split the responses into lines
62
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
63
- # print('decoded_response-->>'+str(response))
64
- response = [
65
- (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
66
- ] # convert to tuples of list
67
- # print('response-->>'+str(response))
68
- '''
69
  return response, history
70
 
71
-
72
  gr.ChatInterface(
73
  chat,
74
  chatbot=gr.Chatbot(height=400),
@@ -80,5 +49,31 @@ gr.ChatInterface(
80
  cache_examples=False,
81
  undo_btn="Delete Previous",
82
  clear_btn="Clear",
83
- ).queue().launch()
84
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # https://www.gradio.app/guides/using-hugging-face-integrations
2
 
 
3
  import gradio as gr
4
+ from transformers import pipeline, Conversation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  model = "mistralai/Mistral-7B-Instruct-v0.1"
7
  model = "TinyLlama/TinyLlama-1.1B-Chat-v0.3"
8
 
 
9
  title = "Shisa 7B"
10
  description = "Test out Shisa 7B in either English or Japanese."
11
  placeholder = "Type Here / ここにε…₯εŠ›γ—γ¦γγ γ•γ„"
 
16
  "γ“γ‚“γ«γ‘γ―γ€γ„γ‹γŒγŠιŽγ”γ—γ§γ™γ‹οΌŸ",
17
  ]
18
 
19
+ # Docs: https://github.com/huggingface/transformers/blob/main/src/transformers/pipelines/conversational.py
20
+ conversation = Conversation()
21
+ chatbot = pipeline('conversational', model)
22
+ '''
23
+ conversation = Conversation("Going to the movies tonight - any suggestions?")
24
+ conversation.add_message({"role": "assistant", "content": "The Big lebowski."})
25
+ conversation.add_message({"role": "user", "content": "Is it good?"})
26
+ conversation.messages[:-1]
27
+ '''
28
 
29
  def chat(input, history=[]):
30
+ conversation.add_message({"role": "user", "content": input})
31
+ # we do this shuffle so local shadow response doesn't get created
32
+ response_conversation = chatbot(conversation)
33
+ print(response_conversation)
34
+ print(response_conversation.messages)
35
+ print(response_conversation.messages[-1]["content"])
36
+
37
+ conversation.add_message(response_conversation.messages[-1])
38
+ response = conversation.messages[-1]["content"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  return response, history
40
 
 
41
  gr.ChatInterface(
42
  chat,
43
  chatbot=gr.Chatbot(height=400),
 
49
  cache_examples=False,
50
  undo_btn="Delete Previous",
51
  clear_btn="Clear",
52
+ ).launch()
53
+
54
+ '''
55
+ gr.Interface.load(
56
+ "EleutherAI/gpt-j-6B",
57
+ inputs=gr.Textbox(lines=5, label="Input Text"),
58
+ title=title,
59
+ description=description,
60
+ article=article,
61
+ ).launch()
62
+
63
+
64
+ # Doesn't support conversational pipelin
65
+ pipe = pipeline('conversational', model)
66
+ gr.Interface.from_pipeline(pipe).launch()
67
+
68
+
69
+ '''
70
+
71
+
72
+ # For async
73
+ # ).queue().launch()
74
+
75
+ '''
76
+ # Pipeline doesn't support conversational...
77
+ pipe = pipeline("conversational", model=model)
78
+ demo = gr.Interface.from_pipeline(pipe)
79
+ '''