Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def explain_code(files):
|
| 10 |
-
|
| 11 |
explanations = []
|
| 12 |
for file in files:
|
| 13 |
filename = file.name if hasattr(file, 'name') else 'Uploaded File'
|
|
@@ -22,16 +23,16 @@ def explain_code(files):
|
|
| 22 |
for idx, line in enumerate(lines):
|
| 23 |
if not line.strip():
|
| 24 |
continue
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
result =
|
| 28 |
line_explanations.append(f"Line {idx+1}: {line}\nExplanation: {result.strip()}\n")
|
| 29 |
explanations.append(f"## {filename}\n" + "\n".join(line_explanations))
|
| 30 |
return "\n\n".join(explanations)
|
| 31 |
|
| 32 |
def main():
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
-
gr.Markdown("# CodeExplainerBot (
|
| 35 |
code_input = gr.File(label="Upload code files", file_count="multiple", type="binary")
|
| 36 |
output = gr.Textbox(label="Line-by-line Explanation", lines=20)
|
| 37 |
explain_btn = gr.Button("Explain Code")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
| 3 |
|
| 4 |
+
def load_model():
|
| 5 |
+
model_name = "Salesforce/codet5-small"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
return pipeline("summarization", model=model, tokenizer=tokenizer)
|
| 9 |
|
| 10 |
def explain_code(files):
|
| 11 |
+
explainer = load_model()
|
| 12 |
explanations = []
|
| 13 |
for file in files:
|
| 14 |
filename = file.name if hasattr(file, 'name') else 'Uploaded File'
|
|
|
|
| 23 |
for idx, line in enumerate(lines):
|
| 24 |
if not line.strip():
|
| 25 |
continue
|
| 26 |
+
input_length = len(line.split())
|
| 27 |
+
max_length = max(1, input_length // 2)
|
| 28 |
+
result = explainer(line, max_length=max_length, do_sample=False)[0].get('summary_text', '')
|
| 29 |
line_explanations.append(f"Line {idx+1}: {line}\nExplanation: {result.strip()}\n")
|
| 30 |
explanations.append(f"## {filename}\n" + "\n".join(line_explanations))
|
| 31 |
return "\n\n".join(explanations)
|
| 32 |
|
| 33 |
def main():
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# CodeExplainerBot (Local CodeT5-small)")
|
| 36 |
code_input = gr.File(label="Upload code files", file_count="multiple", type="binary")
|
| 37 |
output = gr.Textbox(label="Line-by-line Explanation", lines=20)
|
| 38 |
explain_btn = gr.Button("Explain Code")
|