ayushraj2349-2's picture
Create app.py
0040626 verified
raw
history blame
1.62 kB
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch
# βœ… Load the fastest model for code review
model_name = "EleutherAI/pythia-70m" # Smallest & fastest alternative
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16).to("cuda")
# βœ… Function to analyze Python code
def review_code(code_snippet):
inputs = tokenizer(code_snippet, return_tensors="pt").to("cuda") # Move input to GPU
outputs = model.generate(**inputs, max_length=80, do_sample=False, num_beams=3) # Fast inference
reviewed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
return reviewed_code
# βœ… Function to handle user input & return reviewed code
def check_code(input_code):
reviewed_code = review_code(input_code)
return input_code, reviewed_code, reviewed_code # Return for UI display & download
# βœ… Gradio UI with Side-by-Side Comparison & Download Option
interface = gr.Interface(
fn=check_code,
inputs=gr.Textbox(label="Enter Python Code"),
outputs=[
gr.Textbox(label="Original Code", interactive=False), # Left side
gr.Textbox(label="Reviewed Code", interactive=False), # Right side
gr.File(label="Download Reviewed Code") # Download button
],
title="πŸš€ AI Code Reviewer",
description="Enter Python code and get a reviewed version. Download the reviewed code as a file.",
allow_flagging="never"
)
# βœ… Launch the app on Hugging Face Spaces with timeout settings
interface.launch(share=True, server_timeout=40)