Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +19 -0
- prompt_utils.py +6 -0
- requirements.txt +3 -3
- streamlit_app.py +27 -0
README.md
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Code Explainer π‘
|
3 |
+
emoji: π§
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: purple
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: "1.35.0"
|
8 |
+
app_file: streamlit_app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
# π§ Code Explainer (Lightweight Version)
|
13 |
+
|
14 |
+
This app uses `Salesforce/codet5-base` to explain Python code snippets in plain English. Works fast on CPU-only Hugging Face Spaces.
|
15 |
+
|
16 |
+
## π How to Use
|
17 |
+
1. Paste Python code in the box
|
18 |
+
2. Click "Explain"
|
19 |
+
3. Read the AI-generated step-by-step explanation
|
prompt_utils.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def build_prompt(code_snippet):
|
2 |
+
return f"""### Explain this Python code step-by-step:
|
3 |
+
```python
|
4 |
+
{code_snippet}
|
5 |
+
```
|
6 |
+
Explanation:"""
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
streamlit
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
streamlit
|
streamlit_app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from model.model_utils import load_model, generate_explanation
|
3 |
+
from prompt_utils import build_prompt
|
4 |
+
|
5 |
+
st.set_page_config(page_title="Code Explainer", layout="centered")
|
6 |
+
|
7 |
+
st.title("π§ Code Explainer")
|
8 |
+
st.write("Paste your Python code below and get a plain English explanation:")
|
9 |
+
|
10 |
+
code_input = st.text_area("Paste Python Code", height=200)
|
11 |
+
|
12 |
+
if st.button("Explain"):
|
13 |
+
if code_input.strip():
|
14 |
+
with st.spinner("Generating explanation..."):
|
15 |
+
tokenizer, model, device = load_model()
|
16 |
+
prompt = build_prompt(code_input)
|
17 |
+
st.subheader("π Prompt Sent to Model")
|
18 |
+
st.code(prompt)
|
19 |
+
|
20 |
+
explanation = generate_explanation(prompt, tokenizer, model, device)
|
21 |
+
st.subheader("π Raw Output from Model")
|
22 |
+
st.code(explanation)
|
23 |
+
|
24 |
+
st.subheader("β
Final Explanation")
|
25 |
+
st.write(explanation.split("Explanation:")[-1].strip())
|
26 |
+
else:
|
27 |
+
st.warning("Please paste some code to explain.")
|