SandeepU commited on
Commit
8807690
·
verified ·
1 Parent(s): 9e2e1f9

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +20 -17
  2. prompt_utils.py +6 -0
  3. requirements.txt +3 -3
  4. 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
- # Welcome to Streamlit!
16
 
17
- Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
18
 
19
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
20
- forums](https://discuss.streamlit.io).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- altair
2
- pandas
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.")