Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ IMPROVEMENTS OVER v1:
|
|
| 10 |
Run AFTER notebooks 1-4 to use the improved models.
|
| 11 |
Upload this to: https://huggingface.co/spaces/ayshajavd/code-security-analyzer
|
| 12 |
"""
|
|
|
|
|
|
|
| 13 |
import json
|
| 14 |
import re
|
| 15 |
import time
|
|
@@ -20,8 +22,7 @@ from transformers import (
|
|
| 20 |
AutoModelForSequenceClassification,
|
| 21 |
T5ForConditionalGeneration,
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
| 25 |
from huggingface_hub import hf_hub_download
|
| 26 |
import numpy as np
|
| 27 |
|
|
@@ -371,6 +372,28 @@ def create_pdf(report_text):
|
|
| 371 |
|
| 372 |
doc.build(elements)
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
return pdf_path
|
| 375 |
EXAMPLES = [
|
| 376 |
["""import sqlite3\n\ndef get_user(username):\n conn = sqlite3.connect('users.db')\n query = f"SELECT * FROM users WHERE username = '{username}'"\n return conn.execute(query).fetchone()\n"""],
|
|
@@ -395,8 +418,9 @@ with gr.Blocks(
|
|
| 395 |
|
| 396 |
**v2 Improvements:** Per-class threshold optimization | Temperature-calibrated probabilities | Asymmetric Loss training | GraphCodeBERT-base (125M params) | CodeT5+ 220M CWE-aware fixer
|
| 397 |
""")
|
|
|
|
| 398 |
|
| 399 |
-
|
| 400 |
with gr.Column(scale=1):
|
| 401 |
|
| 402 |
code_input = gr.Code(
|
|
@@ -419,6 +443,7 @@ with gr.Blocks(
|
|
| 419 |
size="lg"
|
| 420 |
)
|
| 421 |
|
|
|
|
| 422 |
with gr.Column(scale=1):
|
| 423 |
|
| 424 |
report_output = gr.Markdown(
|
|
@@ -437,6 +462,9 @@ with gr.Blocks(
|
|
| 437 |
label="JSON Report",
|
| 438 |
visible=False
|
| 439 |
)
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
gr.Examples(examples=EXAMPLES, inputs=[code_input], label="Example Code Snippets")
|
| 442 |
|
|
@@ -447,7 +475,9 @@ with gr.Blocks(
|
|
| 447 |
fn=analyze_code, inputs=[code_input], outputs=[report_output], api_name="analyze"
|
| 448 |
)
|
| 449 |
json_btn.click(
|
| 450 |
-
fn=show_json,
|
|
|
|
|
|
|
| 451 |
)
|
| 452 |
download_btn.click(
|
| 453 |
fn=create_pdf,
|
|
|
|
| 10 |
Run AFTER notebooks 1-4 to use the improved models.
|
| 11 |
Upload this to: https://huggingface.co/spaces/ayshajavd/code-security-analyzer
|
| 12 |
"""
|
| 13 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph
|
| 14 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 15 |
import json
|
| 16 |
import re
|
| 17 |
import time
|
|
|
|
| 22 |
AutoModelForSequenceClassification,
|
| 23 |
T5ForConditionalGeneration,
|
| 24 |
)
|
| 25 |
+
|
|
|
|
| 26 |
from huggingface_hub import hf_hub_download
|
| 27 |
import numpy as np
|
| 28 |
|
|
|
|
| 372 |
|
| 373 |
doc.build(elements)
|
| 374 |
|
| 375 |
+
return pdf_path
|
| 376 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph
|
| 377 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def create_pdf(report_text):
|
| 381 |
+
|
| 382 |
+
pdf_path = "security_report.pdf"
|
| 383 |
+
|
| 384 |
+
doc = SimpleDocTemplate(pdf_path)
|
| 385 |
+
|
| 386 |
+
styles = getSampleStyleSheet()
|
| 387 |
+
|
| 388 |
+
elements = [
|
| 389 |
+
Paragraph(
|
| 390 |
+
str(report_text).replace("\n", "<br/>"),
|
| 391 |
+
styles["BodyText"]
|
| 392 |
+
)
|
| 393 |
+
]
|
| 394 |
+
|
| 395 |
+
doc.build(elements)
|
| 396 |
+
|
| 397 |
return pdf_path
|
| 398 |
EXAMPLES = [
|
| 399 |
["""import sqlite3\n\ndef get_user(username):\n conn = sqlite3.connect('users.db')\n query = f"SELECT * FROM users WHERE username = '{username}'"\n return conn.execute(query).fetchone()\n"""],
|
|
|
|
| 418 |
|
| 419 |
**v2 Improvements:** Per-class threshold optimization | Temperature-calibrated probabilities | Asymmetric Loss training | GraphCodeBERT-base (125M params) | CodeT5+ 220M CWE-aware fixer
|
| 420 |
""")
|
| 421 |
+
with gr.Row():
|
| 422 |
|
| 423 |
+
# LEFT COLUMN
|
| 424 |
with gr.Column(scale=1):
|
| 425 |
|
| 426 |
code_input = gr.Code(
|
|
|
|
| 443 |
size="lg"
|
| 444 |
)
|
| 445 |
|
| 446 |
+
# RIGHT COLUMN
|
| 447 |
with gr.Column(scale=1):
|
| 448 |
|
| 449 |
report_output = gr.Markdown(
|
|
|
|
| 462 |
label="JSON Report",
|
| 463 |
visible=False
|
| 464 |
)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
|
| 468 |
|
| 469 |
gr.Examples(examples=EXAMPLES, inputs=[code_input], label="Example Code Snippets")
|
| 470 |
|
|
|
|
| 475 |
fn=analyze_code, inputs=[code_input], outputs=[report_output], api_name="analyze"
|
| 476 |
)
|
| 477 |
json_btn.click(
|
| 478 |
+
fn=show_json,
|
| 479 |
+
inputs=[code_input],
|
| 480 |
+
outputs=[json_output]
|
| 481 |
)
|
| 482 |
download_btn.click(
|
| 483 |
fn=create_pdf,
|