Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| # Model yükle | |
| MODEL_NAME = "mahdin70/codebert-devign-code-vulnerability-detector" | |
| print(f"Model yükleniyor: {MODEL_NAME}") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
| model.eval() | |
| def analyze_code(code): | |
| """Kod güvenlik analizi yap""" | |
| if not code or not code.strip(): | |
| return "Lütfen kod girin", 0.0 | |
| # Tokenize | |
| inputs = tokenizer( | |
| code, | |
| return_tensors="pt", | |
| truncation=True, | |
| max_length=512, | |
| padding=True | |
| ) | |
| # Tahmin | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probabilities = torch.softmax(outputs.logits, dim=-1) | |
| prediction = torch.argmax(probabilities, dim=-1).item() | |
| confidence = probabilities[0][prediction].item() | |
| # Sonuçları yorumla | |
| if prediction == 1: | |
| result = "🔴 ZAFİYET TESPİT EDİLDİ" | |
| detail = f"Bu kodda güvenlik zafiyeti olabilir." | |
| else: | |
| result = "🟢 GÜVENLİ GÖRÜNÜYOR" | |
| detail = "Bu kod güvenli görünüyor." | |
| return f"{result}\n\n{detail}\n\nGüven skoru: {confidence:.2%}", confidence | |
| # Gradio arayüzü | |
| demo = gr.Interface( | |
| fn=analyze_code, | |
| inputs=gr.Code( | |
| label="Kodu yapıştırın", | |
| language="python", | |
| lines=10 | |
| ), | |
| outputs=[ | |
| gr.Textbox(label="Sonuç", lines=5), | |
| gr.Slider(label="Güven Skoru", minimum=0, maximum=1) | |
| ], | |
| title="🔒 Code Security Analyzer", | |
| description=""" | |
| Bu araç CodeBERT tabanlı bir model kullanarak kodunuzda potansiyel güvenlik zafiyetlerini tespit etmeye çalışır. | |
| **Not:** Bu otomatik bir analizdir ve %100 doğru olmayabilir. Önemli kodlar için manuel review yapın. | |
| """, | |
| examples=[ | |
| ["def login(user, pwd):\n query = f\"SELECT * FROM users WHERE name='{user}'\"\n return db.execute(query)"], | |
| ["def login(user, pwd):\n query = \"SELECT * FROM users WHERE name=%s\"\n return db.execute(query, (user,))"], | |
| ["def render(comment):\n return f'\u003cdiv\u003e{comment}\u003c/div\u003e'"], | |
| ] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |