Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
import gradio as gr
|
3 |
import torch
|
|
|
4 |
|
5 |
# β
Load the fastest model on CPU
|
6 |
model_name = "EleutherAI/pythia-70m" # Fastest model for code review
|
@@ -23,21 +24,26 @@ def review_code(code_snippet):
|
|
23 |
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
print("β
Generated Code:", reviewed_code) # Debugging log
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# β
Handle user input and return reviewed code
|
29 |
def check_code(input_code):
|
30 |
-
reviewed_code = review_code(input_code)
|
31 |
-
return input_code, reviewed_code,
|
32 |
|
33 |
-
# β
Gradio UI with Side-by-Side Comparison & Download Option
|
34 |
interface = gr.Interface(
|
35 |
fn=check_code,
|
36 |
inputs=gr.Textbox(label="Enter Python Code"),
|
37 |
outputs=[
|
38 |
gr.Textbox(label="Original Code", interactive=False), # Left side
|
39 |
gr.Textbox(label="Reviewed Code", interactive=False), # Right side
|
40 |
-
gr.File(label="Download Reviewed Code") # Download
|
41 |
],
|
42 |
title="π AI Code Reviewer",
|
43 |
description="π Enter Python code and get a reviewed version. Download the reviewed code as a file.",
|
|
|
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
|
|
|
24 |
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
print("β
Generated Code:", reviewed_code) # Debugging log
|
26 |
|
27 |
+
# β
Write reviewed code to a temporary file for download
|
28 |
+
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
29 |
+
with open(temp_file_path, "w") as temp_file:
|
30 |
+
temp_file.write(reviewed_code)
|
31 |
+
|
32 |
+
return reviewed_code, temp_file_path # β
Return reviewed code & file path
|
33 |
|
34 |
# β
Handle user input and return reviewed code
|
35 |
def check_code(input_code):
|
36 |
+
reviewed_code, file_path = review_code(input_code)
|
37 |
+
return input_code, reviewed_code, file_path # β
Correctly return file path
|
38 |
|
39 |
+
# β
Gradio UI with Side-by-Side Comparison & Fixed Download Option
|
40 |
interface = gr.Interface(
|
41 |
fn=check_code,
|
42 |
inputs=gr.Textbox(label="Enter Python Code"),
|
43 |
outputs=[
|
44 |
gr.Textbox(label="Original Code", interactive=False), # Left side
|
45 |
gr.Textbox(label="Reviewed Code", interactive=False), # Right side
|
46 |
+
gr.File(label="Download Reviewed Code") # β
Fixed Download Button
|
47 |
],
|
48 |
title="π AI Code Reviewer",
|
49 |
description="π Enter Python code and get a reviewed version. Download the reviewed code as a file.",
|