ayushraj2349-2 commited on
Commit
eb97e7b
Β·
verified Β·
1 Parent(s): cfea425

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
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)