Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# β
Load the fastest model for code review
|
6 |
+
model_name = "EleutherAI/pythia-70m" # Smallest & fastest alternative
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16).to("cuda")
|
9 |
+
|
10 |
+
# β
Function to analyze Python code
|
11 |
+
def review_code(code_snippet):
|
12 |
+
inputs = tokenizer(code_snippet, return_tensors="pt").to("cuda") # Move input to GPU
|
13 |
+
outputs = model.generate(**inputs, max_length=80, do_sample=False, num_beams=3) # Fast inference
|
14 |
+
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
15 |
+
return reviewed_code
|
16 |
+
|
17 |
+
# β
Function to handle user input & return reviewed code
|
18 |
+
def check_code(input_code):
|
19 |
+
reviewed_code = review_code(input_code)
|
20 |
+
return input_code, reviewed_code, reviewed_code # Return for UI display & download
|
21 |
+
|
22 |
+
# β
Gradio UI with Side-by-Side Comparison & Download Option
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=check_code,
|
25 |
+
inputs=gr.Textbox(label="Enter Python Code"),
|
26 |
+
outputs=[
|
27 |
+
gr.Textbox(label="Original Code", interactive=False), # Left side
|
28 |
+
gr.Textbox(label="Reviewed Code", interactive=False), # Right side
|
29 |
+
gr.File(label="Download Reviewed Code") # Download button
|
30 |
+
],
|
31 |
+
title="π AI Code Reviewer",
|
32 |
+
description="Enter Python code and get a reviewed version. Download the reviewed code as a file.",
|
33 |
+
allow_flagging="never"
|
34 |
+
)
|
35 |
+
|
36 |
+
# β
Launch the app on Hugging Face Spaces with timeout settings
|
37 |
+
interface.launch(share=True, server_timeout=40)
|