A Flask-based coding assistant that switches between online and offline AI responses depending on internet availability. When the app detects connectivity, it uses Gemini for responses and Together AI for chat title generation. When offline, it falls back to locally served Ollama models such as deepseek-coder:6.7b and tinyllama:1.1b.
- Automatic online/offline response routing
- Local offline inference through Ollama
- Web chat interface with syntax-highlighted code blocks
- Chat history storage in MongoDB when internet is available
- Auto-generated chat titles
- Light/dark theme toggle
- Python
- Flask
- Ollama
- Gemini API
- Together AI API
- MongoDB Atlas
- Vanilla HTML, CSS, and JavaScript
Offline-AI-main/
|- hybrid_ai_app.py # Flask app and API routes
|- db.py # MongoDB connection setup
|- README_HYBRID_MODEL.md # Local model server setup notes
|- templates/
| \- index.html # Main UI template
|- static/
| |- script.js # Frontend chat logic
| |- style_professional.css # App styling
| \- bg.png # UI asset
\- utils/
|- ollama_handler.py # Online/offline model handlers
\- db_helpers.py # Chat document helpers
- The user submits a prompt from the browser UI.
- The Flask app checks internet connectivity.
- If online, the app uses Gemini to generate the response.
- If offline, the app sends the prompt to a local Ollama server.
- If MongoDB is reachable, the conversation is stored in chat history.
- On the second turn, the app attempts to generate a short chat title.
- Python 3.10+ recommended
- Ollama installed locally
- A downloaded Ollama model for offline responses
- Optional:
- Gemini API key for online responses
- Together AI API key for online title generation
- MongoDB Atlas connection for chat history
- Clone the repository.
- Create and activate a virtual environment.
- Install the Python dependencies.
Example:
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txtCreate a .env file in the project root:
GEMINI_API_KEY=your_gemini_api_key
TOGETHER_API_KEY=your_together_api_keyStart Ollama before launching the Flask app:
ollama serveMake sure the required models are available locally, for example:
ollama pull deepseek-coder:6.7b
ollama pull tinyllama:1.1bThe default Ollama endpoint expected by the app is:
http://localhost:11434/api/generate
See README_HYBRID_MODEL.md for the existing local model setup notes.
Start the Flask server with:
python hybrid_ai_app.pyThen open:
http://127.0.0.1:5000
GET /renders the chat interfacePOST /generategenerates a response and optionally stores the turnGET /historyreturns saved chats and supports?search=keyword
- Offline responses are handled in utils/ollama_handler.py.
- The Flask routes live in hybrid_ai_app.py.
- MongoDB connection logic lives in db.py.
- Frontend behavior is implemented in static/script.js.
Sensitive credentials should not be hardcoded in source files. If you plan to share or deploy this project, move database credentials and API keys into environment variables before publishing.
- Improve chat history browsing and conversation reopening
- Stream tokens to the UI progressively
- Add model selection in the interface
- Add tests and configuration management