yuzhounie commited on
Commit
2cc3ccf
Β·
1 Parent(s): b2d9ce7

Integrate API call and enhance Gradio UI for code analysis

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +106 -4
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .idea
app.py CHANGED
@@ -1,7 +1,109 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
 
5
+ # API configuration
6
+ API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:8000")
7
+ API_KEY = os.getenv("API_KEY", "")
8
 
9
+ def analyze_code(code: str, language: str, model: str) -> str:
10
+ """Send code to the vulnerability analysis API and return results."""
11
+ if not code.strip():
12
+ return "Please enter some code to analyze."
13
+
14
+ api_url = f"{API_BASE_URL}/analyze"
15
+
16
+ headers = {
17
+ "Content-Type": "application/json",
18
+ "Authorization": f"Bearer {API_KEY}"
19
+ }
20
+
21
+ payload = {
22
+ "code": code,
23
+ "model": model,
24
+ "language": language
25
+ }
26
+
27
+ try:
28
+ response = requests.post(api_url, json=payload, headers=headers, timeout=60)
29
+
30
+ if response.status_code == 403:
31
+ error_detail = response.json().get("detail", "Token limit exceeded.")
32
+ return f"Error: {error_detail}"
33
+
34
+ if response.status_code != 200:
35
+ error_detail = response.json().get("detail", f"API error: {response.status_code}")
36
+ return f"Error: {error_detail}"
37
+
38
+ result = response.json()
39
+
40
+ # Format the result
41
+ status = result.get("result", {}).get("status", "unknown")
42
+ cwe_type = result.get("result", {}).get("cweType", "N/A")
43
+ model_used = result.get("result", {}).get("model", model)
44
+ explanation = result.get("result", {}).get("response", "")
45
+
46
+ if status == "yes":
47
+ output = f"⚠️ **Vulnerability Detected**\n\n"
48
+ output += f"**CWE Type:** {cwe_type}\n"
49
+ output += f"**Model:** {model_used}\n\n"
50
+ output += f"**Analysis:**\n{explanation}"
51
+ else:
52
+ output = f"βœ… **No Vulnerability Detected**\n\n"
53
+ output += f"**Model:** {model_used}\n\n"
54
+ output += f"**Analysis:**\n{explanation}"
55
+
56
+ return output
57
+
58
+ except requests.exceptions.Timeout:
59
+ return "Error: Request timed out. Please try again."
60
+ except requests.exceptions.ConnectionError:
61
+ return f"Error: Could not connect to API at {API_BASE_URL}"
62
+ except Exception as e:
63
+ return f"Error: {str(e)}"
64
+
65
+ # Language options
66
+ LANGUAGES = [
67
+ "python", "javascript", "typescript", "java", "c", "cpp",
68
+ "csharp", "go", "rust", "php", "ruby", "swift", "kotlin"
69
+ ]
70
+
71
+ # Model options
72
+ MODELS = ["virtueguard-code", "claude-4-sonnet", "gpt-4.1"]
73
+
74
+ # Create Gradio interface
75
+ with gr.Blocks(title="Code Vulnerability Scanner") as demo:
76
+ gr.Markdown("# πŸ” Code Vulnerability Scanner")
77
+ gr.Markdown("Analyze your code for potential security vulnerabilities using LLM-based detection.")
78
+
79
+ with gr.Row():
80
+ with gr.Column(scale=2):
81
+ code_input = gr.Code(
82
+ label="Code to Analyze",
83
+ language="python",
84
+ lines=15,
85
+ placeholder="Paste your code here..."
86
+ )
87
+ with gr.Column(scale=1):
88
+ language_dropdown = gr.Dropdown(
89
+ choices=LANGUAGES,
90
+ value="python",
91
+ label="Programming Language"
92
+ )
93
+ model_dropdown = gr.Dropdown(
94
+ choices=MODELS,
95
+ value="virtueguard-code",
96
+ label="Model"
97
+ )
98
+ analyze_btn = gr.Button("πŸ” Analyze Code", variant="primary")
99
+
100
+ result_output = gr.Markdown(label="Analysis Result")
101
+
102
+ analyze_btn.click(
103
+ fn=analyze_code,
104
+ inputs=[code_input, language_dropdown, model_dropdown],
105
+ outputs=result_output
106
+ )
107
+
108
+ if __name__ == "__main__":
109
+ demo.launch()