Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
|
| 5 |
-
MODEL_ID = "
|
| 6 |
|
| 7 |
# Load once at startup (ZeroGPU handles GPU allocation automatically)
|
| 8 |
bnb_config = BitsAndBytesConfig(
|
|
@@ -51,29 +51,36 @@ Please review the following {language} code:
|
|
| 51 |
|
| 52 |
|
| 53 |
def review_code(code: str, language: str, max_new_tokens: int = 800):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
outputs[0][inputs["input_ids"].shape[1]:],
|
| 73 |
-
skip_special_tokens=True
|
| 74 |
-
)
|
| 75 |
-
return response.strip() or "⚠️ Model returned an empty response."
|
| 76 |
-
|
| 77 |
|
| 78 |
# --- Gradio UI ---
|
| 79 |
with gr.Blocks(title="AI Code Reviewer", theme=gr.themes.Soft()) as demo:
|
|
@@ -113,4 +120,4 @@ with gr.Blocks(title="AI Code Reviewer", theme=gr.themes.Soft()) as demo:
|
|
| 113 |
label="Try an example"
|
| 114 |
)
|
| 115 |
|
| 116 |
-
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
|
| 5 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 6 |
|
| 7 |
# Load once at startup (ZeroGPU handles GPU allocation automatically)
|
| 8 |
bnb_config = BitsAndBytesConfig(
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def review_code(code: str, language: str, max_new_tokens: int = 800):
|
| 54 |
+
try:
|
| 55 |
+
if not code.strip():
|
| 56 |
+
return "⚠️ Please paste some code to review."
|
| 57 |
+
|
| 58 |
+
prompt = build_prompt(code, language)
|
| 59 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 60 |
+
|
| 61 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 62 |
+
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
outputs = model.generate(
|
| 65 |
+
**inputs,
|
| 66 |
+
max_new_tokens=512,
|
| 67 |
+
temperature=0.2,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
top_p=0.95,
|
| 70 |
+
repetition_penalty=1.1,
|
| 71 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 72 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 73 |
+
use_cache=True
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Decode only the newly generated tokens (skip the prompt)
|
| 77 |
+
response = tokenizer.decode(
|
| 78 |
+
outputs[0][inputs["input_ids"].shape[1]:],
|
| 79 |
+
skip_special_tokens=True
|
| 80 |
)
|
| 81 |
+
return response.strip() or "⚠️ Model returned an empty response."
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"❌ Error:\n\n{str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# --- Gradio UI ---
|
| 86 |
with gr.Blocks(title="AI Code Reviewer", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 120 |
label="Try an example"
|
| 121 |
)
|
| 122 |
|
| 123 |
+
demo.queue().launch()
|