Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
MODEL_NAME = "
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
generator = pipeline(
|
| 8 |
"text-generation",
|
| 9 |
model=MODEL_NAME,
|
| 10 |
tokenizer=tokenizer,
|
| 11 |
-
device="cpu", #
|
| 12 |
)
|
| 13 |
|
| 14 |
def generate_code(natural_language_input):
|
| 15 |
try:
|
| 16 |
-
# Create a
|
| 17 |
-
|
| 18 |
-
{"role": "system", "content": "You are a helpful AI assistant that generates Python code based on natural language descriptions."},
|
| 19 |
-
{"role": "user", "content": f"Write Python code that: {natural_language_input}"}
|
| 20 |
-
]
|
| 21 |
|
| 22 |
-
# Generate
|
| 23 |
-
prompt = tokenizer.apply_chat_template(chat_prompt, tokenize=False, add_generation_prompt=True)
|
| 24 |
-
|
| 25 |
-
# Generate code with appropriate parameters for code generation
|
| 26 |
generated = generator(
|
| 27 |
prompt,
|
| 28 |
-
max_new_tokens=256
|
| 29 |
-
temperature=0.
|
| 30 |
-
top_p=0.
|
| 31 |
-
do_sample=True,
|
| 32 |
pad_token_id=tokenizer.eos_token_id,
|
| 33 |
-
|
| 34 |
)
|
| 35 |
|
| 36 |
# Extract and clean the generated code
|
| 37 |
full_response = generated[0]['generated_text']
|
| 38 |
code_response = full_response.replace(prompt, "").strip()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
if "```
|
| 42 |
-
code_response = code_response.split("```
|
| 43 |
-
elif "```" in code_response:
|
| 44 |
-
code_response = code_response.split("```")[1].split("```")[0]
|
| 45 |
|
| 46 |
return code_response
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"Error generating code: {str(e)}"
|
| 49 |
|
| 50 |
-
# Gradio interface
|
| 51 |
-
with gr.Blocks(
|
| 52 |
gr.Markdown("""
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
Describe what you want the code to do in natural language, and get Python code!
|
| 56 |
""")
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
label="Generated Python Code",
|
| 75 |
-
language="python",
|
| 76 |
-
interactive=True,
|
| 77 |
-
lines=10
|
| 78 |
-
)
|
| 79 |
-
with gr.Row():
|
| 80 |
-
copy_btn = gr.Button("Copy to Clipboard")
|
| 81 |
-
refresh_btn = gr.Button("Try Again")
|
| 82 |
|
| 83 |
-
#
|
| 84 |
examples = gr.Examples(
|
| 85 |
examples=[
|
| 86 |
-
["
|
| 87 |
-
["
|
| 88 |
-
["
|
| 89 |
-
["
|
| 90 |
],
|
| 91 |
inputs=input_text,
|
| 92 |
-
label="
|
| 93 |
)
|
| 94 |
|
| 95 |
# Button actions
|
|
@@ -104,18 +86,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 104 |
inputs=None,
|
| 105 |
outputs=[input_text, output_code]
|
| 106 |
)
|
| 107 |
-
|
| 108 |
-
refresh_btn.click(
|
| 109 |
-
fn=generate_code,
|
| 110 |
-
inputs=input_text,
|
| 111 |
-
outputs=output_code
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
copy_btn.click(
|
| 115 |
-
fn=lambda code: gr.Clipboard().copy(code),
|
| 116 |
-
inputs=output_code,
|
| 117 |
-
outputs=None,
|
| 118 |
-
api_name="copy_code"
|
| 119 |
-
)
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer
|
| 3 |
|
| 4 |
+
# Load a smaller model that fits within free tier memory limits
|
| 5 |
+
MODEL_NAME = "Salesforce/codegen-350M-mono"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
generator = pipeline(
|
| 8 |
"text-generation",
|
| 9 |
model=MODEL_NAME,
|
| 10 |
tokenizer=tokenizer,
|
| 11 |
+
device="cpu", # Force CPU usage for free tier
|
| 12 |
)
|
| 13 |
|
| 14 |
def generate_code(natural_language_input):
|
| 15 |
try:
|
| 16 |
+
# Create a clear prompt for code generation
|
| 17 |
+
prompt = f"# Python code that {natural_language_input}\n#"
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Generate with conservative parameters to save memory
|
|
|
|
|
|
|
|
|
|
| 20 |
generated = generator(
|
| 21 |
prompt,
|
| 22 |
+
max_new_tokens=128, # Reduced from 256 to save memory
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
top_p=0.9,
|
|
|
|
| 25 |
pad_token_id=tokenizer.eos_token_id,
|
| 26 |
+
do_sample=True,
|
| 27 |
)
|
| 28 |
|
| 29 |
# Extract and clean the generated code
|
| 30 |
full_response = generated[0]['generated_text']
|
| 31 |
code_response = full_response.replace(prompt, "").strip()
|
| 32 |
|
| 33 |
+
# Remove any non-code text after generation
|
| 34 |
+
if "```" in code_response:
|
| 35 |
+
code_response = code_response.split("```")[0]
|
|
|
|
|
|
|
| 36 |
|
| 37 |
return code_response
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"Error generating code: {str(e)}\n\n(Tip: Try a simpler description or break your request into smaller parts)"
|
| 40 |
|
| 41 |
+
# Streamlined Gradio interface to reduce memory footprint
|
| 42 |
+
with gr.Blocks(title="Text to Python Code Generator") as demo:
|
| 43 |
gr.Markdown("""
|
| 44 |
+
# 🐍 Python Code Generator
|
| 45 |
+
*Free tier version using Salesforce/codegen-350M-mono*
|
|
|
|
| 46 |
""")
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
+
input_text = gr.Textbox(
|
| 50 |
+
label="Describe what you want the code to do",
|
| 51 |
+
placeholder="e.g., 'calculate fibonacci sequence up to N numbers'",
|
| 52 |
+
lines=3
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
| 57 |
+
clear_btn = gr.Button("Clear")
|
| 58 |
+
|
| 59 |
+
output_code = gr.Code(
|
| 60 |
+
label="Generated Python Code",
|
| 61 |
+
language="python",
|
| 62 |
+
interactive=True
|
| 63 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Memory-friendly examples
|
| 66 |
examples = gr.Examples(
|
| 67 |
examples=[
|
| 68 |
+
["print hello world"],
|
| 69 |
+
["function to calculate square of a number"],
|
| 70 |
+
["read a CSV file using pandas"],
|
| 71 |
+
["simple Flask app with one route"]
|
| 72 |
],
|
| 73 |
inputs=input_text,
|
| 74 |
+
label="Try these examples (keep requests simple)"
|
| 75 |
)
|
| 76 |
|
| 77 |
# Button actions
|
|
|
|
| 86 |
inputs=None,
|
| 87 |
outputs=[input_text, output_code]
|
| 88 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Launch with minimal resources
|
| 91 |
+
demo.launch(debug=False, show_error=True)
|