import gradio as gr from transformers import pipeline # Load a faster text generation model (DistilGPT-2) text_generator = pipeline('text-generation', model='distilgpt2') def get_completion(code_snippet): # Prompt to explain the code snippet provided by the user prompt = f""" Please explain the following Python code snippet in detail, including what it does and the expected output: Code Snippet: ```python {code_snippet} ``` Include any relevant information that helps clarify how the code works. """ # Generate text using the model response = text_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.5) return response[0]['generated_text'].strip() # Define the Gradio interface iface = gr.Interface( fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)], outputs=[gr.Textbox(label="Explanation Here", lines=10)], title="Code Explainer Using Transformers" ) # Launch the Gradio interface iface.launch()