3v324v23 commited on
Commit
5e467f7
1 Parent(s): fdd01f4

Add application file

Browse files
Files changed (1) hide show
  1. app.py +122 -4
app.py CHANGED
@@ -1,7 +1,125 @@
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
 
4
+ FIM_PREFIX = "<fim_prefix>"
5
+ FIM_MIDDLE = "<fim_middle>"
6
+ FIM_SUFFIX = "<fim_suffix>"
7
+ FIM_INDICATOR = "<FILL_HERE>"
8
 
9
+ API_URL_BASE ="https://api-inference.huggingface.co/models/bigcode/starcoderbase"
10
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
11
+
12
+ theme = gr.themes.Monochrome(
13
+ primary_hue="indigo",
14
+ secondary_hue="blue",
15
+ neutral_hue="slate",
16
+ radius_size=gr.themes.sizes.radius_sm,
17
+ font=[
18
+ gr.themes.GoogleFont("Open Sans"),
19
+ "ui-sans-serif",
20
+ "system-ui",
21
+ "sans-serif",
22
+ ],
23
+ )
24
+ css = ".generating {visibility: hidden}"
25
+
26
+ monospace_css = """
27
+ #q-input textarea {
28
+ font-family: monospace, 'Consolas', Courier, monospace;
29
+ }
30
+ """
31
+
32
+
33
+ css += monospace_css + ".gradio-container {color: black}"
34
+
35
+ description = """
36
+ <div style="text-align: center;">
37
+ <p>This is a demo to generate code with <a href="https://huggingface.co/bigcode/starcoder" style='color: #e6b800;'>StarCoder</a></p>
38
+ </div>
39
+ """
40
+
41
+ examples = [
42
+ "X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.1)\n\n# Train a logistic regression model, predict the labels on the test set and compute the accuracy score",
43
+ "// Returns every other value in the array as a new array.\nfunction everyOther(arr) {",
44
+ "def alternating(list1, list2):\n results = []\n for i in range(min(len(list1), len(list2))):\n results.append(list1[i])\n results.append(list2[i])\n if len(list1) > len(list2):\n <FILL_HERE>\n else:\n results.extend(list2[i+1:])\n return results",
45
+ ]
46
+
47
+ client_base = Client(
48
+ API_URL_BASE, headers={"Authorization": f"Bearer {HF_TOKEN}"},
49
+ )
50
+
51
+ def generate(
52
+ prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, version="StarCoder",
53
+ ):
54
+
55
+ temperature = float(temperature)
56
+ if temperature < 1e-2:
57
+ temperature = 1e-2
58
+ top_p = float(top_p)
59
+ fim_mode = False
60
+
61
+ generate_kwargs = dict(
62
+ temperature=temperature,
63
+ max_new_tokens=max_new_tokens,
64
+ top_p=top_p,
65
+ repetition_penalty=repetition_penalty,
66
+ do_sample=True,
67
+ seed=42,
68
+ )
69
+
70
+ if FIM_INDICATOR in prompt:
71
+ fim_mode = True
72
+ try:
73
+ prefix, suffix = prompt.split(FIM_INDICATOR)
74
+ except:
75
+ raise ValueError(f"Only one {FIM_INDICATOR} allowed in prompt!")
76
+ prompt = f"{FIM_PREFIX}{prefix}{FIM_SUFFIX}{suffix}{FIM_MIDDLE}"
77
+
78
+ if version == "StarCoder":
79
+ stream = client.generate_stream(prompt, **generate_kwargs)
80
+ else:
81
+ stream = client_base.generate_stream(prompt, **generate_kwargs)
82
+
83
+ if fim_mode:
84
+ output = prefix
85
+ else:
86
+ output = prompt
87
+
88
+ previous_token = ""
89
+ for response in stream:
90
+ if response.token.text == "<|endoftext|>":
91
+ if fim_mode:
92
+ output += suffix
93
+ else:
94
+ return output
95
+ else:
96
+ output += response.token.text
97
+ previous_token = response.token.text
98
+ yield output
99
+ return output
100
+ def process_example(args):
101
+ for x in generate(args):
102
+ pass
103
+ return x
104
+
105
+ with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
106
+ with gr.Column():
107
+ gr.Markdown(description)
108
+ with gr.Row():
109
+ with gr.Column():
110
+ instruction = gr.Textbox(
111
+ placeholder="Enter your code here",
112
+ label="Code",
113
+ elem_id="q-input",
114
+ )
115
+ submit = gr.Button("Generate", variant="primary")
116
+ output = gr.Code(elem_id="q-output", lines=30)
117
+ gr.Examples(
118
+ examples=examples,
119
+ inputs=[instruction],
120
+ cache_examples=False,
121
+ fn=process_example,
122
+ outputs=[output],
123
+ )
124
+
125
+ demo.queue(concurrency_count=16).launch(debug=True)