Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +20 -17
- prompt_utils.py +6 -0
- requirements.txt +3 -3
- streamlit_app.py +21 -0
README.md
CHANGED
|
@@ -1,20 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
title: Code Explainer P
|
| 3 |
-
emoji: 🚀
|
| 4 |
-
colorFrom: red
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: docker
|
| 7 |
-
app_port: 8501
|
| 8 |
-
tags:
|
| 9 |
-
- streamlit
|
| 10 |
-
pinned: false
|
| 11 |
-
short_description: Streamlit template space
|
| 12 |
-
license: mit
|
| 13 |
-
---
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Code Explainer 🧠
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
This project uses the `bigcode/starcoder` model to explain Python code in plain English.
|
| 4 |
|
| 5 |
+
## How to Run
|
| 6 |
|
| 7 |
+
```bash
|
| 8 |
+
pip install -r requirements.txt
|
| 9 |
+
streamlit run app/main.py
|
| 10 |
+
```
|
| 11 |
+
|
| 12 |
+
## Example
|
| 13 |
+
|
| 14 |
+
Input:
|
| 15 |
+
```python
|
| 16 |
+
def fibonacci(n):
|
| 17 |
+
if n <= 1:
|
| 18 |
+
return n
|
| 19 |
+
return fibonacci(n-1) + fibonacci(n-2)
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
Output:
|
| 23 |
+
> This function calculates the nth Fibonacci number using recursion...
|
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,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.")
|