syedMohib44 commited on
Commit
b24070d
·
1 Parent(s): 70f694d
Files changed (1) hide show
  1. app.py +20 -50
app.py CHANGED
@@ -3,16 +3,17 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  from fpdf import FPDF
4
  from datetime import date
5
  import os
6
- import re
7
 
8
- # Load Hugging Face model
9
  token = os.getenv("HF_TOKEN")
10
- model_id = "syedMohib44/ai-auditor-model-l"
11
 
12
  tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
13
  model = AutoModelForCausalLM.from_pretrained(model_id, token=token)
 
14
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
15
 
 
16
  def generate_audit_pdf(code, description, findings):
17
  pdf = FPDF()
18
  pdf.add_page()
@@ -24,7 +25,7 @@ def generate_audit_pdf(code, description, findings):
24
 
25
  pdf.set_font("Arial", '', 12)
26
  pdf.cell(0, 10, f"Scan Date: {date.today().strftime('%Y-%m-%d')}", ln=True)
27
- pdf.cell(0, 10, "Model: Gemma 2B IT with LoRA", ln=True)
28
  pdf.cell(0, 10, "Audit Engine: AI Audit Agent", ln=True)
29
  pdf.ln(5)
30
 
@@ -55,56 +56,25 @@ def generate_audit_pdf(code, description, findings):
55
  pdf.output(output_path)
56
  return output_path
57
 
58
- def extract_sections(text):
59
- headers = ["High Severity", "Medium Severity", "Low Severity", "Best Practices"]
60
- findings = {}
61
-
62
- for i, header in enumerate(headers):
63
- if i + 1 < len(headers):
64
- next_header = headers[i + 1]
65
- pattern = rf"{header}:(.*?)(?=\n{next_header}:)"
66
- else:
67
- pattern = rf"{header}:(.*?)(?=\nDisclaimer:|\Z)"
68
-
69
- match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
70
- findings[header] = match.group(1).strip() if match else "No issues found."
71
-
72
- return findings
73
-
74
-
75
  def audit_contract(code, description):
76
- prompt = f"""
77
- ### Instruction:
78
- You are a professional smart contract auditor.
79
-
80
- Audit the following smart contract and generate a structured report.
81
-
82
- Each severity level should contain:
83
- - Title
84
- - Description
85
- - Recommendation
86
-
87
- If no issues are found in a category, return: "No issues found."
88
-
89
-
90
  ### Description:
91
  {description}
92
-
93
- ### Audit Report:
94
-
95
- Disclaimer: This is an automated audit report generated by AI. Manual review by security experts is recommended.
96
  """
97
- response = pipe(prompt, max_new_tokens=800, do_sample=False)[0]["generated_text"]
98
-
99
- # Print raw response for debugging
100
- print("=== RAW MODEL OUTPUT ===")
101
- print(response)
102
 
103
- findings_dict = response #extract_sections(response)
104
- pdf_path = generate_audit_pdf(code, description, findings_dict)
105
- # combined_findings = "\n\n".join(f"{k}:\n{v}" for k, v in findings_dict.items())
106
- return response, pdf_path
107
 
 
108
  iface = gr.Interface(
109
  fn=audit_contract,
110
  inputs=[
@@ -116,8 +86,8 @@ iface = gr.Interface(
116
  gr.File(label="Download Audit Report")
117
  ],
118
  title="AI Smart Contract Auditor",
119
- description="Paste your Solidity contract and description. The AI will generate a structured audit with severity levels and a downloadable PDF."
120
  )
121
 
122
  if __name__ == "__main__":
123
- iface.launch()
 
3
  from fpdf import FPDF
4
  from datetime import date
5
  import os
 
6
 
7
+ # Load model with Hugging Face token
8
  token = os.getenv("HF_TOKEN")
9
+ model_id = "syedMohib44/ai-auditor-model"
10
 
11
  tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
12
  model = AutoModelForCausalLM.from_pretrained(model_id, token=token)
13
+
14
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
15
 
16
+ # PDF generator
17
  def generate_audit_pdf(code, description, findings):
18
  pdf = FPDF()
19
  pdf.add_page()
 
25
 
26
  pdf.set_font("Arial", '', 12)
27
  pdf.cell(0, 10, f"Scan Date: {date.today().strftime('%Y-%m-%d')}", ln=True)
28
+ pdf.cell(0, 10, "Model: TinyLlama-1.1B-Chat with LoRA", ln=True)
29
  pdf.cell(0, 10, "Audit Engine: AI Audit Agent", ln=True)
30
  pdf.ln(5)
31
 
 
56
  pdf.output(output_path)
57
  return output_path
58
 
59
+ # Inference function for Gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  def audit_contract(code, description):
61
+ prompt = f"""### Instruction:
62
+ Please audit the following smart contract and provide a recommendation.
63
+ ### Code:
64
+ {code}
 
 
 
 
 
 
 
 
 
 
65
  ### Description:
66
  {description}
67
+ ### Recommendation:
 
 
 
68
  """
69
+ result = pipe(prompt, max_new_tokens=300)[0]["generated_text"]
70
+ findings = result.split("### Recommendation:")[-1].strip()
71
+
72
+ # Generate PDF
73
+ pdf_path = generate_audit_pdf(code, description, findings)
74
 
75
+ return findings, pdf_path
 
 
 
76
 
77
+ # Gradio UI
78
  iface = gr.Interface(
79
  fn=audit_contract,
80
  inputs=[
 
86
  gr.File(label="Download Audit Report")
87
  ],
88
  title="AI Smart Contract Auditor",
89
+ description="Paste your smart contract code and description. The AI will generate an audit with findings and a downloadable PDF report."
90
  )
91
 
92
  if __name__ == "__main__":
93
+ iface.launch()