Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# Load a code-generation model
|
| 5 |
code_generator = pipeline("text2text-generation", model="bigcode/t5-code-generation")
|
| 6 |
|
| 7 |
-
def generate_code(description):
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
iface = gr.Interface(
|
| 13 |
fn=generate_code,
|
| 14 |
-
inputs=
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# Launch the app
|
| 21 |
if __name__ == "__main__":
|
| 22 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import subprocess
|
| 4 |
|
| 5 |
+
# Load a code-generation model with support for multiple languages
|
| 6 |
code_generator = pipeline("text2text-generation", model="bigcode/t5-code-generation")
|
| 7 |
|
| 8 |
+
def generate_code(description, language):
|
| 9 |
+
prompt = f"Generate {language} code: {description}"
|
| 10 |
+
response = code_generator(prompt, max_length=400)[0]['generated_text']
|
| 11 |
+
return response.strip()
|
| 12 |
|
| 13 |
+
def execute_code(code, language):
|
| 14 |
+
if language == "Python":
|
| 15 |
+
try:
|
| 16 |
+
result = subprocess.run(['python3', '-c', code], capture_output=True, text=True, timeout=5)
|
| 17 |
+
return result.stdout if result.stdout else result.stderr
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return str(e)
|
| 20 |
+
return "Code execution only supported for Python."
|
| 21 |
+
|
| 22 |
+
# Create a Gradio interface with language selection and execution
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=generate_code,
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.Textbox(lines=5, placeholder="Describe your coding task..."),
|
| 27 |
+
gr.Dropdown(choices=["Python", "JavaScript", "Java"], label="Programming Language")
|
| 28 |
+
],
|
| 29 |
+
outputs=[gr.Code(), gr.Textbox(label="Execution Output")],
|
| 30 |
+
title="Multi-Language Text-to-Code AI",
|
| 31 |
+
description="Convert natural language descriptions into code in different programming languages! Run Python code directly in the app.",
|
| 32 |
+
theme="default",
|
| 33 |
+
allow_flagging="never"
|
| 34 |
)
|
| 35 |
|
| 36 |
# Launch the app
|
| 37 |
if __name__ == "__main__":
|
| 38 |
+
iface.launch(share=True)
|