Spaces:
Configuration error
Configuration error
Upload 2 files
Browse files- app/main.py +21 -0
- app/prompt_utils.py +6 -0
app/main.py
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from model.model_utils import load_model, generate_explanation
|
| 3 |
+
from app.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 |
+
explanation = generate_explanation(prompt, tokenizer, model, device)
|
| 18 |
+
st.subheader("📝 Explanation")
|
| 19 |
+
st.write(explanation.split("Explanation:")[-1].strip())
|
| 20 |
+
else:
|
| 21 |
+
st.warning("Please paste some code to explain.")
|
app/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:"""
|