anthony01 commited on
Commit
899ee70
·
verified ·
1 Parent(s): c09a7d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -51
app.py CHANGED
@@ -1,64 +1,27 @@
1
- import os
2
- import tkinter as tk
3
- from tkinterdnd2 import TkinterDnD, DND_FILES
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
- import torch
6
 
7
- # Load the pre-trained model and tokenizer from Hugging Face
8
- model_name = "Salesforce/codegen-350M-mono" # CodeGen model for code understanding
9
  model = AutoModelForCausalLM.from_pretrained(model_name)
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
 
12
- # Function to explain Java code using the LLM model
13
  def explain_code(code):
14
  inputs = tokenizer(code, return_tensors="pt")
15
  outputs = model.generate(inputs['input_ids'], max_length=500)
16
  explanation = tokenizer.decode(outputs[0], skip_special_tokens=True)
17
  return explanation
18
 
19
- # Function to process the files from the folder and explain each Java file
20
- def process_folder(folder_path):
21
- explanations = []
22
-
23
- # Walk through the directory and find all Java files
24
- for filename in os.listdir(folder_path):
25
- if filename.endswith(".java"):
26
- file_path = os.path.join(folder_path, filename)
27
-
28
- # Read Java file contents
29
- with open(file_path, 'r', encoding='utf-8') as file:
30
- java_code = file.read()
31
-
32
- # Get explanation from the model
33
- explanation = explain_code(java_code)
34
- explanations.append(f"Explanation for {filename}:\n{explanation}\n")
35
-
36
- return explanations
37
 
38
- # Function to update the output area in the GUI
39
- def on_drop(event):
40
- folder_path = event.data # This gets the path of the dropped folder
41
- if os.path.isdir(folder_path):
42
- explanations = process_folder(folder_path)
43
- output_text.delete(1.0, tk.END) # Clear previous output
44
- output_text.insert(tk.END, "\n\n".join(explanations)) # Display new output
45
 
46
- # Set up the GUI window
47
- root = TkinterDnD.Tk()
48
- root.title("Java Code Explainer")
49
- root.geometry("800x600")
50
-
51
- # Set up the text widget for displaying explanations
52
- output_text = tk.Text(root, wrap=tk.WORD, width=90, height=25)
53
- output_text.pack(padx=10, pady=10)
54
-
55
- # Set up the label for dragging and dropping the folder
56
- drop_label = tk.Label(root, text="Drag and drop a folder containing Java files here", width=80, height=2)
57
- drop_label.pack(padx=10, pady=10)
58
-
59
- # Register the drop area for folders
60
- root.drop_target_register(DND_FILES)
61
- root.dnd_bind('<<Drop>>', on_drop)
62
-
63
- # Start the GUI event loop
64
- root.mainloop()
 
1
+ import gradio as gr
 
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
 
4
+ # Load the model and tokenizer
5
+ model_name = "codegen-350M-mono"
6
  model = AutoModelForCausalLM.from_pretrained(model_name)
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
 
9
  def explain_code(code):
10
  inputs = tokenizer(code, return_tensors="pt")
11
  outputs = model.generate(inputs['input_ids'], max_length=500)
12
  explanation = tokenizer.decode(outputs[0], skip_special_tokens=True)
13
  return explanation
14
 
15
+ # Gradio interface for uploading files
16
+ def process_uploaded_file(file):
17
+ code = file.read().decode("utf-8")
18
+ return explain_code(code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # Create the Gradio interface
21
+ iface = gr.Interface(fn=process_uploaded_file,
22
+ inputs=gr.File(label="Upload Java File"),
23
+ outputs="text",
24
+ live=True)
 
 
25
 
26
+ # Launch the interface
27
+ iface.launch()