Spaces:
Running
Running
File size: 1,133 Bytes
49b9e99 4eb6999 789250e 49b9e99 789250e 49b9e99 789250e 49b9e99 789250e 49b9e99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load CodeT5 model and tokenizer
tokenizer = T5Tokenizer.from_pretrained("Salesforce/codet5-base")
model = T5ForConditionalGeneration.from_pretrained("Salesforce/codet5-base")
# Function to explain code
def explain_code(code_snippet):
if not code_snippet.strip():
return "❗ Please enter some code."
input_text = f"summarize: {code_snippet.strip()}"
input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=512)
outputs = model.generate(input_ids, max_length=150, num_beams=4, early_stopping=True)
explanation = tokenizer.decode(outputs[0], skip_special_tokens=True)
return explanation
# Gradio Interface
demo = gr.Interface(
fn=explain_code,
inputs=gr.Textbox(lines=15, label="Paste your code here"),
outputs=gr.Textbox(label="Explanation"),
title="🧠 Code Explainer using Hugging Face",
description="This tool uses Salesforce's CodeT5 to convert your code into a human-readable explanation. Works on CPU!",
theme="default"
)
demo.launch()
|