CoolFace
Apppublic

Varsana/AI_Code_Explainer

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py45 linesDownload Raw Back to root
1from transformers import pipeline2import gradio as gr3 4# Using a small publicly available model that doesn't require authentication5model_name = "t5-small"6 7# Create a text-generation pipeline8code_explainer = pipeline(9    "text2text-generation",10    model=model_name,11    tokenizer=model_name12)13 14def explain_code(code):15    if not code.strip():16        return "❌ Please enter some code to explain."17    18    try:19        # Simple prompt that works with T520        prompt = f"Explain this code: {code}"21        22        # Generate explanation23        explanation = code_explainer(24            prompt,25            max_length=100,26            num_beams=1,27            early_stopping=True28        )[0]['generated_text']29        30        return explanation.strip()31    32    except Exception as e:33        return f"⚠️ Error: {str(e)}"34 35# Create Gradio interface36demo = gr.Interface(37    fn=explain_code,38    inputs=gr.Textbox(lines=10, placeholder="Paste your code here...", label="Input Code"),39    outputs=gr.Textbox(lines=5, label="Explanation"),40    title="💡 Basic Code Explainer",41    description="This lightweight AI explains code snippets using a small public model",42    allow_flagging="never"43)44 45demo.launch(server_name="0.0.0.0", share=False)