Spaces:
Sleeping
Sleeping
Commit
·
a976d42
1
Parent(s):
78085ee
Deploy production-ready AI Concept Explainer with GPT-4.1-mini
Browse files- .gitignore +48 -0
- README.md +57 -143
- app.py +9 -15
- requirements.txt +5 -11
.gitignore
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
*.egg-info/
|
| 8 |
+
dist/
|
| 9 |
+
build/
|
| 10 |
+
|
| 11 |
+
# Virtual environments
|
| 12 |
+
venv/
|
| 13 |
+
env/
|
| 14 |
+
ENV/
|
| 15 |
+
adk_venv/
|
| 16 |
+
.venv/
|
| 17 |
+
|
| 18 |
+
# Environment files
|
| 19 |
+
.env
|
| 20 |
+
*.env
|
| 21 |
+
api_keys.env
|
| 22 |
+
|
| 23 |
+
# IDE
|
| 24 |
+
.vscode/
|
| 25 |
+
.idea/
|
| 26 |
+
*.swp
|
| 27 |
+
*.swo
|
| 28 |
+
*.code-workspace
|
| 29 |
+
|
| 30 |
+
# Git
|
| 31 |
+
.git/
|
| 32 |
+
|
| 33 |
+
# OS
|
| 34 |
+
.DS_Store
|
| 35 |
+
Thumbs.db
|
| 36 |
+
|
| 37 |
+
# Gradio
|
| 38 |
+
.gradio/
|
| 39 |
+
flagged/
|
| 40 |
+
|
| 41 |
+
# Test files
|
| 42 |
+
test_*.py
|
| 43 |
+
test_docker.sh
|
| 44 |
+
|
| 45 |
+
# Docker (not needed in HF Spaces)
|
| 46 |
+
Dockerfile
|
| 47 |
+
docker-compose.yml
|
| 48 |
+
.dockerignore
|
README.md
CHANGED
|
@@ -8,192 +8,106 @@ sdk_version: "5.47.2"
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
-
short_description: AI-powered concept explainer
|
| 12 |
---
|
| 13 |
|
| 14 |
# 🧠 AI Concept Explainer
|
| 15 |
|
| 16 |
-
An intelligent Gradio app that explains any concept at different complexity levels using OpenAI's GPT
|
| 17 |
|
| 18 |
## 🎯 Features
|
| 19 |
|
| 20 |
- **Adaptive Complexity**: 5 levels from "like I'm 5" to expert-level explanations
|
| 21 |
- **Multi-language Support**: English, Russian, German, Spanish, French, Italian
|
| 22 |
-
- **
|
| 23 |
-
- **
|
| 24 |
-
- **Modern UI**: Clean, responsive interface with gradient styling
|
| 25 |
- **Error Handling**: Graceful API error management
|
| 26 |
|
| 27 |
-
## 🚀
|
| 28 |
|
| 29 |
-
###
|
| 30 |
-
- Python 3.7+
|
| 31 |
-
- OpenAI API key
|
| 32 |
-
- Gradio library
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
```bash
|
| 38 |
-
cd gradio_concept_explainer
|
| 39 |
-
```
|
| 40 |
|
| 41 |
-
2. **Activate the virtual environment**:
|
| 42 |
```bash
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
3.
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
```
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
```bash
|
| 53 |
-
|
| 54 |
-
echo "OPENAI_API_KEY=your_api_key_here" > ../api_keys.env
|
| 55 |
```
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
```bash
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
```
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
```
|
| 66 |
|
| 67 |
## 🎮 How to Use
|
| 68 |
|
| 69 |
-
1. **Enter your question**
|
| 70 |
2. **Select complexity level** (1-5) using the slider
|
| 71 |
3. **Choose your language** from the dropdown
|
| 72 |
-
4. **Click "Explain Concept"** to generate your
|
| 73 |
|
| 74 |
### Complexity Levels
|
| 75 |
-
- **Level 1**: Like I'm 5 years old - simple words and analogies
|
| 76 |
-
- **Level 2**: Like I'm 10 years old - basic concepts with examples
|
| 77 |
-
- **Level 3**: High school level - intermediate with some technical terms
|
| 78 |
-
- **Level 4**: College level - advanced concepts with detailed explanations
|
| 79 |
-
- **Level 5**: Expert level - professional depth with technical precision
|
| 80 |
-
|
| 81 |
-
## 📋 Example Questions
|
| 82 |
|
| 83 |
-
-
|
| 84 |
-
-
|
| 85 |
-
-
|
| 86 |
-
-
|
| 87 |
-
-
|
| 88 |
|
| 89 |
-
## 🔧 Technical
|
| 90 |
|
| 91 |
-
### Architecture
|
| 92 |
- **Framework**: Gradio 5.47.2
|
| 93 |
-
- **AI Model**: OpenAI GPT-
|
| 94 |
-
- **Language**: Python 3.
|
| 95 |
- **Streaming**: Real-time response generation
|
| 96 |
-
- **Port**: localhost:7860
|
| 97 |
-
|
| 98 |
-
### Key Functions
|
| 99 |
-
|
| 100 |
-
#### `explain_concept(question, level, language)`
|
| 101 |
-
Generates streaming explanations using OpenAI API.
|
| 102 |
-
- **Parameters**:
|
| 103 |
-
- `question` (str): Concept to explain
|
| 104 |
-
- `level` (int): Complexity level (1-5)
|
| 105 |
-
- `language` (str): Output language
|
| 106 |
-
- **Returns**: Streaming explanation chunks
|
| 107 |
-
- **Error Handling**: Graceful API error management
|
| 108 |
-
|
| 109 |
-
### Environment Support
|
| 110 |
-
- **Local Development**: Auto-opens browser, localhost only
|
| 111 |
-
- **Hugging Face Spaces**: Production-ready configuration
|
| 112 |
-
- **Signal Handling**: Graceful shutdown for development
|
| 113 |
-
|
| 114 |
-
## 🎨 Interface Design
|
| 115 |
-
|
| 116 |
-
- **Modern Theme**: Soft gradient styling
|
| 117 |
-
- **Responsive Layout**: Works on all screen sizes
|
| 118 |
-
- **Interactive Elements**: Sliders, dropdowns, example buttons
|
| 119 |
-
- **Real-time Streaming**: Live explanation generation
|
| 120 |
-
- **Accessible Design**: Clear labels and error messages
|
| 121 |
|
| 122 |
-
##
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
### Modifying Complexity Levels
|
| 130 |
-
```python
|
| 131 |
-
EXPLANATION_LEVELS = {
|
| 132 |
-
1: "your custom level 1 description",
|
| 133 |
-
2: "your custom level 2 description",
|
| 134 |
-
# ... add more levels
|
| 135 |
-
}
|
| 136 |
-
```
|
| 137 |
-
|
| 138 |
-
### Changing the AI Model
|
| 139 |
-
```python
|
| 140 |
-
stream = client.chat.completions.create(
|
| 141 |
-
model="gpt-3.5-turbo", # Change model here
|
| 142 |
-
# ... rest of parameters
|
| 143 |
-
)
|
| 144 |
-
```
|
| 145 |
-
|
| 146 |
-
## 🔒 Security & Privacy
|
| 147 |
-
|
| 148 |
-
- **API Key Protection**: Environment variable storage
|
| 149 |
-
- **Local Execution**: No data persistence
|
| 150 |
-
- **Secure Requests**: HTTPS API calls to OpenAI
|
| 151 |
-
- **No Data Storage**: Explanations not saved locally
|
| 152 |
-
|
| 153 |
-
## 🚨 Troubleshooting
|
| 154 |
-
|
| 155 |
-
### Common Issues
|
| 156 |
-
|
| 157 |
-
1. **"OPENAI_API_KEY not found"**:
|
| 158 |
-
```bash
|
| 159 |
-
# Set your API key in api_keys.env
|
| 160 |
-
echo "OPENAI_API_KEY=your_key_here" > ../api_keys.env
|
| 161 |
-
```
|
| 162 |
-
|
| 163 |
-
2. **"Module not found" errors**:
|
| 164 |
-
```bash
|
| 165 |
-
pip install gradio openai python-dotenv
|
| 166 |
-
```
|
| 167 |
-
|
| 168 |
-
3. **Port already in use**:
|
| 169 |
-
```python
|
| 170 |
-
# Change port in app.py
|
| 171 |
-
server_port=7861
|
| 172 |
-
```
|
| 173 |
-
|
| 174 |
-
4. **API rate limits**:
|
| 175 |
-
- Check your OpenAI usage limits
|
| 176 |
-
- Consider upgrading your OpenAI plan
|
| 177 |
-
|
| 178 |
-
## 📈 Future Enhancements
|
| 179 |
-
|
| 180 |
-
- **Voice Output**: Text-to-speech for explanations
|
| 181 |
-
- **Visual Diagrams**: Generate concept illustrations
|
| 182 |
-
- **Explanation History**: Save and revisit past explanations
|
| 183 |
-
- **Custom Prompts**: User-defined explanation styles
|
| 184 |
-
- **Export Options**: Save explanations as PDF/Word
|
| 185 |
-
- **Collaborative Features**: Share explanations with others
|
| 186 |
|
| 187 |
## 📝 License
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
## 👥 Contributing
|
| 192 |
-
|
| 193 |
-
This AI Concept Explainer is designed as a learning example. Feel free to:
|
| 194 |
-
- Fork and modify for personal use
|
| 195 |
-
- Use as a starting point for similar projects
|
| 196 |
-
- Suggest improvements or additional features
|
| 197 |
|
| 198 |
---
|
| 199 |
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
+
short_description: AI-powered concept explainer with adaptive complexity
|
| 12 |
---
|
| 13 |
|
| 14 |
# 🧠 AI Concept Explainer
|
| 15 |
|
| 16 |
+
An intelligent Gradio app that explains any concept at different complexity levels using OpenAI's GPT-4o-mini. Get personalized explanations tailored to your understanding level and preferred language.
|
| 17 |
|
| 18 |
## 🎯 Features
|
| 19 |
|
| 20 |
- **Adaptive Complexity**: 5 levels from "like I'm 5" to expert-level explanations
|
| 21 |
- **Multi-language Support**: English, Russian, German, Spanish, French, Italian
|
| 22 |
+
- **Real-time Streaming**: Live explanation generation with streaming responses
|
| 23 |
+
- **Modern UI**: Clean, responsive interface with dark/light mode support
|
|
|
|
| 24 |
- **Error Handling**: Graceful API error management
|
| 25 |
|
| 26 |
+
## 🚀 Setup
|
| 27 |
|
| 28 |
+
### Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
1. Fork or import this repository to your Hugging Face Space
|
| 31 |
+
2. Go to **Settings** → **Repository secrets**
|
| 32 |
+
3. Add secret: `OPENAI_API_KEY` with your OpenAI API key
|
| 33 |
+
4. The app will automatically deploy!
|
| 34 |
|
| 35 |
+
### Local Development
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
```bash
|
| 38 |
+
# Clone and navigate to the project
|
| 39 |
+
cd Explainer_docker
|
| 40 |
|
| 41 |
+
# Create virtual environment (Python 3.11+)
|
| 42 |
+
python3 -m venv venv
|
| 43 |
+
source venv/bin/activate
|
| 44 |
+
|
| 45 |
+
# Install dependencies
|
| 46 |
+
pip install -r requirements.txt
|
| 47 |
+
|
| 48 |
+
# Set your API key
|
| 49 |
+
echo "OPENAI_API_KEY=your_api_key_here" > .env
|
| 50 |
+
|
| 51 |
+
# Run the app
|
| 52 |
+
python app.py
|
| 53 |
```
|
| 54 |
|
| 55 |
+
Visit `http://127.0.0.1:7860` in your browser.
|
| 56 |
+
|
| 57 |
+
**To stop**: Press `Ctrl+C` in the terminal, then deactivate the virtual environment:
|
| 58 |
```bash
|
| 59 |
+
deactivate
|
|
|
|
| 60 |
```
|
| 61 |
|
| 62 |
+
### Docker Deployment
|
| 63 |
+
|
| 64 |
```bash
|
| 65 |
+
# Create .env file with your API key
|
| 66 |
+
echo "OPENAI_API_KEY=your_key_here" > .env
|
| 67 |
+
|
| 68 |
+
# Build and run
|
| 69 |
+
docker-compose up --build
|
| 70 |
|
| 71 |
+
# Access at http://localhost:7860
|
| 72 |
```
|
| 73 |
+
|
| 74 |
+
**To stop**: Press `Ctrl+C`, then:
|
| 75 |
+
```bash
|
| 76 |
+
docker-compose down
|
| 77 |
```
|
| 78 |
|
| 79 |
## 🎮 How to Use
|
| 80 |
|
| 81 |
+
1. **Enter your question** or click a quick example
|
| 82 |
2. **Select complexity level** (1-5) using the slider
|
| 83 |
3. **Choose your language** from the dropdown
|
| 84 |
+
4. **Click "Explain Concept"** to generate your explanation
|
| 85 |
|
| 86 |
### Complexity Levels
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
- **Level 1**: Like I'm 5 - simple words and analogies
|
| 89 |
+
- **Level 2**: Like I'm 10 - basic concepts with examples
|
| 90 |
+
- **Level 3**: High school - intermediate with technical terms
|
| 91 |
+
- **Level 4**: College - advanced concepts with details
|
| 92 |
+
- **Level 5**: Expert - professional depth and precision
|
| 93 |
|
| 94 |
+
## 🔧 Technical Stack
|
| 95 |
|
|
|
|
| 96 |
- **Framework**: Gradio 5.47.2
|
| 97 |
+
- **AI Model**: OpenAI GPT-4o-mini
|
| 98 |
+
- **Language**: Python 3.11+
|
| 99 |
- **Streaming**: Real-time response generation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
## 🔒 Security
|
| 102 |
|
| 103 |
+
- API keys stored as environment variables only
|
| 104 |
+
- No data persistence or storage
|
| 105 |
+
- Secure HTTPS API calls to OpenAI
|
| 106 |
+
- `.env` files are gitignored and never committed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
## 📝 License
|
| 109 |
|
| 110 |
+
MIT License - Part of the LLM and Agentic AI Bootcamp Materials.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
---
|
| 113 |
|
app.py
CHANGED
|
@@ -13,11 +13,14 @@ from openai import OpenAI
|
|
| 13 |
from dotenv import load_dotenv
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
-
try
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
# Initialize OpenAI client
|
| 22 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 23 |
if not openai_api_key:
|
|
@@ -97,18 +100,9 @@ def explain_concept(question, level, language):
|
|
| 97 |
if "connection" in error_msg or "timeout" in error_msg or "ssl" in error_msg:
|
| 98 |
yield f"""❌ **Connection Error**: Unable to reach OpenAI API.
|
| 99 |
|
| 100 |
-
**On Hugging Face Spaces:**
|
| 101 |
-
1. Verify `OPENAI_API_KEY` is set in **Settings → Repository secrets**
|
| 102 |
-
2. Restart the Space after adding the secret
|
| 103 |
-
3. Check the Logs tab for detailed error messages
|
| 104 |
-
|
| 105 |
**Error details**: `{error_type}: {str(e)[:200]}`"""
|
| 106 |
elif "api key" in error_msg or "authentication" in error_msg or "401" in error_msg:
|
| 107 |
-
yield """❌ **Authentication Error**: Invalid or missing API key.
|
| 108 |
-
|
| 109 |
-
**Fix**: Go to **Settings → Repository secrets** and add:
|
| 110 |
-
- Name: `OPENAI_API_KEY`
|
| 111 |
-
- Value: Your OpenAI API key (starts with `sk-`)"""
|
| 112 |
elif "rate limit" in error_msg or "429" in error_msg:
|
| 113 |
yield "❌ **Rate Limit**: Too many requests. Please wait a moment and try again."
|
| 114 |
elif "model" in error_msg or "404" in error_msg:
|
|
|
|
| 13 |
from dotenv import load_dotenv
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
+
# For local development, try loading from .env files
|
| 17 |
+
# For Hugging Face Spaces and Docker, env vars are set directly
|
| 18 |
+
if not os.getenv("SPACE_ID"): # Not in HF Spaces
|
| 19 |
+
try:
|
| 20 |
+
load_dotenv('.env') # Try local .env first
|
| 21 |
+
except:
|
| 22 |
+
pass
|
| 23 |
+
|
| 24 |
# Initialize OpenAI client
|
| 25 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 26 |
if not openai_api_key:
|
|
|
|
| 100 |
if "connection" in error_msg or "timeout" in error_msg or "ssl" in error_msg:
|
| 101 |
yield f"""❌ **Connection Error**: Unable to reach OpenAI API.
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
**Error details**: `{error_type}: {str(e)[:200]}`"""
|
| 104 |
elif "api key" in error_msg or "authentication" in error_msg or "401" in error_msg:
|
| 105 |
+
yield """❌ **Authentication Error**: Invalid or missing API key."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
elif "rate limit" in error_msg or "429" in error_msg:
|
| 107 |
yield "❌ **Rate Limit**: Too many requests. Please wait a moment and try again."
|
| 108 |
elif "model" in error_msg or "404" in error_msg:
|
requirements.txt
CHANGED
|
@@ -1,11 +1,5 @@
|
|
| 1 |
-
# Python 3.
|
| 2 |
-
#
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# httpx==0.27.0
|
| 7 |
-
|
| 8 |
-
gradio
|
| 9 |
-
openai
|
| 10 |
-
python-dotenv
|
| 11 |
-
httpx
|
|
|
|
| 1 |
+
# Python 3.11+
|
| 2 |
+
# Pinned versions for Docker reproducibility
|
| 3 |
+
gradio==5.47.2
|
| 4 |
+
openai==1.107.3
|
| 5 |
+
python-dotenv==1.1.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|