Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
-
|
| 4 |
-
# Available models for code explanation
|
| 5 |
-
MODELS = {
|
| 6 |
-
"CodeT5-small-sum": "Salesforce/codet5-small",
|
| 7 |
-
"CodeT5-base-sum": "Salesforce/codet5-base-multi-sum",
|
| 8 |
-
"CodeBERTa": "huggingface/CodeBERTa-small-v1"
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
-
def load_model(model_name):
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 14 |
-
# Use summarization for CodeT5, text2text-generation for others
|
| 15 |
-
if "codet5" in model_name.lower():
|
| 16 |
-
return pipeline("summarization", model=model, tokenizer=tokenizer)
|
| 17 |
-
else:
|
| 18 |
-
return pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 19 |
-
|
| 20 |
-
def explain_code(
|
| 21 |
-
model_id = MODELS[model_choice]
|
| 22 |
-
explainer = load_model(model_id)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
# Available models for code explanation
|
| 5 |
+
MODELS = {
|
| 6 |
+
"CodeT5-small-sum": "Salesforce/codet5-small",
|
| 7 |
+
"CodeT5-base-sum": "Salesforce/codet5-base-multi-sum",
|
| 8 |
+
"CodeBERTa": "huggingface/CodeBERTa-small-v1"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def load_model(model_name):
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 14 |
+
# Use summarization for CodeT5, text2text-generation for others
|
| 15 |
+
if "codet5" in model_name.lower():
|
| 16 |
+
return pipeline("summarization", model=model, tokenizer=tokenizer)
|
| 17 |
+
else:
|
| 18 |
+
return pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 19 |
+
|
| 20 |
+
def explain_code(files, model_choice):
|
| 21 |
+
model_id = MODELS[model_choice]
|
| 22 |
+
explainer = load_model(model_id)
|
| 23 |
+
explanations = []
|
| 24 |
+
for file in files:
|
| 25 |
+
filename = file.name if hasattr(file, 'name') else 'Uploaded File'
|
| 26 |
+
code = file.read().decode('utf-8') if hasattr(file, 'read') else file
|
| 27 |
+
lines = code.splitlines()
|
| 28 |
+
line_explanations = []
|
| 29 |
+
for idx, line in enumerate(lines):
|
| 30 |
+
if not line.strip():
|
| 31 |
+
continue # Skip empty lines
|
| 32 |
+
input_length = len(line.split())
|
| 33 |
+
max_length = max(1, input_length // 2)
|
| 34 |
+
gen_kwargs = dict(max_length=max_length, do_sample=False)
|
| 35 |
+
if "codet5" in model_id.lower():
|
| 36 |
+
result = explainer(line, **gen_kwargs)[0].get('summary_text', '')
|
| 37 |
+
else:
|
| 38 |
+
prompt = f"Explain this line of code:\n{line}"
|
| 39 |
+
result = explainer(prompt, **gen_kwargs)[0].get('generated_text', '')
|
| 40 |
+
line_explanations.append(f"Line {idx+1}: {line}\nExplanation: {result.strip()}\n")
|
| 41 |
+
explanations.append(f"## {filename}\n" + "\n".join(line_explanations))
|
| 42 |
+
return "\n\n".join(explanations)
|
| 43 |
+
|
| 44 |
+
def main():
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("# CodeExplainerBot (Hugging Face UI)")
|
| 47 |
+
model_choice = gr.Dropdown(list(MODELS.keys()), label="Select a model", value="CodeT5-small-sum")
|
| 48 |
+
code_input = gr.File(label="Upload code files", file_count="multiple", type="file")
|
| 49 |
+
output = gr.Textbox(label="Line-by-line Explanation", lines=20)
|
| 50 |
+
explain_btn = gr.Button("Explain Code")
|
| 51 |
+
explain_btn.click(fn=explain_code, inputs=[code_input, model_choice], outputs=output)
|
| 52 |
+
demo.launch()
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
main()
|