File size: 2,466 Bytes
a21c858
a925f1c
 
 
6bd9265
a925f1c
 
 
 
 
 
 
 
6bd9265
a925f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aed00ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a925f1c
 
 
 
aed00ec
 
a925f1c
 
 
 
a21c858
a925f1c
aed00ec
a21c858
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gradio as gr
import torch
from sentence_transformers import SentenceTransformer
from torch.nn.functional import cosine_similarity
from spaces import GPU

# モデルの読み込み
model = SentenceTransformer("Shuu12121/CodeCloneDetection-ModernBERT-Owl")
model.eval()

# 閾値設定(安定性の高い0.9推奨)
THRESHOLD = 0.9

@GPU
def detect_clone(code1, code2):
    if not code1.strip() or not code2.strip():
        return "❌ どちらのコードも入力してください", ""

    with torch.no_grad():
        embeddings = model.encode([code1, code2], convert_to_tensor=True)
        sim_score = cosine_similarity(embeddings[0].unsqueeze(0), embeddings[1].unsqueeze(0)).item()

    result = (
        f"🟢 類似度: {sim_score:.4f}\n→ これらのコードは **クローン** と判定されます。"
        if sim_score >= THRESHOLD
        else f"🔴 類似度: {sim_score:.4f}\n→ これらのコードは **クローンではありません**。"
    )
    return result, sim_score

# Javaクローン例(ファイル読み込み)
java_code1 = """\
public static String readFileToString(File file, Charset encoding) throws IOException {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\\n");
        }
        return sb.toString();
    }
}"""

java_code2 = """\
public static String readFileToString(File file, Charset encoding) throws IOException {
    byte[] bytes = java.nio.file.Files.readAllBytes(file.toPath());
    return new String(bytes, encoding);
}"""

# Gradioインターフェースの作成
demo = gr.Interface(
    fn=detect_clone,
    inputs=[
        gr.Textbox(label="コードスニペット1", lines=12, value=java_code1),
        gr.Textbox(label="コードスニペット2", lines=8, value=java_code2),
    ],
    outputs=[
        gr.Markdown(label="判定結果"),
        gr.Number(label="Cosine Similarity")
    ],
    title="Code Clone Detection with ModernBERT-Owl 🦉",
    description="Shuu12121/CodeModernBERT-Owl によって構築された Sentence-BERT モデルを使用し、コードクローンを検出します。これは Java における意味的に同等なファイル読み込み関数の例です。"
)

if __name__ == "__main__":
    demo.launch()