jk12p commited on
Commit
4eb6999
·
verified ·
1 Parent(s): d7dcbc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -1,26 +1,27 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
- # Load the code explainer pipeline
5
  @st.cache_resource
6
  def load_model():
7
- return pipeline("text2text-generation", model="philschmid/code-explainer", device=-1)
 
 
8
 
9
- explainer = load_model()
10
 
11
- # Streamlit UI
12
- st.title("🧠 Code Explainer (Hugging Face)")
13
 
14
- st.markdown("Paste any code snippet below (Python, JavaScript, etc.) and get a plain-English explanation using a Hugging Face model.")
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 to explain.")
21
  else:
22
- with st.spinner("Explaining your code..."):
23
- result = explainer(f"Explain this code: {code_input}")
24
- explanation = result[0]['generated_text']
25
- st.success("✅ Explanation:")
26
- st.write(explanation)
 
 
 
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)