Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
# Initialize the Hugging Face Inference Client
|
5 |
+
client = InferenceClient()
|
6 |
+
|
7 |
+
# Function to stream the compliance suggestions as they are generated
|
8 |
+
def analyze_compliance_stream(code, compliance_standard):
|
9 |
+
prompt = f"Analyze the following code for {compliance_standard} compliance and suggest modifications or refactoring to meet the guidelines:\n\n{code}"
|
10 |
+
|
11 |
+
messages = [
|
12 |
+
{"role": "user", "content": prompt}
|
13 |
+
]
|
14 |
+
|
15 |
+
# Create a stream to receive generated content
|
16 |
+
stream = client.chat.completions.create(
|
17 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
18 |
+
messages=messages,
|
19 |
+
temperature=0.5,
|
20 |
+
max_tokens=1024,
|
21 |
+
top_p=0.7,
|
22 |
+
stream=True
|
23 |
+
)
|
24 |
+
|
25 |
+
# Stream content as it is generated
|
26 |
+
compliance_suggestions = ""
|
27 |
+
for chunk in stream:
|
28 |
+
compliance_suggestions += chunk.choices[0].delta.content
|
29 |
+
yield compliance_suggestions # Yield incremental content to display immediately
|
30 |
+
|
31 |
+
# Create Gradio interface with the modified layout
|
32 |
+
with gr.Blocks() as app:
|
33 |
+
gr.Markdown("## Code Compliance Advisor")
|
34 |
+
gr.Markdown("Analyze your code for legal compliance and security standards (e.g., GDPR, HIPAA) and receive actionable suggestions.")
|
35 |
+
|
36 |
+
with gr.Row():
|
37 |
+
# First column for input components
|
38 |
+
with gr.Column():
|
39 |
+
code_input = gr.Textbox(lines=10, label="Code Snippet", placeholder="Enter your code here", elem_id="full_width")
|
40 |
+
compliance_standard = gr.Dropdown(
|
41 |
+
choices=["GDPR", "HIPAA", "PCI-DSS", "SOC 2", "ISO 27001"],
|
42 |
+
label="Compliance Standard",
|
43 |
+
value="GDPR"
|
44 |
+
)
|
45 |
+
analyze_button = gr.Button("Analyze Compliance")
|
46 |
+
|
47 |
+
# Second column for output
|
48 |
+
with gr.Column():
|
49 |
+
gr.Markdown("### Compliance Suggestions") # This acts as the label for the output
|
50 |
+
output_markdown = gr.Markdown()
|
51 |
+
|
52 |
+
# Link button to function with inputs and outputs
|
53 |
+
analyze_button.click(fn=analyze_compliance_stream, inputs=[code_input, compliance_standard], outputs=output_markdown)
|
54 |
+
|
55 |
+
# Run the Gradio app
|
56 |
+
app.launch()
|