Andrey Vorozhko commited on
Commit
7ea8290
·
1 Parent(s): 4982e87

Correct using state in new gradio version

Browse files
Files changed (2) hide show
  1. app.py +8 -9
  2. requirements.txt +1 -2
app.py CHANGED
@@ -3,13 +3,13 @@ import gradio as gr
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from util_funcs import getLengthParam, calcAnswerLengthByProbability, cropContext
5
 
6
- def chat_function(Message): # model, tokenizer
7
 
8
  input_user = Message
9
 
10
- history = gr.get_state() or []
11
 
12
- chat_history_ids = torch.zeros((1, 0), dtype=torch.int) if history == [] else torch.tensor(history[-1][2], dtype=torch.long)
13
 
14
  # encode the new user input, add parameters and return a tensor in Pytorch
15
  lengthId = getLengthParam(input_user, tokenizer)
@@ -78,8 +78,7 @@ def chat_function(Message): # model, tokenizer
78
  # Случай когда надо перегенерировать ответ наступил, берем изначальный тензор
79
  chat_history_ids = chat_history_ids_initial
80
 
81
- history.append((input_user, answer, chat_history_ids.tolist()))
82
- gr.set_state(history)
83
  html = "<div class='chatbot'>"
84
  for user_msg, resp_msg, _ in history:
85
  if user_msg != '-':
@@ -87,7 +86,7 @@ def chat_function(Message): # model, tokenizer
87
  if resp_msg != '-':
88
  html += f"<div class='resp_msg'>{resp_msg}</div>"
89
  html += "</div>"
90
- return html
91
 
92
  # Download checkpoint:
93
 
@@ -106,8 +105,8 @@ description = """
106
  article = "<p style='text-align: center'><a href='https://huggingface.co/avorozhko/ruDialoGpt3-medium-finetuned-context'>Бот на основе дообученной GPT-3</a></p>"
107
 
108
  iface = gr.Interface(fn=chat_function,
109
- inputs=gr.inputs.Textbox(lines=3, placeholder="Что вы хотите сказать боту..."),
110
- outputs="html",
111
  title=title, description=description, article=article,
112
  theme='dark-grass',
113
  css= """
@@ -119,7 +118,7 @@ iface = gr.Interface(fn=chat_function,
119
  .panels.unaligned :last-child {order: -1 !important;}
120
  """,
121
  allow_screenshot=False,
122
- allow_flagging='auto'
123
  )
124
 
125
  if __name__ == "__main__":
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from util_funcs import getLengthParam, calcAnswerLengthByProbability, cropContext
5
 
6
+ def chat_function(Message, History): # model, tokenizer
7
 
8
  input_user = Message
9
 
10
+ History = History or []
11
 
12
+ chat_history_ids = torch.zeros((1, 0), dtype=torch.int) if History == [] else torch.tensor(history[-1][2], dtype=torch.long)
13
 
14
  # encode the new user input, add parameters and return a tensor in Pytorch
15
  lengthId = getLengthParam(input_user, tokenizer)
 
78
  # Случай когда надо перегенерировать ответ наступил, берем изначальный тензор
79
  chat_history_ids = chat_history_ids_initial
80
 
81
+ History.append((input_user, answer, chat_history_ids.tolist()))
 
82
  html = "<div class='chatbot'>"
83
  for user_msg, resp_msg, _ in history:
84
  if user_msg != '-':
 
86
  if resp_msg != '-':
87
  html += f"<div class='resp_msg'>{resp_msg}</div>"
88
  html += "</div>"
89
+ return html, History
90
 
91
  # Download checkpoint:
92
 
 
105
  article = "<p style='text-align: center'><a href='https://huggingface.co/avorozhko/ruDialoGpt3-medium-finetuned-context'>Бот на основе дообученной GPT-3</a></p>"
106
 
107
  iface = gr.Interface(fn=chat_function,
108
+ inputs=[gr.inputs.Textbox(lines=3, placeholder="Что вы хотите сказать боту..."), 'state'],
109
+ outputs=['html', 'state'],
110
  title=title, description=description, article=article,
111
  theme='dark-grass',
112
  css= """
 
118
  .panels.unaligned :last-child {order: -1 !important;}
119
  """,
120
  allow_screenshot=False,
121
+ allow_flagging='never'
122
  )
123
 
124
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1,3 +1,2 @@
1
  transformers
2
- torch
3
- gradio==2.7.0
 
1
  transformers
2
+ torch