adarshzolekar commited on
Commit
5e0c977
·
verified ·
1 Parent(s): 3c1ca5c

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +33 -7
  2. app.py +115 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,14 +1,40 @@
1
  ---
2
  title: AI Code Reviewer
3
- emoji: 🦀
4
- colorFrom: indigo
5
- colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 6.26.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
- short_description: AI Code Reviewer
 
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: AI Code Reviewer
3
+ colorFrom: gray
4
+ colorTo: red
 
5
  sdk: gradio
6
+ sdk_version: 6.3.0
 
7
  app_file: app.py
8
  pinned: false
9
+ license: apache-2.0
10
+ short_description: Paste code to get AI-powered code review.
11
  ---
12
 
13
+ # AI Code Reviewer
14
+
15
+ Paste a code snippet and get a short review covering bugs, edge cases,
16
+ complexity, and style.
17
+
18
+ Powered by `Qwen/Qwen2.5-Coder-0.5B-Instruct`.
19
+
20
+ The model runs on the GPU configured for the Hugging Face Space using
21
+ `@spaces.GPU`.
22
+
23
+ ## Features
24
+
25
+ - Bug detection
26
+ - Edge-case identification
27
+ - Complexity feedback
28
+ - Code style suggestions
29
+ - Concise AI-generated review
30
+ - GPU-accelerated inference
31
+
32
+ ## Stack
33
+
34
+ Gradio · Transformers · PyTorch · Hugging Face Spaces
35
+
36
+ ## Note
37
+
38
+ The 0.5B model favors speed over depth. It works best for short and
39
+ medium-sized code snippets and may miss subtle issues in large or complex
40
+ projects.
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+
6
+ MODEL_NAME = "Qwen/Qwen2.5-Coder-0.5B-Instruct"
7
+
8
+ # Load tokenizer and model once at startup
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ MODEL_NAME,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
14
+ )
15
+
16
+ # Use GPU when available
17
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
+
19
+ model = model.to(device)
20
+ model.eval()
21
+
22
+
23
+ SYSTEM_PROMPT = (
24
+ "You are a concise code reviewer. Given a code snippet, point out bugs, "
25
+ "edge cases, complexity issues, and style improvements. "
26
+ "Be specific and brief."
27
+ )
28
+
29
+
30
+ @spaces.GPU
31
+ def review_code(code):
32
+ if not code or not code.strip():
33
+ return "Paste some code first."
34
+
35
+ messages = [
36
+ {
37
+ "role": "system",
38
+ "content": SYSTEM_PROMPT
39
+ },
40
+ {
41
+ "role": "user",
42
+ "content": f"Review this code:\n\n{code}"
43
+ }
44
+ ]
45
+
46
+ # Apply the model's chat template
47
+ prompt = tokenizer.apply_chat_template(
48
+ messages,
49
+ tokenize=False,
50
+ add_generation_prompt=True
51
+ )
52
+
53
+ inputs = tokenizer(
54
+ prompt,
55
+ return_tensors="pt",
56
+ truncation=True,
57
+ max_length=4096
58
+ )
59
+
60
+ # Move inputs to the same device as the model
61
+ inputs = {
62
+ key: value.to(device)
63
+ for key, value in inputs.items()
64
+ }
65
+
66
+ with torch.no_grad():
67
+ output_ids = model.generate(
68
+ **inputs,
69
+ max_new_tokens=300,
70
+ do_sample=False,
71
+ temperature=None,
72
+ top_p=None
73
+ )
74
+
75
+ # Only decode newly generated tokens
76
+ generated_ids = output_ids[0][inputs["input_ids"].shape[1]:]
77
+
78
+ review = tokenizer.decode(
79
+ generated_ids,
80
+ skip_special_tokens=True
81
+ )
82
+
83
+ return review.strip()
84
+
85
+
86
+ with gr.Blocks(title="AI Code Reviewer") as demo:
87
+
88
+ gr.Markdown(
89
+ "## AI Code Reviewer\n"
90
+ "Paste a code snippet and get feedback on bugs, edge cases, "
91
+ "complexity, and style."
92
+ )
93
+
94
+ code_input = gr.Code(
95
+ label="Your code",
96
+ language="python",
97
+ lines=15
98
+ )
99
+
100
+ review_btn = gr.Button(
101
+ "Review",
102
+ variant="primary"
103
+ )
104
+
105
+ output = gr.Markdown()
106
+
107
+ review_btn.click(
108
+ review_code,
109
+ inputs=code_input,
110
+ outputs=output
111
+ )
112
+
113
+
114
+ if __name__ == "__main__":
115
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==6.3.0
2
+ transformers>=4.42
3
+ torch
4
+ accelerate
5
+ spaces