SandeepU commited on
Commit
8559052
·
verified ·
1 Parent(s): d030737

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +19 -0
  2. requirements.txt +2 -0
  3. streamlit_app.py +33 -0
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: GPT Code Explainer 💡
3
+ emoji: 🤖
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: streamlit
7
+ sdk_version: "1.35.0"
8
+ app_file: streamlit_app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # 🤖 GPT Code Explainer (OpenAI GPT-3.5)
13
+
14
+ This app uses the OpenAI GPT-3.5 model to explain Python code in natural language.
15
+
16
+ ## 💡 How to Use
17
+ 1. Paste Python code into the text area.
18
+ 2. Add your OpenAI API key as a secret called `OPENAI_API_KEY` in your Hugging Face Space (or as a local env variable).
19
+ 3. Click "Explain" to get a clear explanation of the code.
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ streamlit
streamlit_app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ import os
4
+
5
+ st.set_page_config(page_title="GPT Code Explainer", layout="centered")
6
+
7
+ st.title("🧠 GPT-3.5 Code Explainer")
8
+ st.write("Paste your Python code below and get a detailed explanation using OpenAI GPT-3.5.")
9
+
10
+ code_input = st.text_area("Paste Python Code", height=200)
11
+
12
+ openai_api_key = os.getenv("OPENAI_API_KEY")
13
+
14
+ if st.button("Explain"):
15
+ if not openai_api_key:
16
+ st.error("OPENAI_API_KEY not set. Add it as a secret in your Hugging Face Space or local environment.")
17
+ elif code_input.strip():
18
+ with st.spinner("Asking GPT-3.5..."):
19
+ prompt = f"Explain the following Python function step-by-step in plain English:\n\n{code_input.strip()}"
20
+ try:
21
+ response = openai.ChatCompletion.create(
22
+ model="gpt-3.5-turbo",
23
+ messages=[{"role": "user", "content": prompt}],
24
+ max_tokens=512,
25
+ temperature=0.5
26
+ )
27
+ explanation = response.choices[0].message["content"].strip()
28
+ st.subheader("✅ Explanation")
29
+ st.write(explanation)
30
+ except Exception as e:
31
+ st.error(f"OpenAI API error: {e}")
32
+ else:
33
+ st.warning("Please paste some code to explain.")