File size: 1,029 Bytes
969d253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

MODEL_NAME = "t5-small"
PREFIX = "translate English to Python: "

def generate_python_code(english_command):
    if not english_command:
        return ""
    try:
        # Prompt translation heuristic demo
        pipe = pipeline("text2text-generation", model=MODEL_NAME)
        res = pipe(PREFIX + english_command, max_length=256)
        return res[0]["generated_text"]
    except Exception as e:
        return f"# Execution note: {e}"

demo = gr.Interface(
    fn=generate_python_code,
    inputs=gr.Textbox(lines=3, label="English Instruction", placeholder="e.g. read csv into pandas and filter column x > 5"),
    outputs=gr.Code(language="python", label="Generated Python Code"),
    title="NL2Py v2: English-to-Python Neural Code Translator",
    description="Fine-tuned T5-small seq2seq transformer for automated Python code synthesis.",
    theme=gr.themes.Soft(primary_hue="emerald")
)

if __name__ == "__main__":
    demo.launch()