Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import tempfile # β
Import tempfile to create temp files
|
5 |
+
|
6 |
+
# β
Load the fastest model on CPU
|
7 |
+
model_name = "EleutherAI/pythia-70m" # Fastest model for code review
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to("cpu") # Force CPU mode
|
10 |
+
|
11 |
+
# β
Function to review Python code with debug logs
|
12 |
+
def review_code(code_snippet):
|
13 |
+
print("β
Received Code:", code_snippet) # Debugging log
|
14 |
+
|
15 |
+
# Process input
|
16 |
+
inputs = tokenizer(code_snippet, return_tensors="pt").to("cpu") # Move to CPU
|
17 |
+
outputs = model.generate(
|
18 |
+
**inputs,
|
19 |
+
max_length=50, # β
Reduced token generation to prevent hallucinations
|
20 |
+
do_sample=False,
|
21 |
+
num_beams=3,
|
22 |
+
repetition_penalty=2.0 # β
Penalizes repetitive text generation
|
23 |
+
)
|
24 |
+
|
25 |
+
# Check if the model generated output
|
26 |
+
if outputs is None:
|
27 |
+
print("β Model did not generate output!") # Debugging log
|
28 |
+
return "Error: Model did not generate output."
|
29 |
+
|
30 |
+
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
print("β
Generated Code:", reviewed_code) # Debugging log
|
32 |
+
|
33 |
+
# β
Write reviewed code to a temporary file for download
|
34 |
+
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
35 |
+
with open(temp_file_path, "w") as temp_file:
|
36 |
+
temp_file.write(reviewed_code)
|
37 |
+
|
38 |
+
return reviewed_code, temp_file_path # β
Return reviewed code & file path
|
39 |
+
|
40 |
+
# β
Handle user input and return reviewed code
|
41 |
+
def check_code(input_code):
|
42 |
+
reviewed_code, file_path = review_code(input_code)
|
43 |
+
return input_code, reviewed_code, file_path # β
Correctly return file path
|
44 |
+
|
45 |
+
# β
Gradio UI with Side-by-Side Comparison & Fixed Download Option
|
46 |
+
interface = gr.Interface(
|
47 |
+
fn=check_code,
|
48 |
+
inputs=gr.Textbox(label="Enter Python Code"),
|
49 |
+
outputs=[
|
50 |
+
gr.Textbox(label="Original Code", interactive=False), # Left side
|
51 |
+
gr.Textbox(label="Reviewed Code", interactive=False), # Right side
|
52 |
+
gr.File(label="Download Reviewed Code") # β
Fixed Download Button
|
53 |
+
],
|
54 |
+
title="π AI Code Reviewer",
|
55 |
+
description="π Enter Python code and get a reviewed version. Download the reviewed code as a file.",
|
56 |
+
allow_flagging="never"
|
57 |
+
)
|
58 |
+
|
59 |
+
# β
Launch app (Fixes font issues and removes `share=True`)
|
60 |
+
interface.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|