Nihal Nayak commited on
Commit
4d92f8a
1 Parent(s): 56f924f

instruction response pair

Browse files
Files changed (1) hide show
  1. app.py +13 -41
app.py CHANGED
@@ -6,7 +6,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
6
  """
7
  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
8
  """
9
- # client = InferenceClient("BatsResearch/bonito-v1")
10
  model = AutoModelForCausalLM.from_pretrained("BatsResearch/bonito-v1")
11
  tokenizer = AutoTokenizer.from_pretrained("BatsResearch/bonito-v1")
12
  model.to("cuda")
@@ -31,49 +30,22 @@ def respond(
31
  top_p=top_p,
32
  do_sample=True,
33
  )
 
34
 
35
- response = tokenizer.decode(output[0], skip_special_tokens=True)
36
 
37
- # response = client.text_generation(input_text, max_new_tokens=max_tokens, temperature=temperature, top_p=top_p)
 
 
 
 
 
 
 
 
38
 
39
- return response
40
- # messages = []
41
- # messages.append({"role": "user", "content": message})
42
 
43
- # response = ""
44
-
45
-
46
- # for message in client.text_generation(
47
- # messages,
48
- # max_tokens=max_tokens,
49
- # stream=True,
50
- # temperature=temperature,
51
- # top_p=top_p,
52
- # ):
53
- # token = message.choices[0].delta.content
54
-
55
- # response += token
56
- # yield response
57
-
58
-
59
- """
60
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
61
- """
62
- # demo = gr.ChatInterface(
63
- # respond,
64
- # additional_inputs=[
65
- # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
66
- # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
67
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
68
- # gr.Slider(
69
- # minimum=0.1,
70
- # maximum=1.0,
71
- # value=0.95,
72
- # step=0.05,
73
- # label="Top-p (nucleus sampling)",
74
- # ),
75
- # ],
76
- # )
77
  task_types = [
78
  "extractive question answering",
79
  "multiple-choice question answering",
@@ -101,7 +73,7 @@ demo = gr.Interface(
101
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
102
  gr.Dropdown(task_types, label="Task type"),
103
  ],
104
- outputs=gr.Textbox(label="Response"),
105
  additional_inputs=[
106
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
107
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
 
6
  """
7
  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
8
  """
 
9
  model = AutoModelForCausalLM.from_pretrained("BatsResearch/bonito-v1")
10
  tokenizer = AutoTokenizer.from_pretrained("BatsResearch/bonito-v1")
11
  model.to("cuda")
 
30
  top_p=top_p,
31
  do_sample=True,
32
  )
33
+ pred_start = int(input_ids.shape[-1])
34
 
35
+ response = tokenizer.decode(output[0][pred_start:], skip_special_tokens=True)
36
 
37
+ # check if <|pipe|> is in the response
38
+ if "<|pipe|>" in response:
39
+ pair = response.split("<|pipe|>")
40
+ instruction = pair[0].strip().replace("{{context}}", message)
41
+ response = pair[1].strip()
42
+ else:
43
+ # fallback
44
+ instruction = pair[0].strip().replace("{{context}}", message)
45
+ response = "Unable to generate response. Please regenerate."
46
 
47
+ return instruction, response
 
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  task_types = [
50
  "extractive question answering",
51
  "multiple-choice question answering",
 
73
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
74
  gr.Dropdown(task_types, label="Task type"),
75
  ],
76
+ outputs=[gr.Textbox(label="Input"), gr.Textbox(label="Output")],
77
  additional_inputs=[
78
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
79
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),