kid25 commited on
Commit
ece2aee
·
verified ·
1 Parent(s): 8fbb9b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -88
app.py CHANGED
@@ -20,6 +20,8 @@ from transformers import (
20
  AutoModelForSequenceClassification,
21
  T5ForConditionalGeneration,
22
  )
 
 
23
  from huggingface_hub import hf_hub_download
24
  import numpy as np
25
 
@@ -353,7 +355,23 @@ def get_json_report(code):
353
  if not code or not code.strip(): return {"error": "No code provided"}
354
  return build_json_report(code)
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
 
 
357
  EXAMPLES = [
358
  ["""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"""],
359
  ["""#include <stdio.h>\n#include <string.h>\n\nvoid process_input(char *user_input) {\n char buffer[64];\n strcpy(buffer, user_input);\n printf("Processed: %s\\n", buffer);\n}\n"""],
@@ -385,16 +403,37 @@ with gr.Blocks(
385
  analyze_btn = gr.Button("🔍 Analyze Security", variant="primary", size="lg")
386
  json_btn = gr.Button("📋 JSON Report", variant="secondary", size="lg")
387
  with gr.Column(scale=1):
388
- report_output = gr.Markdown(label="Security Report")
389
- json_output = gr.JSON(label="JSON Report", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
390
 
391
  gr.Examples(examples=EXAMPLES, inputs=[code_input], label="Example Code Snippets")
392
 
393
  def show_json(code):
394
  return gr.update(visible=True, value=get_json_report(code))
395
 
396
- analyze_btn.click(fn=analyze_code, inputs=[code_input], outputs=[report_output], api_name="analyze")
397
- json_btn.click(fn=show_json, inputs=[code_input], outputs=[json_output])
 
 
 
 
 
 
 
 
 
398
 
399
  with gr.Row(visible=False):
400
  api_json_btn = gr.Button("get_json", visible=False)
@@ -431,91 +470,7 @@ curl -X POST https://ayshajavd-code-security-analyzer.hf.space/call/analyze \\
431
  | **A08: Integrity Failures** | CWE-502 |
432
  | **A10: SSRF** | CWE-918 |
433
  """)
434
- def generate_report(
435
- code,
436
- prediction,
437
- cwe,
438
- owasp,
439
- severity,
440
- explanation,
441
- recommendations,
442
- fixed_code
443
- ):
444
- report = f"""
445
- SOFTWARE SECURITY ASSESSMENT REPORT
446
-
447
- Analysis Result
448
- ---------------
449
- Prediction: {prediction}
450
-
451
- Detected Vulnerability
452
- ----------------------
453
- CWE: {cwe}
454
- OWASP: {owasp}
455
- Severity: {severity}
456
-
457
- Explanation
458
- -----------
459
- {explanation}
460
-
461
- Recommendations
462
- ---------------
463
- {recommendations}
464
-
465
- Suggested Secure Version
466
- ------------------------
467
- {fixed_code}
468
-
469
- Original Code
470
- -------------
471
- {code}
472
- """
473
 
474
- return report
475
- report_output = gr.Textbox(
476
- label="Security Report",
477
- lines=25
478
- )
479
- report = generate_report(
480
- code,
481
- prediction,
482
- cwe,
483
- owasp,
484
- severity,
485
- explanation,
486
- recommendations,
487
- fixed_code
488
- )
489
- return (
490
- prediction,
491
- explanation,
492
- fixed_code,
493
- report
494
- )
495
- from reportlab.platypus import SimpleDocTemplate, Paragraph
496
- from reportlab.lib.styles import getSampleStyleSheet
497
-
498
- def create_pdf(report_text):
499
- pdf_path = "security_report.pdf"
500
-
501
- doc = SimpleDocTemplate(pdf_path)
502
-
503
- styles = getSampleStyleSheet()
504
-
505
- elements = [
506
- Paragraph(report_text.replace("\n", "<br/>"), styles["BodyText"])
507
- ]
508
-
509
- doc.build(elements)
510
-
511
- return pdf_path
512
- download_btn = gr.Button("Generate PDF Report")
513
- pdf_file = gr.File(label="Download Report")
514
- download_btn.click(
515
- create_pdf,
516
- inputs=report_output,
517
- outputs=pdf_file
518
- )
519
 
520
  if __name__ == "__main__":
521
  demo.launch()
 
20
  AutoModelForSequenceClassification,
21
  T5ForConditionalGeneration,
22
  )
23
+ from reportlab.platypus import SimpleDocTemplate, Paragraph
24
+ from reportlab.lib.styles import getSampleStyleSheet
25
  from huggingface_hub import hf_hub_download
26
  import numpy as np
27
 
 
355
  if not code or not code.strip(): return {"error": "No code provided"}
356
  return build_json_report(code)
357
 
358
+ def create_pdf(report_text):
359
+ pdf_path = "security_report.pdf"
360
+
361
+ doc = SimpleDocTemplate(pdf_path)
362
+
363
+ styles = getSampleStyleSheet()
364
+
365
+ elements = [
366
+ Paragraph(
367
+ report_text.replace("\n", "<br/>"),
368
+ styles["BodyText"]
369
+ )
370
+ ]
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"""],
377
  ["""#include <stdio.h>\n#include <string.h>\n\nvoid process_input(char *user_input) {\n char buffer[64];\n strcpy(buffer, user_input);\n printf("Processed: %s\\n", buffer);\n}\n"""],
 
403
  analyze_btn = gr.Button("🔍 Analyze Security", variant="primary", size="lg")
404
  json_btn = gr.Button("📋 JSON Report", variant="secondary", size="lg")
405
  with gr.Column(scale=1):
406
+ report_output = gr.Markdown(label="Security Report")
407
+
408
+ download_btn = gr.Button(
409
+ "📄 Generate PDF Report"
410
+ )
411
+
412
+ pdf_file = gr.File(
413
+ label="Download Report"
414
+ )
415
+
416
+ json_output = gr.JSON(
417
+ label="JSON Report",
418
+ visible=False
419
+ )
420
 
421
  gr.Examples(examples=EXAMPLES, inputs=[code_input], label="Example Code Snippets")
422
 
423
  def show_json(code):
424
  return gr.update(visible=True, value=get_json_report(code))
425
 
426
+ analyze_btn.click(
427
+ fn=analyze_code, inputs=[code_input], outputs=[report_output], api_name="analyze"
428
+ )
429
+ json_btn.click(
430
+ fn=show_json, inputs=[code_input], outputs=[json_output]
431
+ )
432
+ download_btn.click(
433
+ fn=create_pdf,
434
+ inputs=[report_output],
435
+ outputs=[pdf_file]
436
+ )
437
 
438
  with gr.Row(visible=False):
439
  api_json_btn = gr.Button("get_json", visible=False)
 
470
  | **A08: Integrity Failures** | CWE-502 |
471
  | **A10: SSRF** | CWE-918 |
472
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
 
475
  if __name__ == "__main__":
476
  demo.launch()