ColeGuion commited on
Commit
5c8be37
1 Parent(s): b5ee5bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -1,20 +1,31 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- #from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
 
5
  """
6
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
  """
8
  #client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
- client = InferenceClient("vennify/t5-base-grammar-correction")
10
  #gr.load("models/vennify/t5-base-grammar-correction").launch()
11
 
12
  # Load the model and tokenizer
13
- #model_name = "vennify/t5-base-grammar-correction"
14
- #model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
- #tokenizer = AutoTokenizer.from_pretrained(model_name)
16
-
17
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def respond(
@@ -22,32 +33,22 @@ def respond(
22
  history: list[tuple[str, str]],
23
  system_message,
24
  max_tokens,
 
25
  temperature,
26
  top_p,
27
  ):
28
- messages = [{"role": "system", "content": system_message}]
29
-
30
- for val in history:
31
- if val[0]:
32
- messages.append({"role": "user", "content": val[0]})
33
- if val[1]:
34
- messages.append({"role": "assistant", "content": val[1]})
35
 
36
- messages.append({"role": "user", "content": message})
 
 
 
 
37
 
38
- response = ""
39
-
40
- for message in client.chat_completion(
41
- messages,
42
- max_tokens=max_tokens,
43
- stream=True,
44
- temperature=temperature,
45
- top_p=top_p,
46
- ):
47
- token = message.choices[0].delta.content
48
 
49
- response += token
50
- yield response
51
 
52
  """
53
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -56,9 +57,10 @@ demo = gr.ChatInterface(
56
  respond,
57
  additional_inputs=[
58
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
59
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
60
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
62
  ],
63
  )
64
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
 
5
  """
6
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
  """
8
  #client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
+ #client = InferenceClient("vennify/t5-base-grammar-correction")
10
  #gr.load("models/vennify/t5-base-grammar-correction").launch()
11
 
12
  # Load the model and tokenizer
13
+ model_name = "vennify/t5-base-grammar-correction"
14
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
16
 
17
+ def correct_text(text, max_length, num_beams, temperature, top_p):
18
+ inputs = tokenizer.encode(text, return_tensors="pt")
19
+ outputs = model.generate(
20
+ inputs,
21
+ max_length=max_length,
22
+ num_beams=num_beams,
23
+ temperature=temperature,
24
+ top_p=top_p,
25
+ early_stopping=True
26
+ )
27
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+ return corrected_text
29
 
30
 
31
  def respond(
 
33
  history: list[tuple[str, str]],
34
  system_message,
35
  max_tokens,
36
+ num_beams,
37
  temperature,
38
  top_p,
39
  ):
40
+ #messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
41
 
42
+ #for val in history:
43
+ # if val[0]:
44
+ # messages.append({"role": "user", "content": val[0]})
45
+ # if val[1]:
46
+ # messages.append({"role": "assistant", "content": val[1]})
47
 
48
+ #messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
49
 
50
+ response = correct_text(message, max_tokens, num_beams, temperature, top_p)
51
+ yield response
52
 
53
  """
54
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
57
  respond,
58
  additional_inputs=[
59
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
60
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
61
+ gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Num Beams"),
62
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
63
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
64
  ],
65
  )
66