Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,74 +1,92 @@
|
|
| 1 |
-
import
|
| 2 |
-
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
|
| 3 |
-
os.environ['GRADIO_SERVER_PORT'] = '7860'
|
| 4 |
-
|
| 5 |
-
import gradio as gr
|
| 6 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 7 |
import torch
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Tahmin
|
| 32 |
-
with torch.no_grad():
|
| 33 |
-
outputs = model(**inputs)
|
| 34 |
-
probabilities = torch.softmax(outputs.logits, dim=-1)
|
| 35 |
-
prediction = torch.argmax(probabilities, dim=-1).item()
|
| 36 |
-
confidence = probabilities[0][prediction].item()
|
| 37 |
-
|
| 38 |
-
# Sonuçları yorumla
|
| 39 |
-
if prediction == 1:
|
| 40 |
-
result = "🔴 ZAFİYET TESPİT EDİLDİ"
|
| 41 |
-
detail = f"Bu kodda güvenlik zafiyeti olabilir."
|
| 42 |
-
else:
|
| 43 |
-
result = "🟢 GÜVENLİ GÖRÜNÜYOR"
|
| 44 |
-
detail = "Bu kod güvenli görünüyor."
|
| 45 |
-
|
| 46 |
-
return f"{result}\n\n{detail}\n\nGüven skoru: {confidence:.2%}", confidence
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
language="python",
|
| 54 |
-
lines=10
|
| 55 |
-
),
|
| 56 |
-
outputs=[
|
| 57 |
-
gr.Textbox(label="Sonuç", lines=5),
|
| 58 |
-
gr.Slider(label="Güven Skoru", minimum=0, maximum=1)
|
| 59 |
-
],
|
| 60 |
-
title="🔒 Code Security Analyzer",
|
| 61 |
-
description="""
|
| 62 |
-
Bu araç CodeBERT tabanlı bir model kullanarak kodunuzda potansiyel güvenlik zafiyetlerini tespit etmeye çalışır.
|
| 63 |
-
|
| 64 |
-
**Not:** Bu otomatik bir analizdir ve %100 doğru olmayabilir. Önemli kodlar için manuel review yapın.
|
| 65 |
-
""",
|
| 66 |
-
examples=[
|
| 67 |
-
["def login(user, pwd):\n query = f\"SELECT * FROM users WHERE name='{user}'\"\n return db.execute(query)"],
|
| 68 |
-
["def login(user, pwd):\n query = \"SELECT * FROM users WHERE name=%s\"\n return db.execute(query, (user,))"],
|
| 69 |
-
["def render(comment):\n return f'\u003cdiv\u003e{comment}\u003c/div\u003e'"],
|
| 70 |
-
]
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Sayfa ayarları
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Code Security Analyzer",
|
| 8 |
+
page_icon="🔒",
|
| 9 |
+
layout="wide"
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# Başlık
|
| 13 |
+
st.title("🔒 Code Security Analyzer")
|
| 14 |
+
st.markdown("CodeBERT tabanlı kod güvenlik analizi aracı")
|
| 15 |
+
st.markdown("---")
|
| 16 |
|
| 17 |
+
# Model yükleme durumu
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def load_model():
|
| 20 |
+
MODEL_NAME = "mahdin70/codebert-devign-code-vulnerability-detector"
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 23 |
+
model.eval()
|
| 24 |
+
return tokenizer, model
|
| 25 |
+
|
| 26 |
+
with st.spinner("Model yükleniyor..."):
|
| 27 |
+
tokenizer, model = load_model()
|
| 28 |
+
|
| 29 |
+
st.success("Model hazır!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Kod girişi
|
| 32 |
+
code_input = st.text_area(
|
| 33 |
+
"Kodu yapıştırın",
|
| 34 |
+
height=200,
|
| 35 |
+
placeholder="def login(username, password):\n query = f\"SELECT * FROM users WHERE user='{username}'\"\n return db.execute(query)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# Analiz butonu
|
| 39 |
+
if st.button("🔍 Analiz Et", type="primary"):
|
| 40 |
+
if not code_input or not code_input.strip():
|
| 41 |
+
st.warning("Lütfen analiz edilecek kod girin")
|
| 42 |
+
else:
|
| 43 |
+
with st.spinner("Analiz ediliyor..."):
|
| 44 |
+
# Tokenize
|
| 45 |
+
inputs = tokenizer(
|
| 46 |
+
code_input,
|
| 47 |
+
return_tensors="pt",
|
| 48 |
+
truncation=True,
|
| 49 |
+
max_length=512,
|
| 50 |
+
padding=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Tahmin
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
outputs = model(**inputs)
|
| 56 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)
|
| 57 |
+
prediction = torch.argmax(probabilities, dim=-1).item()
|
| 58 |
+
confidence = probabilities[0][prediction].item()
|
| 59 |
+
|
| 60 |
+
# Sonuçları göster
|
| 61 |
+
st.markdown("---")
|
| 62 |
+
st.subheader("Sonuç")
|
| 63 |
+
|
| 64 |
+
if prediction == 1:
|
| 65 |
+
st.error("🔴 ZAFİYET TESPİT EDİLDİ")
|
| 66 |
+
st.markdown("Bu kodda güvenlik zafiyeti olabilir.")
|
| 67 |
+
else:
|
| 68 |
+
st.success("🟢 GÜVENLİ GÖRÜNÜYOR")
|
| 69 |
+
st.markdown("Bu kod güvenli görünüyor.")
|
| 70 |
+
|
| 71 |
+
st.progress(confidence)
|
| 72 |
+
st.markdown(f"**Güven Skoru:** {confidence:.2%}")
|
| 73 |
+
|
| 74 |
+
# Örnekler
|
| 75 |
+
st.markdown("---")
|
| 76 |
+
st.subheader("Örnek Kodlar")
|
| 77 |
+
|
| 78 |
+
examples = {
|
| 79 |
+
"SQL Injection (Zafiyet)": "def login(user, pwd):\n query = f\"SELECT * FROM users WHERE name='{user}'\"\n return db.execute(query)",
|
| 80 |
+
"SQL Injection (Güvenli)": "def login(user, pwd):\n query = \"SELECT * FROM users WHERE name=%s\"\n return db.execute(query, (user,))",
|
| 81 |
+
"XSS (Zafiyet)": "def render(comment):\n return f'<div>{comment}</div>'",
|
| 82 |
+
"XSS (Güvenli)": "def render(comment):\n import html\n return f'<div>{html.escape(comment)}</div>'"
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
selected_example = st.selectbox("Örnek seçin:", list(examples.keys()))
|
| 86 |
+
if st.button("Örneği Kullan"):
|
| 87 |
+
st.session_state.code_input = examples[selected_example]
|
| 88 |
+
st.rerun()
|
| 89 |
+
|
| 90 |
+
# Not
|
| 91 |
+
st.markdown("---")
|
| 92 |
+
st.info("💡 **Not:** Bu otomatik bir analizdir ve %100 doğru olmayabilir. Önemli kodlar için manuel review yapın.")
|