omernet commited on
Commit
9459863
·
verified ·
1 Parent(s): 57893dc

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Model yükle
6
+ MODEL_NAME = "mahdin70/codebert-devign-code-vulnerability-detector"
7
+ print(f"Model yükleniyor: {MODEL_NAME}")
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
11
+ model.eval()
12
+
13
+ def analyze_code(code):
14
+ """Kod güvenlik analizi yap"""
15
+ if not code or not code.strip():
16
+ return "Lütfen kod girin", 0.0
17
+
18
+ # Tokenize
19
+ inputs = tokenizer(
20
+ code,
21
+ return_tensors="pt",
22
+ truncation=True,
23
+ max_length=512,
24
+ padding=True
25
+ )
26
+
27
+ # Tahmin
28
+ with torch.no_grad():
29
+ outputs = model(**inputs)
30
+ probabilities = torch.softmax(outputs.logits, dim=-1)
31
+ prediction = torch.argmax(probabilities, dim=-1).item()
32
+ confidence = probabilities[0][prediction].item()
33
+
34
+ # Sonuçları yorumla
35
+ if prediction == 1:
36
+ result = "🔴 ZAFİYET TESPİT EDİLDİ"
37
+ detail = f"Bu kodda güvenlik zafiyeti olabilir."
38
+ else:
39
+ result = "🟢 GÜVENLİ GÖRÜNÜYOR"
40
+ detail = "Bu kod güvenli görünüyor."
41
+
42
+ return f"{result}\n\n{detail}\n\nGüven skoru: {confidence:.2%}", confidence
43
+
44
+ # Gradio arayüzü
45
+ demo = gr.Interface(
46
+ fn=analyze_code,
47
+ inputs=gr.Code(
48
+ label="Kodu yapıştırın",
49
+ language="python",
50
+ lines=10
51
+ ),
52
+ outputs=[
53
+ gr.Textbox(label="Sonuç", lines=5),
54
+ gr.Slider(label="Güven Skoru", minimum=0, maximum=1)
55
+ ],
56
+ title="🔒 Code Security Analyzer",
57
+ description="""
58
+ Bu araç CodeBERT tabanlı bir model kullanarak kodunuzda potansiyel güvenlik zafiyetlerini tespit etmeye çalışır.
59
+
60
+ **Not:** Bu otomatik bir analizdir ve %100 doğru olmayabilir. Önemli kodlar için manuel review yapın.
61
+ """,
62
+ examples=[
63
+ ["def login(user, pwd):\n query = f\"SELECT * FROM users WHERE name='{user}'\"\n return db.execute(query)"],
64
+ ["def login(user, pwd):\n query = \"SELECT * FROM users WHERE name=%s\"\n return db.execute(query, (user,))"],
65
+ ["def render(comment):\n return f'\u003cdiv\u003e{comment}\u003c/div\u003e'"],
66
+ ]
67
+ )
68
+
69
+ if __name__ == "__main__":
70
+ demo.launch()