使用说明
Ufuq AI Tutoring Platform
An offline-first AI tutoring system using Model Context Protocol (MCP) for refugee and underserved students. Works locally via Wi-Fi hotspot with optional cloud integration.
🌟 Features
Core Capabilities
- Offline-First: Works without internet using local AI models
- Multi-Agent System: Specialized agents for tutoring, translation, quizzes, and content retrieval
- Bilingual Support: Arabic and English with real-time translation
- Progressive Web App: Works on phones, tablets, and computers
- Local Network Access: Students connect via Wi-Fi hotspot or LAN
- Cloud Sync: Syncs progress when internet is available
MCP Agents
- Tutor Agent - Explains concepts, answers questions
- Translator Agent - Arabic ↔ English translation
- Quiz Agent - Generates and grades assessments
- Content Agent - Retrieves from stored educational materials (RAG)
- Sync Agent - Syncs data to cloud when online
🚀 Quick Start
Prerequisites
- Python 3.9+
- 4GB+ RAM (for local models)
- Wi-Fi hotspot capability or local network
Installation
# Clone the repository
git clone <your-repo-url>
cd ufuq
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Download models (first time only)
python download_models.py
Configuration
Create a .env file:
# Optional: Add API keys for online mode
OPENAI_API_KEY=your_key_here
SAMBANOVA_API_KEY=your_key_here
MODAL_API_KEY=your_key_here
# Debug mode
DEBUG=True
Running the Server
# Start the Flask backend
python app.py
The server will run on http://0.0.0.0:5000 (accessible on local network)
Connecting Students
- Enable Wi-Fi Hotspot on the host device
- Students connect to the hotspot
- Students open browser and navigate to:
http://<host-ip>:5000 - Start learning!
📁 Project Structure
ufuq/
├── app.py # Main Flask application
├── mcp_runtime.py # MCP agent orchestration hub
├── agents.py # All 5 MCP agents
├── storage.py # Database and caching
├── config.py # Configuration management
├── requirements.txt # Python dependencies
├── download_models.py # Model download script
├── data/
│ ├── ufuq.db # SQLite database
│ ├── cache/ # Offline cache
│ ├── vectordb/ # ChromaDB vector storage
│ └── content/ # Educational PDFs/materials
└── frontend/ # React PWA (separate)
└── ...
🔧 API Endpoints
Chat (Tutor Agent)
POST /api/chat
{
"student_id": "student_123",
"message": "Explain Newton's laws",
"language": "en"
}
Translation
POST /api/translate
{
"text": "Hello, how are you?",
"source_lang": "en",
"target_lang": "ar"
}
Generate Quiz
POST /api/quiz/generate
{
"student_id": "student_123",
"topic": "Physics - Newton's Laws",
"difficulty": "medium",
"num_questions": 5,
"language": "en"
}
Submit Quiz
POST /api/quiz/submit
{
"student_id": "student_123",
"quiz_id": 1,
"answers": ["A", "B", "C", "D", "A"]
}
Retrieve Content (RAG)
POST /api/content/retrieve
{
"query": "What is photosynthesis?",
"subject": "biology",
"language": "en"
}
Student Progress
GET /api/student/progress?student_id=student_123
Sync Data
POST /api/sync
{
"student_id": "student_123"
}
Check Mode
GET /api/mode
🤖 MCP Architecture
How It Works
- Student Request → Flask receives HTTP request
- MCP Router → Routes to appropriate agent
- Agent Execution → Agent processes with local/cloud models
- Mode Detection → Automatically switches between offline/online
- Response → Returns result to student
- Storage → Saves interaction locally, syncs when online
Agent Selection Logic
# MCP Runtime automatically routes based on request type
mcp_runtime.route_request(
agent_type="tutor", # or "translator", "quiz", "content", "sync"
payload={...}
)
Offline ↔ Online Switching
The system automatically detects internet connectivity:
- Offline: Uses Hugging Face models (FLAN-T5, Helsinki-NLP)
- Online: Uses OpenAI GPT-4 for better quality
- Hybrid: Falls back to offline if online fails
📚 Adding Educational Content
Ingest PDFs for RAG
# Create a content ingestion script
from content_ingestion import ingest_pdf
# Add textbooks
ingest_pdf('path/to/physics_textbook.pdf', subject='physics')
ingest_pdf('path/to/math_textbook.pdf', subject='mathematics')
Manual Content Addition
from agents import ContentAgent
from config import Config
config = Config()
content_agent = ContentAgent(config)
# Add content manually
content_agent.collection.add(
documents=["Newton's First Law: Objects at rest stay at rest..."],
metadatas=[{"subject": "physics", "source": "textbook_ch1"}],
ids=["physics_newton_1"]
)
🌐 Cloud Integration (Optional)
OpenAI Setup
# Set API key in .env
OPENAI_API_KEY=sk-...
Modal Setup (for serverless functions)
pip install modal
modal token new
SambaNova Setup
# Add SambaNova API key
SAMBANOVA_API_KEY=your_key
🧪 Testing
# Run tests
pytest tests/
# Test specific agent
pytest tests/test_tutor_agent.py
# Test offline mode
FORCE_OFFLINE=True pytest tests/
📱 Frontend Integration
The React PWA connects to these endpoints. Example:
// Chat with tutor
const response = await fetch('http://<host-ip>:5000/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
student_id: 'student_123',
message: 'Explain gravity',
language: 'en'
})
});
const data = await response.json();
console.log(data.response); // AI tutor's explanation
🔐 Security Considerations
- Local Network Only: By default, only accessible on LAN
- No Authentication: Add authentication for production
- Data Privacy: All data stored locally, synced optionally
- API Keys: Keep in
.env, never commit to git
🐛 Troubleshooting
Models Not Loading
# Re-download models
python download_models.py --force
Port Already in Use
# Change port in app.py
app.run(host='0.0.0.0', port=5001)
Out of Memory
- Reduce model sizes in
config.py - Use smaller models (e.g.,
flan-t5-smallinstead ofbase) - Disable local model loading and use online-only mode
Connection Issues
- Check firewall settings
- Ensure all devices on same network
- Use IP address instead of localhost
📊 Performance Optimization
For Low-Resource Devices
# In config.py, use smaller models
'offline': {
'tutor': 'google/flan-t5-small', # Instead of base
'translator': 'Helsinki-NLP/opus-mt-tc-big-en-ar' # Smaller variant
}
Batch Processing
# Process multiple quiz questions at once
quiz_agent.execute({
'action': 'batch_generate',
'topics': ['physics', 'math', 'chemistry']
})
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test offline and online modes
- Submit a pull request
📄 License
MIT License - Feel free to use for educational purposes
🆘 Support
For issues during the hackathon:
- Check logs:
tail -f logs/ufuq.log - Test mode:
curl http://localhost:5000/health
🚀 Next Steps
- [ ] Add voice input/output
- [ ] Implement peer-to-peer quiz sharing
- [ ] Add gamification (points, badges)
- [ ] Multi-user collaboration features
- [ ] Teacher dashboard for monitoring
- [ ] More languages (French, Spanish)
- [ ] Offline video lessons
Built with ❤️ for underserved students worldwide