Spaces:
Sleeping
Sleeping
Joash
commited on
Commit
·
a77dc20
1
Parent(s):
9eddb40
Convert to Gradio app for simpler deployment
Browse files- README.md +23 -45
- app.py +140 -0
- requirements.txt +9 -33
README.md
CHANGED
@@ -3,7 +3,9 @@ title: Code Review Assistant
|
|
3 |
emoji: 🤖
|
4 |
colorFrom: blue
|
5 |
colorTo: green
|
6 |
-
sdk:
|
|
|
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
@@ -21,20 +23,14 @@ An automated code review system powered by Gemma-2b that provides intelligent co
|
|
21 |
|
22 |
### LLMOps Integration
|
23 |
- Uses Gemma-2b for intelligent code analysis
|
24 |
-
-
|
25 |
-
-
|
26 |
|
27 |
-
###
|
28 |
-
-
|
29 |
-
-
|
30 |
-
-
|
31 |
-
-
|
32 |
-
|
33 |
-
### Modern Web Interface
|
34 |
-
- Interactive code submission
|
35 |
-
- Syntax highlighting with CodeMirror
|
36 |
-
- Real-time review results
|
37 |
-
- Metrics visualization
|
38 |
|
39 |
## Environment Variables
|
40 |
|
@@ -42,49 +38,31 @@ The following environment variables need to be set in your Hugging Face Space:
|
|
42 |
|
43 |
- `HUGGING_FACE_TOKEN`: Your Hugging Face API token (required)
|
44 |
- `MODEL_NAME`: google/gemma-2b-it (default)
|
45 |
-
- `DEBUG`: false (default)
|
46 |
-
- `LOG_LEVEL`: INFO (default)
|
47 |
-
- `PORT`: 7860 (default)
|
48 |
-
|
49 |
-
## API Endpoints
|
50 |
-
|
51 |
-
- `POST /api/v1/review`: Submit code for review
|
52 |
-
```json
|
53 |
-
{
|
54 |
-
"code": "your code here",
|
55 |
-
"language": "python"
|
56 |
-
}
|
57 |
-
```
|
58 |
-
- `GET /api/v1/metrics`: Get system metrics
|
59 |
-
- `GET /api/v1/history`: Get review history
|
60 |
-
- `GET /health`: Check system health
|
61 |
|
62 |
## Usage
|
63 |
|
64 |
-
1. Enter your code in the
|
65 |
-
2. Select the programming language
|
66 |
-
3. Click "Submit
|
67 |
4. View the detailed analysis including:
|
68 |
- Critical issues
|
69 |
- Suggested improvements
|
70 |
- Best practices
|
71 |
- Security considerations
|
72 |
|
73 |
-
##
|
74 |
|
75 |
-
|
76 |
-
-
|
77 |
-
-
|
78 |
-
- Number of suggestions per review
|
79 |
-
- Daily usage statistics
|
80 |
|
81 |
-
##
|
82 |
|
83 |
-
This
|
84 |
-
-
|
85 |
-
-
|
86 |
-
-
|
87 |
-
-
|
88 |
|
89 |
## License
|
90 |
|
|
|
3 |
emoji: 🤖
|
4 |
colorFrom: blue
|
5 |
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.0.0
|
8 |
+
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
|
|
23 |
|
24 |
### LLMOps Integration
|
25 |
- Uses Gemma-2b for intelligent code analysis
|
26 |
+
- Provides detailed code analysis
|
27 |
+
- Generates actionable suggestions
|
28 |
|
29 |
+
### User Interface
|
30 |
+
- Simple and intuitive Gradio interface
|
31 |
+
- Code input with syntax highlighting
|
32 |
+
- Language selection dropdown
|
33 |
+
- Example code snippets included
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
## Environment Variables
|
36 |
|
|
|
38 |
|
39 |
- `HUGGING_FACE_TOKEN`: Your Hugging Face API token (required)
|
40 |
- `MODEL_NAME`: google/gemma-2b-it (default)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
## Usage
|
43 |
|
44 |
+
1. Enter your code in the text box
|
45 |
+
2. Select the programming language from the dropdown
|
46 |
+
3. Click "Submit" to get the review
|
47 |
4. View the detailed analysis including:
|
48 |
- Critical issues
|
49 |
- Suggested improvements
|
50 |
- Best practices
|
51 |
- Security considerations
|
52 |
|
53 |
+
## Example Code
|
54 |
|
55 |
+
Try the included example code snippets to see how the review system works:
|
56 |
+
- Python function example
|
57 |
+
- JavaScript array processing example
|
|
|
|
|
58 |
|
59 |
+
## Model Details
|
60 |
|
61 |
+
This application uses the Gemma-2b-it model from Google, which is:
|
62 |
+
- Optimized for instruction following
|
63 |
+
- Capable of detailed code analysis
|
64 |
+
- Efficient for deployment
|
65 |
+
- Suitable for code review tasks
|
66 |
|
67 |
## License
|
68 |
|
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
from huggingface_hub import login
|
5 |
+
import os
|
6 |
+
import logging
|
7 |
+
from datetime import datetime
|
8 |
+
|
9 |
+
# Configure logging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
# Environment variables
|
14 |
+
HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
|
15 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2b-it")
|
16 |
+
|
17 |
+
class CodeReviewer:
|
18 |
+
def __init__(self):
|
19 |
+
self.model = None
|
20 |
+
self.tokenizer = None
|
21 |
+
self.device = "cpu"
|
22 |
+
self.initialize_model()
|
23 |
+
|
24 |
+
def initialize_model(self):
|
25 |
+
"""Initialize the model and tokenizer."""
|
26 |
+
try:
|
27 |
+
if HF_TOKEN:
|
28 |
+
login(token=HF_TOKEN)
|
29 |
+
|
30 |
+
logger.info("Loading tokenizer...")
|
31 |
+
self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
32 |
+
|
33 |
+
logger.info("Loading model...")
|
34 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
35 |
+
MODEL_NAME,
|
36 |
+
device_map={"": self.device},
|
37 |
+
torch_dtype=torch.float32,
|
38 |
+
low_cpu_mem_usage=True
|
39 |
+
)
|
40 |
+
logger.info("Model loaded successfully")
|
41 |
+
except Exception as e:
|
42 |
+
logger.error(f"Error initializing model: {e}")
|
43 |
+
raise
|
44 |
+
|
45 |
+
def create_review_prompt(self, code: str, language: str) -> str:
|
46 |
+
"""Create a structured prompt for code review."""
|
47 |
+
return f"""Review this {language} code. List specific points in these sections:
|
48 |
+
Issues:
|
49 |
+
Improvements:
|
50 |
+
Best Practices:
|
51 |
+
Security:
|
52 |
+
|
53 |
+
Code:
|
54 |
+
```{language}
|
55 |
+
{code}
|
56 |
+
```"""
|
57 |
+
|
58 |
+
def review_code(self, code: str, language: str) -> str:
|
59 |
+
"""Perform code review using the model."""
|
60 |
+
try:
|
61 |
+
prompt = self.create_review_prompt(code, language)
|
62 |
+
|
63 |
+
inputs = self.tokenizer(
|
64 |
+
prompt,
|
65 |
+
return_tensors="pt",
|
66 |
+
truncation=True,
|
67 |
+
max_length=512,
|
68 |
+
padding=True
|
69 |
+
)
|
70 |
+
|
71 |
+
with torch.no_grad():
|
72 |
+
outputs = self.model.generate(
|
73 |
+
**inputs,
|
74 |
+
max_new_tokens=512,
|
75 |
+
do_sample=True,
|
76 |
+
temperature=0.7,
|
77 |
+
top_p=0.95,
|
78 |
+
num_beams=1,
|
79 |
+
early_stopping=True
|
80 |
+
)
|
81 |
+
|
82 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
83 |
+
return response[len(prompt):].strip()
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
logger.error(f"Error during code review: {e}")
|
87 |
+
return f"Error performing code review: {str(e)}"
|
88 |
+
|
89 |
+
# Initialize the reviewer
|
90 |
+
reviewer = CodeReviewer()
|
91 |
+
|
92 |
+
def review_code_interface(code: str, language: str) -> str:
|
93 |
+
"""Gradio interface function for code review."""
|
94 |
+
if not code.strip():
|
95 |
+
return "Please enter some code to review."
|
96 |
+
|
97 |
+
try:
|
98 |
+
result = reviewer.review_code(code, language)
|
99 |
+
return result
|
100 |
+
except Exception as e:
|
101 |
+
return f"Error: {str(e)}"
|
102 |
+
|
103 |
+
# Create Gradio interface
|
104 |
+
iface = gr.Interface(
|
105 |
+
fn=review_code_interface,
|
106 |
+
inputs=[
|
107 |
+
gr.Textbox(
|
108 |
+
lines=10,
|
109 |
+
placeholder="Enter your code here...",
|
110 |
+
label="Code"
|
111 |
+
),
|
112 |
+
gr.Dropdown(
|
113 |
+
choices=["python", "javascript", "java", "cpp", "typescript", "go", "rust"],
|
114 |
+
value="python",
|
115 |
+
label="Language"
|
116 |
+
)
|
117 |
+
],
|
118 |
+
outputs=gr.Textbox(
|
119 |
+
label="Review Results",
|
120 |
+
lines=10
|
121 |
+
),
|
122 |
+
title="Code Review Assistant",
|
123 |
+
description="An automated code review system powered by Gemma-2b that provides intelligent code analysis and suggestions for improvements.",
|
124 |
+
examples=[
|
125 |
+
["""def add_numbers(a, b):
|
126 |
+
return a + b""", "python"],
|
127 |
+
["""function calculateSum(numbers) {
|
128 |
+
let sum = 0;
|
129 |
+
for(let i = 0; i < numbers.length; i++) {
|
130 |
+
sum += numbers[i];
|
131 |
+
}
|
132 |
+
return sum;
|
133 |
+
}""", "javascript"]
|
134 |
+
],
|
135 |
+
theme=gr.themes.Soft()
|
136 |
+
)
|
137 |
+
|
138 |
+
# Launch the app
|
139 |
+
if __name__ == "__main__":
|
140 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,36 +1,12 @@
|
|
1 |
# Core dependencies
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
# Model and ML
|
9 |
-
transformers>=4.39.0 # Updated to latest version for Gemma support
|
10 |
-
# torch is installed separately in Dockerfile
|
11 |
-
numpy<2.0.0 # Added explicit numpy version
|
12 |
-
accelerate==0.27.2
|
13 |
-
safetensors==0.4.2
|
14 |
-
bitsandbytes==0.41.1 # For model quantization
|
15 |
-
sentencepiece==0.1.99
|
16 |
-
|
17 |
-
# Monitoring and metrics
|
18 |
-
prometheus-client==0.17.1
|
19 |
-
prometheus-fastapi-instrumentator==6.1.0
|
20 |
-
|
21 |
-
# Database
|
22 |
-
sqlalchemy==2.0.23
|
23 |
-
alembic==1.12.1
|
24 |
-
psycopg2-binary==2.9.9
|
25 |
-
|
26 |
-
# Testing
|
27 |
-
pytest==7.4.3
|
28 |
-
pytest-asyncio==0.21.1
|
29 |
-
httpx==0.24.1
|
30 |
|
31 |
# Utilities
|
32 |
-
python-
|
33 |
-
|
34 |
-
|
35 |
-
aiofiles==23.2.1
|
36 |
-
jinja2==3.1.2
|
|
|
1 |
# Core dependencies
|
2 |
+
gradio>=4.0.0
|
3 |
+
transformers>=4.39.0
|
4 |
+
torch>=2.0.0
|
5 |
+
accelerate>=0.27.2
|
6 |
+
safetensors>=0.4.2
|
7 |
+
sentencepiece>=0.1.99
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Utilities
|
10 |
+
python-dotenv>=1.0.0
|
11 |
+
pydantic>=2.4.2
|
12 |
+
numpy<2.0.0
|
|
|
|