Martin Vlach commited on
Commit
c415fa7
β€’
1 Parent(s): 4bf07e3

change code of app to match the MC GHub

Browse files
Files changed (2) hide show
  1. app.py +64 -46
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,53 +1,71 @@
1
- import gradio as gr
2
- import argparse
3
  import os
4
- import json
5
- from vllm import LLM, SamplingParams
 
 
6
 
7
 
8
- def parse_args():
9
- parser = argparse.ArgumentParser()
10
- parser.add_argument("--base_model", type=str) # model path
11
- parser.add_argument("--n_gpus", type=int, default=1) # n_gpu
12
- return parser.parse_args()
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- def predict(message, history, system_prompt, temperature, max_tokens):
15
- instruction = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. "
16
- for human, assistant in history:
17
- instruction += 'USER: '+ human + ' ASSISTANT: '+ assistant + '</s>'
18
- instruction += 'USER: '+ message + ' ASSISTANT:'
19
- problem = [instruction]
20
- stop_tokens = ["USER:", "USER", "ASSISTANT:", "ASSISTANT"]
21
- sampling_params = SamplingParams(temperature=temperature, top_p=1, max_tokens=max_tokens, stop=stop_tokens)
22
- completions = llm.generate(problem, sampling_params)
23
- for output in completions:
24
- prompt = output.prompt
25
- print('==========================question=============================')
26
- print(prompt)
27
- generated_text = output.outputs[0].text
28
- print('===========================answer=============================')
29
- print(generated_text)
30
- for idx in range(len(generated_text)):
31
- yield generated_text[:idx+1]
32
 
 
 
 
33
 
34
- if __name__ == "__main__":
35
- args = parse_args()
36
- llm = LLM(model=args.base_model, tensor_parallel_size=args.n_gpus)
37
- gr.ChatInterface(
38
- predict,
39
- title="LLM playground - WizardLM-13B-V1.2",
40
- description="This is a LLM playground for WizardLM-13B-V1.2, github: https://github.com/nlpxucan/WizardLM, huggingface: https://huggingface.co/WizardLM",
41
- theme="soft",
42
- chatbot=gr.Chatbot(height=1400, label="Chat History",),
43
- textbox=gr.Textbox(placeholder="input", container=False, scale=7),
44
- retry_btn=None,
45
- undo_btn="Delete Previous",
46
- clear_btn="Clear",
47
- additional_inputs=[
48
- gr.Textbox("A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.", label="System Prompt"),
49
- gr.Slider(0, 1, 0.9, label="Temperature"),
50
- gr.Slider(100, 2048, 1024, label="Max Tokens"),
 
 
 
 
 
 
51
  ],
52
- additional_inputs_accordion_name="Parameters",
53
- ).queue().launch(share=False, server_port=7870)
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer
2
+ import transformers
3
  import os
4
+ import sys
5
+ import fire
6
+ import torch
7
+ import gradio as gr
8
 
9
 
10
+ def main(
11
+ base_model="ise-uiuc/Magicoder-S-DS-6.7B",
12
+ device="cuda:0",
13
+ port=8080,
14
+ ):
15
+ tokenizer = AutoTokenizer.from_pretrained(base_model)
16
+ pipeline = transformers.pipeline(
17
+ "text-generation",
18
+ model=base_model,
19
+ torch_dtype=torch.float16,
20
+ device=device
21
+ )
22
+ def evaluate_magicoder(
23
+ instruction,
24
+ temperature=1,
25
+ max_new_tokens=2048,
26
+ ):
27
+ MAGICODER_PROMPT = """You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions.
28
 
29
+ @@ Instruction
30
+ {instruction}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ @@ Response
33
+ """
34
+ prompt = MAGICODER_PROMPT.format(instruction=instruction)
35
 
36
+ sequences = pipeline(
37
+ prompt,
38
+ temperature=temperature,
39
+ max_new_tokens=max_new_tokens,
40
+ )
41
+ for seq in sequences:
42
+ print('==========================question=============================')
43
+ print(prompt)
44
+ generated_text = seq['generated_text'].replace(prompt, "")
45
+ print('===========================answer=============================')
46
+ print(generated_text)
47
+ return generated_text
48
+
49
+ gr.Interface(
50
+ fn=evaluate_magicoder,
51
+ inputs=[
52
+ gr.components.Textbox(
53
+ lines=3, label="Instruction", placeholder="Anything you want to ask Magicoder ?"
54
+ ),
55
+ gr.components.Slider(minimum=0, maximum=1, value=0, label="Temperature"),
56
+ gr.components.Slider(
57
+ minimum=1, maximum=2048, step=1, value=128, label="Max tokens"
58
+ ),
59
  ],
60
+ outputs=[
61
+ gr.components.Textbox(
62
+ lines=30,
63
+ label="Output",
64
+ )
65
+ ],
66
+ title="Magicoder",
67
+ description="This is a LLM playground for Magicoder! Follow us on Github: https://github.com/ise-uiuc/magicoder and Huggingface: https://huggingface.co/ise-uiuc."
68
+ ).queue().launch(share=True, server_port=port)
69
+
70
+ if __name__ == "__main__":
71
+ fire.Fire(main)
requirements.txt CHANGED
@@ -1 +1 @@
1
- vllm
 
1
+ fire