This is a lightweight FastAPI + Jinja app that translates user text into a selected target language using Google's Gemini via LangChain.
It serves a simple single-page UI at /
and a JSON API at POST /translate
.
Secrets are managed with a .env
file containing GOOGLE_API_KEY
.
- Key Features
- Architecture
- Requirements
- Setup
- API Documentation
- Frontend
- Security
- Troubleshooting
- Useful Commands
- Project Structure
- Clean, single-page UI built with Jinja templates + vanilla JS
- FastAPI backend using LangChain
ChatGoogleGenerativeAI
- One-click translate: enter text, pick a target language, get instant output
- Simple JSON API at
POST /translate
- .env-based configuration (no keys in code)
- Backend:
main.py
- Serves HTML at
GET /
viatemplates/index.html
- JSON translation API:
POST /translate
- LangChain pipeline:
ChatPromptTemplate
→ChatGoogleGenerativeAI
- Serves HTML at
- Templates & Static
- Templates:
templates/index.html
- Static assets:
static/style.css
,static/script.js
- Templates:
- Optional utility:
langchain_module.py
- Standalone helper using
google.generativeai
for multi-language mapping
- Standalone helper using
- Schemas:
schemas.py
(Pydantic models that can be reused)
- Python 3.10+
- A Google API key with Gemini access in
GOOGLE_API_KEY
- pip >= 21
- Windows, macOS, or Linux
-
Create a virtual environment
- Windows (PowerShell)
python -m venv venv .\venv\Scripts\activate
- macOS/Linux (bash/zsh)
python3 -m venv venv source venv/bin/activate
- Windows (PowerShell)
-
Install dependencies
pip install -r requirements.txt
-
Configure environment Create a
.env
file in the project root with:GOOGLE_API_KEY=your_gemini_api_key
-
Run the app
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
- 🔗 App URL: http://localhost:8000
- 📚 Swagger docs: http://localhost:8000/docs
-
Endpoints
- GET / → renders
templates/index.html
- POST /translate → returns translated text
- GET / → renders
-
Request body schema (JSON)
{ "text": "Hello, world!", "target_language": "Spanish" }
-
Response schema (JSON)
{ "translated_text": "Hola, mundo!" }
-
cURL example
curl -X POST \ -H "Content-Type: application/json" \ -d '{"text":"Good morning","target_language":"French"}' \ http://localhost:8000/translate
HTTPie
http POST :8000/translate text="Good morning" target_language="French"
- In
main.py
:ChatPromptTemplate
+ChatGoogleGenerativeAI
(gemini-1.5-flash, temperature=0) - Prompt enforces: "Only return the final translated text and nothing else."
langchain_module.py
showcases a directgoogle.generativeai
usage for multi-language mapping.
Rendered with server-side templates (Jinja2Templates
) and enhanced with a tiny JS script (static/script.js
). No Node/React required.
- Never commit your
.env
withGOOGLE_API_KEY
. - Rate limiting and auth are out-of-scope for this demo; add per your deployment needs.
-
Missing GOOGLE_API_KEY
- Ensure
.env
exists and contains a valid key.
- Ensure
-
Import/module errors
- Recreate venv and reinstall requirements.
-
API returns 500
- Check console logs; ensure the key has access to Gemini APIs.
- Double-check that your
requirements.txt
is installed in the active venv.
- Run server (dev):
uvicorn main:app --reload
- Freeze deps:
pip freeze > requirements.txt
main.py
langchain_module.py
schemas.py
templates/
index.html
static/
style.css
script.js
requirements.txt
.env (created by you)
- The app is synchronous at the API level; long inputs depend on upstream model latency.
- Consider adding caching, auth, and rate limiting for production.
- Built with FastAPI, LangChain, and Google Gemini.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with 💖 by Rishabh Dhawad