File size: 1,025 Bytes
78b65d9
685aa17
6d04149
7593627
 
6d04149
175c478
7593627
6f9ccc9
7593627
 
6f9ccc9
7593627
6f9ccc9
 
41f2d4f
7593627
6f9ccc9
6d04149
7593627
 
 
 
6d04149
6f9ccc9
78b65d9
 
6f9ccc9
7593627
 
78b65d9
6d04149
7593627
6d04149
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
32
33
34
35
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()