Update app.py
Browse files
app.py
CHANGED
|
@@ -1,254 +1,413 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM,
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
APP_TITLE = "π STHASI Code Review Assistant"
|
| 9 |
-
APP_DESCRIPTION = """
|
| 10 |
-
An AI-powered code review assistant that analyzes code quality, detects vulnerabilities, and suggests improvements.
|
| 11 |
-
Built with β€οΈ by **[Syed Tahir Hussan](https://www.linkedin.com/in/syedtahirhussan/)**
|
| 12 |
-
|
| 13 |
-
**Features:**
|
| 14 |
-
- π‘οΈ Security vulnerability detection across 13+ languages
|
| 15 |
-
- π Code quality analysis
|
| 16 |
-
- π‘ Improvement suggestions
|
| 17 |
-
- π Bug detection
|
| 18 |
-
|
| 19 |
-
**Supported Languages:** Python, JavaScript, Java, C, C++, C#, Go, PHP, Ruby, Swift, Kotlin, TypeScript, Fortran
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
FOOTER = """
|
| 23 |
-
---
|
| 24 |
-
**STHASI Code Review Assistant** | Built with β€οΈ by [Syed Tahir Hussan](https://www.linkedin.com/in/syedtahirhussan/)
|
| 25 |
-
|
| 26 |
-
π§ [GitHub](https://github.com/SyedTahirHussan) | πΌ [LinkedIn](https://www.linkedin.com/in/syedtahirhussan/)
|
| 27 |
-
|
| 28 |
-
*Powered by CodeAstra-7B & Hugging Face Spaces*
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
# Global variables for model
|
| 32 |
model = None
|
| 33 |
tokenizer = None
|
| 34 |
-
|
| 35 |
|
| 36 |
def load_model():
|
| 37 |
-
"""Load the
|
| 38 |
-
global model, tokenizer,
|
| 39 |
|
| 40 |
-
if
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
peft_model_id = "rootxhacker/CodeAstra-7B"
|
| 45 |
-
config = PeftConfig.from_pretrained(peft_model_id)
|
| 46 |
|
| 47 |
-
|
| 48 |
model = AutoModelForCausalLM.from_pretrained(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
device_map='auto',
|
| 53 |
trust_remote_code=True
|
| 54 |
)
|
| 55 |
-
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
| 56 |
-
|
| 57 |
-
# Load PEFT adapter
|
| 58 |
-
model = PeftModel.from_pretrained(model, peft_model_id)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
Args:
|
| 70 |
-
code_input: The code to analyze
|
| 71 |
-
analysis_type: Type of analysis (vulnerability, quality, both)
|
| 72 |
-
language: Programming language
|
| 73 |
-
"""
|
| 74 |
-
global model, tokenizer, model_loaded
|
| 75 |
-
|
| 76 |
-
if not code_input.strip():
|
| 77 |
-
return "β οΈ Please enter some code to analyze."
|
| 78 |
-
|
| 79 |
-
if not model_loaded:
|
| 80 |
-
return "β οΈ Model not loaded. Please wait for initialization or refresh the page."
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
```
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
```
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
```
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Generate response
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
result = result.split("Comprehensive Analysis:")[-1].strip()
|
| 138 |
-
|
| 139 |
-
return f"## π Analysis Results\n\n{result}"
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
-
return f"β Error during analysis: {str(e)}\n\nPlease try again or contact support."
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
"Python - SQL Injection": '''def user_login(username, password):
|
| 147 |
-
query = f"SELECT * FROM users WHERE username='{username}' AND password='{password}'"
|
| 148 |
-
cursor.execute(query)
|
| 149 |
-
return cursor.fetchone()''',
|
| 150 |
-
|
| 151 |
-
"JavaScript - XSS": '''function displayUserInput() {
|
| 152 |
-
var userInput = document.getElementById("input").value;
|
| 153 |
-
document.getElementById("output").innerHTML = userInput;
|
| 154 |
-
}''',
|
| 155 |
-
|
| 156 |
-
"Python - Hardcoded Secrets": '''import requests
|
| 157 |
-
|
| 158 |
-
API_KEY = "sk-1234567890abcdef"
|
| 159 |
-
DB_PASSWORD = "admin123"
|
| 160 |
-
|
| 161 |
-
def connect_to_api():
|
| 162 |
-
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 163 |
-
return requests.get("https://api.example.com", headers=headers)''',
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
"
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
with gr.Row():
|
| 189 |
-
with gr.Column(scale=
|
|
|
|
|
|
|
| 190 |
code_input = gr.Code(
|
| 191 |
-
label="
|
| 192 |
language="python",
|
| 193 |
lines=15,
|
| 194 |
-
placeholder="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
)
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
analyze_btn = gr.Button("π Analyze Code", variant="primary", size="lg")
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
gr.Markdown(
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
label="Load Example",
|
| 219 |
-
interactive=True
|
| 220 |
)
|
| 221 |
-
|
| 222 |
-
with gr.Column(scale=2):
|
| 223 |
-
output = gr.Markdown(label="Analysis Results")
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
|
| 227 |
-
fn=analyze_code,
|
| 228 |
-
inputs=[code_input, analysis_type, language_select],
|
| 229 |
-
outputs=output
|
| 230 |
-
)
|
| 231 |
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
)
|
| 242 |
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
-
#
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
-
return
|
| 249 |
|
| 250 |
-
# Launch the app
|
| 251 |
if __name__ == "__main__":
|
| 252 |
-
demo =
|
| 253 |
-
demo.queue()
|
| 254 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
import re
|
|
|
|
| 5 |
|
| 6 |
+
# Global variables to store model and tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model = None
|
| 8 |
tokenizer = None
|
| 9 |
+
pipe = None
|
| 10 |
|
| 11 |
def load_model():
|
| 12 |
+
"""Load the model and tokenizer"""
|
| 13 |
+
global model, tokenizer, pipe
|
| 14 |
|
| 15 |
+
if pipe is None:
|
| 16 |
+
print("Loading model...")
|
| 17 |
+
model_name = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_name,
|
| 22 |
+
torch_dtype=torch.bfloat16,
|
| 23 |
+
device_map="auto",
|
|
|
|
| 24 |
trust_remote_code=True
|
| 25 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
pipe = pipeline(
|
| 28 |
+
"text-generation",
|
| 29 |
+
model=model,
|
| 30 |
+
tokenizer=tokenizer,
|
| 31 |
+
torch_dtype=torch.bfloat16,
|
| 32 |
+
device_map="auto"
|
| 33 |
+
)
|
| 34 |
+
print("Model loaded successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return pipe
|
| 37 |
+
|
| 38 |
+
def analyze_code(code, language, analysis_type, temperature, max_tokens):
|
| 39 |
+
"""Analyze code for quality, security, and improvements"""
|
| 40 |
try:
|
| 41 |
+
# Load model if not already loaded
|
| 42 |
+
generator = load_model()
|
| 43 |
+
|
| 44 |
+
# Construct detailed prompt based on analysis type
|
| 45 |
+
if analysis_type == "π Full Analysis (Quality + Security + Improvements)":
|
| 46 |
+
prompt = f"""You are an expert code reviewer. Analyze the following {language} code comprehensively:
|
| 47 |
+
|
| 48 |
+
**Code to Review:**
|
| 49 |
+
```{language}
|
| 50 |
+
{code}
|
| 51 |
```
|
| 52 |
|
| 53 |
+
**Please provide a detailed analysis including:**
|
| 54 |
+
|
| 55 |
+
1. **Code Quality Assessment:**
|
| 56 |
+
- Code structure and organization
|
| 57 |
+
- Naming conventions and readability
|
| 58 |
+
- Code complexity and maintainability
|
| 59 |
+
- Best practices adherence
|
| 60 |
|
| 61 |
+
2. **Security Vulnerabilities:**
|
| 62 |
+
- Identify security flaws (SQL injection, XSS, authentication issues, etc.)
|
| 63 |
+
- Rate severity (Critical/High/Medium/Low)
|
| 64 |
+
- Explain potential exploits
|
| 65 |
+
|
| 66 |
+
3. **Improvement Suggestions:**
|
| 67 |
+
- Performance optimizations
|
| 68 |
+
- Refactoring recommendations
|
| 69 |
+
- Better coding patterns
|
| 70 |
+
- Updated or improved code snippets
|
| 71 |
+
|
| 72 |
+
4. **Overall Rating:** Rate the code quality (1-10) with justification.
|
| 73 |
+
|
| 74 |
+
Provide specific, actionable feedback with code examples where applicable."""
|
| 75 |
+
|
| 76 |
+
elif analysis_type == "π‘οΈ Security Vulnerabilities Only":
|
| 77 |
+
prompt = f"""You are a cybersecurity expert specializing in code security. Analyze the following {language} code for security vulnerabilities:
|
| 78 |
+
|
| 79 |
+
**Code to Review:**
|
| 80 |
+
```{language}
|
| 81 |
+
{code}
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
**Please identify:**
|
| 85 |
+
|
| 86 |
+
1. **Security Vulnerabilities:**
|
| 87 |
+
- List all security issues (injection attacks, authentication flaws, data exposure, etc.)
|
| 88 |
+
- Severity rating for each (Critical/High/Medium/Low)
|
| 89 |
+
- CVE references if applicable
|
| 90 |
+
- Potential attack vectors
|
| 91 |
+
|
| 92 |
+
2. **Secure Code Examples:**
|
| 93 |
+
- Provide fixed/secure versions of vulnerable code
|
| 94 |
+
- Explain why the changes improve security
|
| 95 |
+
|
| 96 |
+
3. **Security Best Practices:**
|
| 97 |
+
- Recommendations specific to this code
|
| 98 |
+
- Industry-standard security patterns
|
| 99 |
+
|
| 100 |
+
Focus exclusively on security concerns."""
|
| 101 |
+
|
| 102 |
+
elif analysis_type == "β¨ Code Quality & Improvements":
|
| 103 |
+
prompt = f"""You are a senior software engineer reviewing code quality. Analyze the following {language} code:
|
| 104 |
+
|
| 105 |
+
**Code to Review:**
|
| 106 |
+
```{language}
|
| 107 |
+
{code}
|
| 108 |
```
|
| 109 |
|
| 110 |
+
**Please evaluate:**
|
| 111 |
+
|
| 112 |
+
1. **Code Quality:**
|
| 113 |
+
- Readability and maintainability
|
| 114 |
+
- Code organization and structure
|
| 115 |
+
- Naming conventions
|
| 116 |
+
- Documentation quality
|
| 117 |
+
|
| 118 |
+
2. **Performance:**
|
| 119 |
+
- Efficiency concerns
|
| 120 |
+
- Resource utilization
|
| 121 |
+
- Algorithmic complexity
|
| 122 |
|
| 123 |
+
3. **Improvements:**
|
| 124 |
+
- Refactoring suggestions
|
| 125 |
+
- Design pattern recommendations
|
| 126 |
+
- Better implementation approaches
|
| 127 |
+
- Optimized code examples
|
| 128 |
+
|
| 129 |
+
4. **Best Practices:**
|
| 130 |
+
- Language-specific conventions
|
| 131 |
+
- Industry standards compliance
|
| 132 |
+
|
| 133 |
+
Provide constructive feedback with examples."""
|
| 134 |
+
|
| 135 |
+
else: # Bug Detection
|
| 136 |
+
prompt = f"""You are an expert debugger. Analyze the following {language} code to find bugs and issues:
|
| 137 |
+
|
| 138 |
+
**Code to Review:**
|
| 139 |
+
```{language}
|
| 140 |
+
{code}
|
| 141 |
```
|
| 142 |
|
| 143 |
+
**Please identify:**
|
| 144 |
+
|
| 145 |
+
1. **Bugs and Errors:**
|
| 146 |
+
- Syntax errors
|
| 147 |
+
- Logic errors
|
| 148 |
+
- Runtime errors
|
| 149 |
+
- Edge case failures
|
| 150 |
+
|
| 151 |
+
2. **Potential Issues:**
|
| 152 |
+
- Null pointer exceptions
|
| 153 |
+
- Memory leaks
|
| 154 |
+
- Race conditions
|
| 155 |
+
- Unhandled exceptions
|
| 156 |
+
|
| 157 |
+
3. **Fixes:**
|
| 158 |
+
- Corrected code snippets
|
| 159 |
+
- Explanation of the fix
|
| 160 |
+
|
| 161 |
+
Focus on identifying and fixing bugs."""
|
| 162 |
+
|
| 163 |
# Generate response
|
| 164 |
+
messages = [{"role": "user", "content": prompt}]
|
| 165 |
+
|
| 166 |
+
response = generator(
|
| 167 |
+
messages,
|
| 168 |
+
max_new_tokens=max_tokens,
|
| 169 |
+
temperature=temperature,
|
| 170 |
+
top_p=0.95,
|
| 171 |
+
do_sample=True,
|
| 172 |
+
repetition_penalty=1.1
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# Extract the generated text
|
| 176 |
+
result = response[0]["generated_text"]
|
| 177 |
+
|
| 178 |
+
# Get only the assistant's response
|
| 179 |
+
if isinstance(result, list):
|
| 180 |
+
assistant_response = result[-1]["content"]
|
| 181 |
+
else:
|
| 182 |
+
assistant_response = result
|
| 183 |
+
|
| 184 |
+
return assistant_response
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
except Exception as e:
|
| 187 |
+
return f"β **Error during analysis:** {str(e)}\n\nPlease try again or contact support."
|
| 188 |
|
| 189 |
+
def create_demo():
|
| 190 |
+
"""Create the Gradio interface"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
# Custom CSS for better styling
|
| 193 |
+
custom_css = """
|
| 194 |
+
.gradio-container {
|
| 195 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 196 |
+
}
|
| 197 |
+
.header {
|
| 198 |
+
text-align: center;
|
| 199 |
+
padding: 20px;
|
| 200 |
+
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 201 |
+
color: white;
|
| 202 |
+
border-radius: 10px;
|
| 203 |
+
margin-bottom: 20px;
|
| 204 |
+
}
|
| 205 |
+
.footer {
|
| 206 |
+
text-align: center;
|
| 207 |
+
padding: 15px;
|
| 208 |
+
margin-top: 20px;
|
| 209 |
+
border-top: 1px solid #e0e0e0;
|
| 210 |
+
}
|
| 211 |
+
.example-box {
|
| 212 |
+
background-color: #f5f5f5;
|
| 213 |
+
padding: 15px;
|
| 214 |
+
border-radius: 5px;
|
| 215 |
+
margin: 10px 0;
|
| 216 |
+
}
|
| 217 |
+
"""
|
| 218 |
|
| 219 |
+
with gr.Blocks(css=custom_css, title="STHASI Code Review Assistant", theme=gr.themes.Soft()) as demo:
|
| 220 |
+
|
| 221 |
+
# Header
|
| 222 |
+
gr.HTML("""
|
| 223 |
+
<div class="header">
|
| 224 |
+
<h1>π STHASI Code Review Assistant</h1>
|
| 225 |
+
<p style="font-size: 18px; margin: 10px 0;">
|
| 226 |
+
AI-Powered Code Quality & Security Analysis
|
| 227 |
+
</p>
|
| 228 |
+
<p style="font-size: 14px; opacity: 0.9;">
|
| 229 |
+
Built with β€οΈ by <a href="https://www.linkedin.com/in/syedtahirhussan/" style="color: white; text-decoration: underline;" target="_blank">Syed Tahir Hussan</a>
|
| 230 |
+
</p>
|
| 231 |
+
</div>
|
| 232 |
+
""")
|
| 233 |
+
|
| 234 |
+
gr.Markdown("""
|
| 235 |
+
## π Welcome to STHASI Code Review Assistant!
|
| 236 |
+
|
| 237 |
+
This AI-powered tool analyzes your code for:
|
| 238 |
+
- β
**Code Quality**: Structure, readability, and best practices
|
| 239 |
+
- π‘οΈ **Security Vulnerabilities**: SQL injection, XSS, authentication flaws, and more
|
| 240 |
+
- π‘ **Improvements**: Performance optimizations and refactoring suggestions
|
| 241 |
+
- π **Bug Detection**: Logic errors, edge cases, and potential issues
|
| 242 |
+
|
| 243 |
+
**Powered by Qwen2.5-Coder-7B-Instruct** - State-of-the-art code understanding model
|
| 244 |
+
""")
|
| 245 |
|
| 246 |
with gr.Row():
|
| 247 |
+
with gr.Column(scale=1):
|
| 248 |
+
gr.Markdown("### π Input Code")
|
| 249 |
+
|
| 250 |
code_input = gr.Code(
|
| 251 |
+
label="Paste your code here",
|
| 252 |
language="python",
|
| 253 |
lines=15,
|
| 254 |
+
placeholder="# Paste your code here...\ndef example_function():\n pass"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
language = gr.Dropdown(
|
| 258 |
+
choices=["python", "javascript", "java", "cpp", "c", "csharp", "go", "rust", "php", "ruby", "typescript", "sql", "bash", "kotlin", "swift"],
|
| 259 |
+
value="python",
|
| 260 |
+
label="Programming Language",
|
| 261 |
+
info="Select the programming language of your code"
|
| 262 |
)
|
| 263 |
|
| 264 |
+
analysis_type = gr.Radio(
|
| 265 |
+
choices=[
|
| 266 |
+
"π Full Analysis (Quality + Security + Improvements)",
|
| 267 |
+
"π‘οΈ Security Vulnerabilities Only",
|
| 268 |
+
"β¨ Code Quality & Improvements",
|
| 269 |
+
"π Bug Detection"
|
| 270 |
+
],
|
| 271 |
+
value="π Full Analysis (Quality + Security + Improvements)",
|
| 272 |
+
label="Analysis Type",
|
| 273 |
+
info="Choose what aspect to focus on"
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 277 |
+
temperature = gr.Slider(
|
| 278 |
+
minimum=0.1,
|
| 279 |
+
maximum=1.0,
|
| 280 |
+
value=0.7,
|
| 281 |
+
step=0.1,
|
| 282 |
+
label="Temperature",
|
| 283 |
+
info="Higher = more creative, Lower = more focused"
|
| 284 |
)
|
| 285 |
|
| 286 |
+
max_tokens = gr.Slider(
|
| 287 |
+
minimum=512,
|
| 288 |
+
maximum=4096,
|
| 289 |
+
value=2048,
|
| 290 |
+
step=256,
|
| 291 |
+
label="Max Tokens",
|
| 292 |
+
info="Maximum length of the analysis"
|
| 293 |
)
|
| 294 |
|
| 295 |
analyze_btn = gr.Button("π Analyze Code", variant="primary", size="lg")
|
| 296 |
+
|
| 297 |
+
with gr.Column(scale=1):
|
| 298 |
+
gr.Markdown("### π Analysis Results")
|
| 299 |
|
| 300 |
+
output = gr.Markdown(
|
| 301 |
+
label="Review Results",
|
| 302 |
+
value="*Analysis results will appear here...*"
|
|
|
|
|
|
|
| 303 |
)
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
+
# Examples section
|
| 306 |
+
gr.Markdown("### π‘ Example Codes to Try")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
+
with gr.Row():
|
| 309 |
+
with gr.Column():
|
| 310 |
+
gr.Markdown("""
|
| 311 |
+
**Example 1: SQL Injection Vulnerability**
|
| 312 |
+
```python
|
| 313 |
+
def get_user(username):
|
| 314 |
+
query = f"SELECT * FROM users WHERE name = '{username}'"
|
| 315 |
+
return db.execute(query)
|
| 316 |
+
```
|
| 317 |
+
""")
|
| 318 |
+
|
| 319 |
+
with gr.Column():
|
| 320 |
+
gr.Markdown("""
|
| 321 |
+
**Example 2: Insecure Password Storage**
|
| 322 |
+
```python
|
| 323 |
+
def store_password(password):
|
| 324 |
+
# Bad practice: storing plain text
|
| 325 |
+
with open('passwords.txt', 'a') as f:
|
| 326 |
+
f.write(password + '\\n')
|
| 327 |
+
```
|
| 328 |
+
""")
|
| 329 |
|
| 330 |
+
# Examples for quick testing
|
| 331 |
+
gr.Examples(
|
| 332 |
+
examples=[
|
| 333 |
+
[
|
| 334 |
+
"""def login(username, password):
|
| 335 |
+
query = f"SELECT * FROM users WHERE username='{username}' AND password='{password}'"
|
| 336 |
+
result = db.execute(query)
|
| 337 |
+
if result:
|
| 338 |
+
return True
|
| 339 |
+
return False""",
|
| 340 |
+
"python",
|
| 341 |
+
"π‘οΈ Security Vulnerabilities Only",
|
| 342 |
+
0.7,
|
| 343 |
+
2048
|
| 344 |
+
],
|
| 345 |
+
[
|
| 346 |
+
"""function validateEmail(email) {
|
| 347 |
+
return email.includes('@');
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
function processUserInput(input) {
|
| 351 |
+
document.getElementById('output').innerHTML = input;
|
| 352 |
+
}""",
|
| 353 |
+
"javascript",
|
| 354 |
+
"π Full Analysis (Quality + Security + Improvements)",
|
| 355 |
+
0.7,
|
| 356 |
+
2048
|
| 357 |
+
],
|
| 358 |
+
[
|
| 359 |
+
"""public class Calculator {
|
| 360 |
+
public int divide(int a, int b) {
|
| 361 |
+
return a / b;
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
public String getUserData(String userId) {
|
| 365 |
+
return "SELECT * FROM users WHERE id=" + userId;
|
| 366 |
+
}
|
| 367 |
+
}""",
|
| 368 |
+
"java",
|
| 369 |
+
"π Bug Detection",
|
| 370 |
+
0.7,
|
| 371 |
+
2048
|
| 372 |
+
]
|
| 373 |
+
],
|
| 374 |
+
inputs=[code_input, language, analysis_type, temperature, max_tokens],
|
| 375 |
+
outputs=output,
|
| 376 |
+
fn=analyze_code,
|
| 377 |
+
cache_examples=False
|
| 378 |
)
|
| 379 |
|
| 380 |
+
# Footer
|
| 381 |
+
gr.HTML("""
|
| 382 |
+
<div class="footer">
|
| 383 |
+
<p>
|
| 384 |
+
<strong>Built with β€οΈ by Syed Tahir Hussan</strong><br>
|
| 385 |
+
π <a href="https://www.linkedin.com/in/syedtahirhussan/" target="_blank">LinkedIn</a> |
|
| 386 |
+
π» <a href="https://github.com/SyedTahirHussan" target="_blank">GitHub</a><br>
|
| 387 |
+
<em>Powered by Qwen2.5-Coder-7B-Instruct & Gradio 5.49.1</em>
|
| 388 |
+
</p>
|
| 389 |
+
<p style="font-size: 12px; color: #666; margin-top: 10px;">
|
| 390 |
+
β οΈ Note: This is an AI-powered tool. Always verify critical security findings with manual review and security experts.
|
| 391 |
+
</p>
|
| 392 |
+
</div>
|
| 393 |
+
""")
|
| 394 |
|
| 395 |
+
# Event handler
|
| 396 |
+
analyze_btn.click(
|
| 397 |
+
fn=analyze_code,
|
| 398 |
+
inputs=[code_input, language, analysis_type, temperature, max_tokens],
|
| 399 |
+
outputs=output,
|
| 400 |
+
api_name="analyze"
|
| 401 |
+
)
|
| 402 |
|
| 403 |
+
return demo
|
| 404 |
|
|
|
|
| 405 |
if __name__ == "__main__":
|
| 406 |
+
demo = create_demo()
|
| 407 |
+
demo.queue(max_size=10)
|
| 408 |
+
demo.launch(
|
| 409 |
+
server_name="0.0.0.0",
|
| 410 |
+
server_port=7860,
|
| 411 |
+
share=False,
|
| 412 |
+
show_error=True
|
| 413 |
+
)
|