Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer from Hugging Face
|
| 5 |
+
model_name = "Salesforce/codet5-small"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Streamlit UI
|
| 10 |
+
st.title("Code Generator")
|
| 11 |
+
st.write("Generate code snippets from natural language prompts using CodeT5!")
|
| 12 |
+
|
| 13 |
+
# Input for natural language prompt
|
| 14 |
+
prompt = st.text_area("Enter your coding task:", placeholder="Write a Python function to calculate the factorial of a number.")
|
| 15 |
+
|
| 16 |
+
# Slider to control output length
|
| 17 |
+
max_length = st.slider("Maximum length of generated code:", 20, 200, 50)
|
| 18 |
+
|
| 19 |
+
# Button to trigger code generation
|
| 20 |
+
if st.button("Generate Code"):
|
| 21 |
+
if prompt.strip():
|
| 22 |
+
# Tokenize and generate code
|
| 23 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True)
|
| 24 |
+
outputs = model.generate(inputs.input_ids, max_length=max_length, num_beams=4, early_stopping=True)
|
| 25 |
+
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
# Display generated code
|
| 28 |
+
st.write("### Generated Code:")
|
| 29 |
+
st.code(generated_code, language="python")
|
| 30 |
+
else:
|
| 31 |
+
st.warning("Please enter a prompt to generate code.")
|