harpreetsahota commited on
Commit
f590120
Β·
verified Β·
1 Parent(s): 0ab46f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -84
app.py CHANGED
@@ -1,69 +1,28 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
 
3
 
4
- description = """# <p style="text-align: center; color: #292b47;"> 🏎️ <span style='color: #3264ff;'>DeciCoder-6B:</span> A Fast Code Generation ModelπŸ’¨ </p>
5
- <span style='color: #292b47;'>Welcome to <a href="https://huggingface.co/Deci/DeciCoder-6B" style="color: #3264ff;">DeciCoder</a>!
6
- DeciCoder-6B was trained on the Python, Java, Javascript, Rust, C++, C, and C# subset of the Starcoder Training Dataset, and it's released under the Apache 2.0 license. This model is capable of code-completion and instruction following. It surpasses CodeGen 2.5 7B, CodeLlama 7B, abd StarCoder 7B in its supported languages on HumanEval, and leads by 3 points in Python over StarCoderBase 15.5B."""
 
 
 
7
 
8
- GENERATION_TITLE= "<p style='font-size: 24px; color: #292b47;'>πŸ’» Your generated code:</p>"
9
 
10
- def instantiate_huggingface_model(
11
- model_name,
12
- quantization_config=None,
13
- device_map="auto",
14
- use_cache=True,
15
- trust_remote_code=None,
16
- pad_token=None,
17
- padding_side="left"
18
- ):
19
- """
20
- Instantiate a HuggingFace model with optional quantization using the BitsAndBytes library.
21
-
22
- Parameters:
23
- - model_name (str): The name of the model to load from HuggingFace's model hub.
24
- - quantization_config (BitsAndBytesConfig, optional): Configuration for model quantization.
25
- If None, defaults to a pre-defined quantization configuration for 4-bit quantization.
26
- - device_map (str, optional): Device placement strategy for model layers ('auto' by default).
27
- - use_cache (bool, optional): Whether to cache model outputs (False by default).
28
- - trust_remote_code (bool, optional): Whether to trust remote code for custom layers (True by default).
29
- - pad_token (str, optional): The pad token to be used by the tokenizer. If None, uses the EOS token.
30
- - padding_side (str, optional): The side on which to pad the sequences ('left' by default).
31
-
32
- Returns:
33
- - model (PreTrainedModel): The instantiated model ready for inference or fine-tuning.
34
- - tokenizer (PreTrainedTokenizer): The tokenizer associated with the model.
35
-
36
- The function will throw an exception if model loading fails.
37
- """
38
-
39
- # If quantization_config is not provided, use the default configuration
40
- if quantization_config is None:
41
- quantization_config = BitsAndBytesConfig(
42
- load_in_8bit=True,
43
- low_cpu_mem_usage=True,
44
- )
45
-
46
- model = AutoModelForCausalLM.from_pretrained(
47
- model_name,
48
- quantization_config=quantization_config,
49
- device_map=device_map,
50
- use_cache=use_cache,
51
- trust_remote_code=trust_remote_code
52
- )
53
 
54
- tokenizer = AutoTokenizer.from_pretrained(model_name,
55
- trust_remote_code=trust_remote_code)
 
 
 
56
 
57
- if pad_token is not None:
58
- tokenizer.pad_token = pad_token
59
- else:
60
- tokenizer.pad_token = tokenizer.eos_token
61
-
62
- tokenizer.padding_side = padding_side
63
 
64
- return model, tokenizer
65
 
66
- model, tokenizer = instantiate_huggingface_model("Deci-early-access/DeciCoder-6B", trust_remote_code=True)
67
 
68
  pipe = pipeline("text-generation",
69
  model=model,
@@ -73,22 +32,6 @@ pipe = pipeline("text-generation",
73
  temperature=1e-3,
74
  )
75
 
76
- def post_processing(prompt: str, completion: str) -> str:
77
- """
78
- Post-processes the generated code completion with HTML styling.
79
-
80
- Args:
81
- prompt (str): The input code prompt.
82
- completion (str): The generated code completion.
83
-
84
- Returns:
85
- str: The HTML-styled code with prompt and completion.
86
- """
87
- completion = "<span style='color: #ff5b86;'>" + completion + "</span>"
88
- prompt = "<span style='color: #7484b7;'>" + prompt + "</span>"
89
- code_html = f"<br><hr><br><pre style='font-size: 12px'><code>{prompt}{completion}</code></pre><br><hr>"
90
- return GENERATION_TITLE + code_html
91
-
92
  def code_generation(prompt: str) -> str:
93
  """
94
  Generates code based on the given prompt. Handles both regular and FIM (Fill-In-Missing) generation.
@@ -98,25 +41,25 @@ def code_generation(prompt: str) -> str:
98
 
99
  Returns:
100
  str: The HTML-styled generated code.
101
- """
102
  completion = pipe(prompt)[0]['generated_text']
103
- completion = completion[len(prompt):]
104
- return post_processing(prompt, completion)
105
 
106
  demo = gr.Blocks(
107
  css=".gradio-container {background-color: #FAFBFF; color: #292b47}"
108
  )
109
  with demo:
 
 
 
 
110
  with gr.Row():
111
- _, colum_2, _ = gr.Column(scale=1), gr.Column(scale=6), gr.Column(scale=1)
112
- with colum_2:
113
- gr.Markdown(value=description)
114
- code = gr.Code(lines=5, language="python", label="Input code", value="def nth_element_in_fibonnaci(element):\n \"\"\"Returns the nth element of the Fibonnaci sequence.\"\"\"")
115
- run = gr.Button(value="πŸ‘¨πŸ½β€πŸ’» Generate code", size='lg')
116
- output = gr.HTML(label="πŸ’» Your generated code")
117
-
118
 
 
119
  event = run.click(code_generation, [code], output)
120
  gr.HTML(label="Keep in touch", value="<img src='https://huggingface.co/spaces/Deci/DeciCoder-Demo/resolve/main/deci-coder-banner.png' alt='Keep in touch' style='display: block; color: #292b47; margin: auto; max-width: 800px;'>")
121
 
122
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
3
+ from threading import Thread
4
 
5
+ description = """
6
+ <p style="text-align: center; font-size: 24px; color: #292b47;">
7
+ <strong>πŸš€ <span style='color: #3264ff;'>DeciCoder-6B: Bigger, Faster, Stronger </span></strong>
8
+ </p>
9
+ <span style='color: #292b47;'>Welcome to the <a href="https://huggingface.co/Deci/DeciCoder-6B" style="color: #3264ff;">DeciCoder-6B playground</a>! DeciCoder-6B was trained on the Python, Java, Javascript, Rust, C++, C, and C# subset of the Starcoder Training Dataset, and it's released under the Apache 2.0 license. This model is capable of code-completion and instruction following. It surpasses CodeGen 2.5 7B, CodeLlama 7B, abd StarCoder 7B in its supported languages on HumanEval, and leads by 3 points in Python over StarCoderBase 15.5B.</span>
10
+ """
11
 
 
12
 
13
+ checkpoint = "Deci-early-access/DeciCoder-6B"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ model = AutoModelForCausalLM.from_pretrained(checkpoint,
16
+ trust_remote_code=True,
17
+ device_map="auto",
18
+ low_cpu_mem_usage=True,
19
+ load_in_4bit=True)
20
 
21
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True)
 
 
 
 
 
22
 
23
+ tokenizer.pad_token = tokenizer.eos_token
24
 
25
+ tokenizer.padding_side = "left"
26
 
27
  pipe = pipeline("text-generation",
28
  model=model,
 
32
  temperature=1e-3,
33
  )
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def code_generation(prompt: str) -> str:
36
  """
37
  Generates code based on the given prompt. Handles both regular and FIM (Fill-In-Missing) generation.
 
41
 
42
  Returns:
43
  str: The HTML-styled generated code.
44
+ """
45
  completion = pipe(prompt)[0]['generated_text']
46
+ return completion
47
+
48
 
49
  demo = gr.Blocks(
50
  css=".gradio-container {background-color: #FAFBFF; color: #292b47}"
51
  )
52
  with demo:
53
+ gr.Markdown(value=description)
54
+ with gr.Row():
55
+ code = gr.Code(lines=10, language="python", label="πŸ‘¨πŸ½β€πŸ’» Input", value="def nth_element_in_fibonnaci(element):\n \"\"\"Returns the nth element of the Fibonnaci sequence.\"\"\"")
56
+ output = gr.Code(label="πŸ’» Generated code")
57
  with gr.Row():
58
+ run = gr.Button(value="πŸ‘¨πŸ½β€πŸ’» Generate code")
59
+ clear = gr.Button("πŸ—‘οΈ Clear")
 
 
 
 
 
60
 
61
+ clear.click(lambda: (None, None), None, [code, output], queue=False)
62
  event = run.click(code_generation, [code], output)
63
  gr.HTML(label="Keep in touch", value="<img src='https://huggingface.co/spaces/Deci/DeciCoder-Demo/resolve/main/deci-coder-banner.png' alt='Keep in touch' style='display: block; color: #292b47; margin: auto; max-width: 800px;'>")
64
 
65
+ demo.launch(debug=True)