Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
# Force CPU usage & prevent model download issues
|
| 8 |
os.environ["HF_HOME"] = "./cache" # Store model locally
|
| 9 |
-
MODEL_NAME = "
|
| 10 |
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
@@ -27,29 +27,20 @@ def execute_code(code, language):
|
|
| 27 |
return str(e)
|
| 28 |
return "Code execution only supported for Python."
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return code, output
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
fn=generate_and_execute,
|
| 38 |
-
inputs=[
|
| 39 |
-
gr.Textbox(lines=5, placeholder="Describe your coding task..."),
|
| 40 |
-
gr.Dropdown(choices=["Python", "JavaScript", "Java"], label="Programming Language")
|
| 41 |
-
],
|
| 42 |
-
outputs=[
|
| 43 |
-
gr.Code(label="Generated Code"),
|
| 44 |
-
gr.Textbox(label="Execution Output")
|
| 45 |
-
],
|
| 46 |
-
title="Multi-Language Text-to-Code AI",
|
| 47 |
-
description="Convert natural language descriptions into code in different programming languages! Run Python code directly in the app.",
|
| 48 |
-
theme="default",
|
| 49 |
-
allow_flagging="never",
|
| 50 |
-
live=True
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
if
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
# Force CPU usage & prevent model download issues
|
| 8 |
os.environ["HF_HOME"] = "./cache" # Store model locally
|
| 9 |
+
MODEL_NAME = "Salesforce/codegen-350M-mono" # Updated model
|
| 10 |
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
|
|
| 27 |
return str(e)
|
| 28 |
return "Code execution only supported for Python."
|
| 29 |
|
| 30 |
+
# Streamlit UI
|
| 31 |
+
st.title("Multi-Language Text-to-Code AI")
|
| 32 |
+
st.write("Convert natural language descriptions into code in different programming languages! Run Python code directly in the app.")
|
|
|
|
| 33 |
|
| 34 |
+
description = st.text_area("Describe your coding task...")
|
| 35 |
+
language = st.selectbox("Select Programming Language", ["Python", "JavaScript", "Java"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
if st.button("Generate Code"):
|
| 38 |
+
if description:
|
| 39 |
+
code = generate_code(description, language)
|
| 40 |
+
st.code(code, language=language.lower())
|
| 41 |
+
|
| 42 |
+
if language == "Python":
|
| 43 |
+
output = execute_code(code, language)
|
| 44 |
+
st.text_area("Execution Output", output, height=150)
|
| 45 |
+
else:
|
| 46 |
+
st.warning("Please enter a description to generate code.")
|