michaelmc1618 commited on
Commit
527cbfe
·
verified ·
1 Parent(s): bec2869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -28
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import os
2
- import json
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
  from transformers import pipeline
6
 
7
  # Install necessary dependencies
8
- os.system('pip install transformers gradio requests')
9
 
10
  # Inference client for chat completion
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
@@ -50,28 +50,16 @@ def evaluate_pci_compliance(audit_data):
50
  compliance_analysis = respond(audit_data, system_message, max_tokens=1024, temperature=0.7, top_p=0.95)
51
  return compliance_analysis
52
 
53
- # Analyze JSON file input
54
- def analyze_json_file(file_obj):
55
- # Handle both file-like objects and strings properly
56
- if isinstance(file_obj, bytes):
57
- file_str = file_obj.decode("utf-8")
58
- elif isinstance(file_obj, str):
59
- file_str = file_obj
60
- else:
61
- # Assuming it's a file-like object, read the content as a string
62
- file_str = file_obj.read().decode("utf-8")
63
-
64
- # Check if the file content is empty
65
- if not file_str.strip():
66
- raise ValueError("Uploaded file is empty or not in JSON format.")
67
-
68
  try:
69
- # Attempt to parse JSON
70
- json_data = json.loads(file_str)
71
- except json.JSONDecodeError as e:
72
- raise ValueError(f"Error decoding JSON: {str(e)}")
73
 
74
- audit_data = json.dumps(json_data, indent=2) # Convert JSON to a formatted string
 
75
  return audit_data
76
 
77
  # Custom CSS for the specified theme
@@ -147,22 +135,22 @@ footer {
147
  # Gradio Interface
148
  with gr.Blocks(css=custom_css) as demo:
149
  with gr.Column():
150
- gr.Markdown("# GDPR and PCI Compliance Evaluation\n### Upload Audit Data in JSON Format")
151
- json_file = gr.File(label="Upload JSON file")
152
 
153
  gdpr_compliance = gr.Textbox(lines=10, placeholder="GDPR Compliance Analysis...", label="GDPR Compliance Analysis", elem_classes="label-hidden")
154
  pci_compliance = gr.Textbox(lines=10, placeholder="PCI Compliance Analysis...", label="PCI Compliance Analysis", elem_classes="label-hidden")
155
 
156
- def run_compliance_checks(json_file):
157
- if json_file is None:
158
  return "No file uploaded", "No file uploaded"
159
- audit_data = analyze_json_file(json_file)
160
  gdpr_analysis = evaluate_gdpr_compliance(audit_data)
161
  pci_analysis = evaluate_pci_compliance(audit_data)
162
  return gdpr_analysis, pci_analysis
163
 
164
  check_compliance_btn = gr.Button("Run Compliance Checks")
165
- check_compliance_btn.click(run_compliance_checks, inputs=[json_file], outputs=[gdpr_compliance, pci_compliance])
166
 
167
  clear_btn = gr.Button("Clear")
168
  clear_btn.click(lambda: ("", ""), None, [gdpr_compliance, pci_compliance])
 
1
  import os
2
+ import pandas as pd
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
  from transformers import pipeline
6
 
7
  # Install necessary dependencies
8
+ os.system('pip install transformers gradio requests pandas')
9
 
10
  # Inference client for chat completion
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
50
  compliance_analysis = respond(audit_data, system_message, max_tokens=1024, temperature=0.7, top_p=0.95)
51
  return compliance_analysis
52
 
53
+ # Analyze CSV file input
54
+ def analyze_csv_file(file_obj):
55
+ # Read the CSV file into a pandas DataFrame
 
 
 
 
 
 
 
 
 
 
 
 
56
  try:
57
+ df = pd.read_csv(file_obj)
58
+ except Exception as e:
59
+ raise ValueError(f"Error reading CSV file: {str(e)}")
 
60
 
61
+ # Convert DataFrame to dictionary for processing
62
+ audit_data = df.to_dict(orient='records')
63
  return audit_data
64
 
65
  # Custom CSS for the specified theme
 
135
  # Gradio Interface
136
  with gr.Blocks(css=custom_css) as demo:
137
  with gr.Column():
138
+ gr.Markdown("# GDPR and PCI Compliance Evaluation\n### Upload Audit Data in CSV Format")
139
+ csv_file = gr.File(label="Upload CSV file")
140
 
141
  gdpr_compliance = gr.Textbox(lines=10, placeholder="GDPR Compliance Analysis...", label="GDPR Compliance Analysis", elem_classes="label-hidden")
142
  pci_compliance = gr.Textbox(lines=10, placeholder="PCI Compliance Analysis...", label="PCI Compliance Analysis", elem_classes="label-hidden")
143
 
144
+ def run_compliance_checks(csv_file):
145
+ if csv_file is None:
146
  return "No file uploaded", "No file uploaded"
147
+ audit_data = analyze_csv_file(csv_file)
148
  gdpr_analysis = evaluate_gdpr_compliance(audit_data)
149
  pci_analysis = evaluate_pci_compliance(audit_data)
150
  return gdpr_analysis, pci_analysis
151
 
152
  check_compliance_btn = gr.Button("Run Compliance Checks")
153
+ check_compliance_btn.click(run_compliance_checks, inputs=[csv_file], outputs=[gdpr_compliance, pci_compliance])
154
 
155
  clear_btn = gr.Button("Clear")
156
  clear_btn.click(lambda: ("", ""), None, [gdpr_compliance, pci_compliance])