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

Delete app.py

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