Delete app.py
Browse files
app.py
DELETED
@@ -1,83 +0,0 @@
|
|
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 = "Salesforce/codegen-350M-mono" # 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 |
-
import tempfile
|
12 |
-
|
13 |
-
import tempfile
|
14 |
-
|
15 |
-
import tempfile
|
16 |
-
|
17 |
-
def review_code(code_snippet):
|
18 |
-
print("β
Received Code:", code_snippet) # Debugging log
|
19 |
-
|
20 |
-
# β
Improved prompt with strict format rules
|
21 |
-
prompt = f"""
|
22 |
-
### Instruction:
|
23 |
-
You are a Python code reviewer. Your job is to analyze and fix errors in the provided Python code.
|
24 |
-
Make necessary corrections such as adding missing return statements, fixing syntax errors, and correcting logical mistakes.
|
25 |
-
ONLY return the corrected function definitionβDO NOT generate any new function calls or explanations.
|
26 |
-
|
27 |
-
### Input Code:
|
28 |
-
{code_snippet}
|
29 |
-
|
30 |
-
### Corrected Function:
|
31 |
-
"""
|
32 |
-
|
33 |
-
# Process input
|
34 |
-
inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move to CPU
|
35 |
-
outputs = model.generate(
|
36 |
-
**inputs,
|
37 |
-
max_new_tokens=50, # β
Limits AI output to prevent hallucinations
|
38 |
-
do_sample=False,
|
39 |
-
num_beams=4, # β
Higher beams for better correction quality
|
40 |
-
repetition_penalty=3.0 # β
Stronger penalty to prevent repeated tokens
|
41 |
-
)
|
42 |
-
|
43 |
-
# Check if the model generated output
|
44 |
-
if outputs is None:
|
45 |
-
print("β Model did not generate output!") # Debugging log
|
46 |
-
return "Error: Model did not generate output."
|
47 |
-
|
48 |
-
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
49 |
-
print("β
Generated Code:", reviewed_code) # Debugging log
|
50 |
-
|
51 |
-
# β
Ensure AI output starts with "def" to guarantee it returned a function
|
52 |
-
if not reviewed_code.startswith("def"):
|
53 |
-
return "Error: AI did not return a valid function."
|
54 |
-
|
55 |
-
# β
Write reviewed code to a temporary file for download
|
56 |
-
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
57 |
-
with open(temp_file_path, "w") as temp_file:
|
58 |
-
temp_file.write(reviewed_code)
|
59 |
-
|
60 |
-
return reviewed_code, temp_file_path # β
Return reviewed code & file path
|
61 |
-
|
62 |
-
|
63 |
-
# β
Handle user input and return reviewed code
|
64 |
-
def check_code(input_code):
|
65 |
-
reviewed_code, file_path = review_code(input_code)
|
66 |
-
return input_code, reviewed_code, file_path # β
Correctly return file path
|
67 |
-
|
68 |
-
# β
Gradio UI with Side-by-Side Comparison & Fixed Download Option
|
69 |
-
interface = gr.Interface(
|
70 |
-
fn=check_code,
|
71 |
-
inputs=gr.Textbox(label="Enter Python Code"),
|
72 |
-
outputs=[
|
73 |
-
gr.Textbox(label="Original Code", interactive=False), # Left side
|
74 |
-
gr.Textbox(label="Reviewed Code", interactive=False), # Right side
|
75 |
-
gr.File(label="Download Reviewed Code") # β
Fixed Download Button
|
76 |
-
],
|
77 |
-
title="π AI Code Reviewer",
|
78 |
-
description="π Enter Python code and get a reviewed version. Download the reviewed code as a file.",
|
79 |
-
allow_flagging="never"
|
80 |
-
)
|
81 |
-
|
82 |
-
# β
Launch app (Fixes font issues and removes `share=True`)
|
83 |
-
interface.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|