AB498 commited on
Commit
7ef9108
·
1 Parent(s): 480c4e6
Files changed (1) hide show
  1. app.py +101 -43
app.py CHANGED
@@ -1,50 +1,108 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- def respond(
5
- message,
6
- history: list[dict[str, str]],
7
- system_message,
8
- max_tokens,
9
- temperature,
10
- top_p,
11
- ):
12
- client = InferenceClient(model="openai/gpt-oss-20b")
13
 
14
- messages = [{"role": "system", "content": system_message}]
15
- messages.extend(history)
16
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- response = ""
19
-
20
- for msg in client.chat_completion(
21
- messages,
22
- max_tokens=max_tokens,
23
- stream=True,
24
- temperature=temperature,
25
- top_p=top_p,
26
- ):
27
- choices = msg.choices
28
- token = ""
29
- if len(choices) and choices[0].delta.content:
30
- token = choices[0].delta.content
31
- response += token
32
- yield response
33
-
34
- chatbot = gr.ChatInterface(
35
- respond,
36
- type="messages",
37
- additional_inputs=[
38
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
39
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
40
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
41
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
42
- ],
43
- )
44
-
45
- demo = gr.Blocks()
46
- with demo:
47
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  if __name__ == "__main__":
50
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import RobertaTokenizer, RobertaForMaskedLM
3
+ import torch
4
 
5
+ # Load CodeBERT model and tokenizer
6
+ model_name = "microsoft/codebert-base-mlm"
7
+ tokenizer = RobertaTokenizer.from_pretrained(model_name)
8
+ model = RobertaForMaskedLM.from_pretrained(model_name)
 
 
 
 
 
9
 
10
+ def predict_masked_code(code_with_mask, top_k=5):
11
+ """
12
+ Predict the masked token in code.
13
+ Use <mask> to indicate where to predict.
14
+ """
15
+ try:
16
+ # Replace <mask> with the tokenizer's mask token
17
+ code_with_mask = code_with_mask.replace("<mask>", tokenizer.mask_token)
18
+
19
+ # Tokenize input
20
+ inputs = tokenizer(code_with_mask, return_tensors="pt")
21
+
22
+ # Find the position of the mask token
23
+ mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
24
+
25
+ if len(mask_token_index) == 0:
26
+ return "Error: No <mask> token found in the input. Please include <mask> where you want predictions."
27
+
28
+ # Get predictions
29
+ with torch.no_grad():
30
+ outputs = model(**inputs)
31
+ predictions = outputs.logits
32
+
33
+ # Get top-k predictions for the mask token
34
+ mask_token_logits = predictions[0, mask_token_index, :]
35
+ top_tokens = torch.topk(mask_token_logits, top_k, dim=1)
36
+
37
+ results = []
38
+ for i, (token_id, score) in enumerate(zip(top_tokens.indices[0].tolist(), top_tokens.values[0].tolist())):
39
+ predicted_token = tokenizer.decode([token_id])
40
+ filled_code = code_with_mask.replace(tokenizer.mask_token, predicted_token)
41
+ results.append(f"{i+1}. {predicted_token} (score: {score:.2f})\n Code: {filled_code}")
42
+
43
+ return "\n\n".join(results)
44
+
45
+ except Exception as e:
46
+ return f"Error: {str(e)}"
47
 
48
+ # Create Gradio interface
49
+ with gr.Blocks(title="CodeBERT Masked Language Model") as demo:
50
+ gr.Markdown(
51
+ """
52
+ # CodeBERT Masked Language Model
53
+
54
+ This model predicts masked tokens in code. Use `<mask>` to indicate where you want predictions.
55
+
56
+ ### Examples:
57
+ - `def <mask>(x, y): return x + y`
58
+ - `import <mask>`
59
+ - `for i in <mask>(10):`
60
+ - `x = [1, 2, 3]; y = x.<mask>()`
61
+ """
62
+ )
63
+
64
+ with gr.Row():
65
+ with gr.Column():
66
+ code_input = gr.Textbox(
67
+ label="Code with <mask>",
68
+ placeholder="Enter code with <mask> token...",
69
+ lines=5,
70
+ value="def <mask>(x, y):\n return x + y"
71
+ )
72
+ top_k_slider = gr.Slider(
73
+ minimum=1,
74
+ maximum=10,
75
+ value=5,
76
+ step=1,
77
+ label="Number of predictions"
78
+ )
79
+ predict_btn = gr.Button("Predict", variant="primary")
80
+
81
+ with gr.Column():
82
+ output = gr.Textbox(
83
+ label="Predictions",
84
+ lines=15,
85
+ interactive=False
86
+ )
87
+
88
+ # Examples
89
+ gr.Examples(
90
+ examples=[
91
+ ["def <mask>(x, y):\n return x + y", 5],
92
+ ["import <mask>", 5],
93
+ ["for i in <mask>(10):", 5],
94
+ ["x = [1, 2, 3]\ny = x.<mask>()", 5],
95
+ ["if x <mask> 0:", 5],
96
+ ["class <mask>:", 5],
97
+ ],
98
+ inputs=[code_input, top_k_slider],
99
+ )
100
+
101
+ predict_btn.click(
102
+ fn=predict_masked_code,
103
+ inputs=[code_input, top_k_slider],
104
+ outputs=output
105
+ )
106
 
107
  if __name__ == "__main__":
108
  demo.launch()