ayushraj2349-2 commited on
Commit
159c5e2
Β·
verified Β·
1 Parent(s): b595fa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
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
- return reviewed_code
 
 
 
 
 
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, reviewed_code # Return all for UI & download
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 button
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.",