Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,27 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
# Load the code explainer pipeline
|
| 5 |
@st.cache_resource
|
| 6 |
def load_model():
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.
|
| 13 |
|
| 14 |
-
st.
|
| 15 |
-
|
| 16 |
-
code_input = st.text_area("📝 Paste your code here:", height=200)
|
| 17 |
|
| 18 |
if st.button("Explain Code"):
|
| 19 |
if code_input.strip() == "":
|
| 20 |
-
st.warning("Please paste some code
|
| 21 |
else:
|
| 22 |
-
with st.spinner("
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
|
|
|
|
| 4 |
@st.cache_resource
|
| 5 |
def load_model():
|
| 6 |
+
tokenizer = T5Tokenizer.from_pretrained("Salesforce/codet5-base")
|
| 7 |
+
model = T5ForConditionalGeneration.from_pretrained("Salesforce/codet5-base")
|
| 8 |
+
return tokenizer, model
|
| 9 |
|
| 10 |
+
tokenizer, model = load_model()
|
| 11 |
|
| 12 |
+
st.title("🧠 Code Explainer (CodeT5)")
|
| 13 |
+
st.markdown("Paste code and get an explanation using the CodeT5 model from Hugging Face.")
|
| 14 |
|
| 15 |
+
code_input = st.text_area("Paste your code here:", height=200)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
if st.button("Explain Code"):
|
| 18 |
if code_input.strip() == "":
|
| 19 |
+
st.warning("Please paste some code first.")
|
| 20 |
else:
|
| 21 |
+
with st.spinner("Generating explanation..."):
|
| 22 |
+
input_text = f"summarize: {code_input.strip()}"
|
| 23 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=512)
|
| 24 |
+
outputs = model.generate(input_ids, max_length=150, num_beams=4, early_stopping=True)
|
| 25 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
st.success("Explanation:")
|
| 27 |
+
st.write(summary)
|