| import gradio as gr |
| from transformers import pipeline |
| import subprocess |
|
|
| |
| code_generator = pipeline("text2text-generation", model="bigcode/t5-code-generation") |
|
|
| def generate_code(description, language): |
| prompt = f"Generate {language} code: {description}" |
| response = code_generator(prompt, max_length=400)[0]['generated_text'] |
| return response.strip() |
|
|
| def execute_code(code, language): |
| if language == "Python": |
| try: |
| result = subprocess.run(['python3', '-c', code], capture_output=True, text=True, timeout=5) |
| return result.stdout if result.stdout else result.stderr |
| except Exception as e: |
| return str(e) |
| return "Code execution only supported for Python." |
|
|
| |
| iface = gr.Interface( |
| fn=generate_code, |
| inputs=[ |
| gr.Textbox(lines=5, placeholder="Describe your coding task..."), |
| gr.Dropdown(choices=["Python", "JavaScript", "Java"], label="Programming Language") |
| ], |
| outputs=[gr.Code(), gr.Textbox(label="Execution Output")], |
| title="Multi-Language Text-to-Code AI", |
| description="Convert natural language descriptions into code in different programming languages! Run Python code directly in the app.", |
| theme="default", |
| allow_flagging="never" |
| ) |
|
|
| |
| if __name__ == "__main__": |
| iface.launch(share=True) |
|
|