Spaces:
Sleeping
Sleeping
app.py
CHANGED
|
@@ -1,50 +1,108 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
max_tokens,
|
| 9 |
-
temperature,
|
| 10 |
-
top_p,
|
| 11 |
-
):
|
| 12 |
-
client = InferenceClient(model="openai/gpt-oss-20b")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|