Skip to content

Commit ef727ad

Browse files
Add CodeTranslation blueprint
1 parent 68b7ef4 commit ef727ad

30 files changed

Lines changed: 1918 additions & 0 deletions

CodeTranslation/.env.example

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Backend API Configuration
2+
BACKEND_PORT=5001
3+
4+
# Keycloak Authentication
5+
BASE_URL=https://your-enterprise-api.com
6+
KEYCLOAK_CLIENT_ID=api
7+
KEYCLOAK_CLIENT_SECRET=your-client-secret
8+
9+
# Model Configuration - CodeLlama-34b-instruct
10+
INFERENCE_MODEL_ENDPOINT=CodeLlama-34b-Instruct-hf
11+
INFERENCE_MODEL_NAME=codellama/CodeLlama-34b-Instruct-hf
12+
13+
# LLM Settings
14+
LLM_TEMPERATURE=0.2
15+
LLM_MAX_TOKENS=4096
16+
17+
# Code Translation Settings
18+
MAX_CODE_LENGTH=10000
19+
MAX_FILE_SIZE=10485760
20+
21+
# CORS Configuration
22+
CORS_ALLOW_ORIGINS=["http://localhost:5173", "http://localhost:3000"]

CodeTranslation/.gitignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Environment variables
2+
.env
3+
.env.local
4+
.env.production
5+
.env.*.local
6+
7+
# Python
8+
__pycache__/
9+
*.py[cod]
10+
*$py.class
11+
*.so
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
venv/
30+
env/
31+
ENV/
32+
.venv
33+
34+
# Node
35+
node_modules/
36+
npm-debug.log*
37+
yarn-debug.log*
38+
yarn-error.log*
39+
pnpm-debug.log*
40+
lerna-debug.log*
41+
.npm
42+
.yarn
43+
package-lock.json
44+
45+
# IDE
46+
.vscode/
47+
.idea/
48+
*.swp
49+
*.swo
50+
*~
51+
.DS_Store
52+
53+
# Build outputs
54+
dist/
55+
*.log
56+
57+
# Temporary files
58+
*.tmp
59+
tmp/
60+
temp/

CodeTranslation/README.md

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
## Code Translation
2+
3+
A full-stack code translation application that converts code between programming languages using AI.
4+
The system integrates a FastAPI backend powered by CodeLlama-34b-instruct, alongside a modern React + Vite + Tailwind CSS frontend for an intuitive translation experience.
5+
6+
## Table of Contents
7+
8+
- [Project Overview](#project-overview)
9+
- [Features](#features)
10+
- [Architecture](#architecture)
11+
- [Prerequisites](#prerequisites)
12+
- [Quick Start Deployment](#quick-start-deployment)
13+
- [User Interface](#user-interface)
14+
- [Troubleshooting](#troubleshooting)
15+
16+
---
17+
18+
## Project Overview
19+
20+
The **Code Translation** application demonstrates how large language models can be used to translate code between different programming languages. It accepts source code in one language, processes it through CodeLlama-34b-instruct, and returns translated code in the target language. This project integrates seamlessly with cloud-hosted APIs or local model endpoints, offering flexibility for research, enterprise, or educational use.
21+
22+
---
23+
24+
## Features
25+
26+
**Backend**
27+
28+
- Code translation between 6 languages (Java, C, C++, Python, Rust, Go)
29+
- PDF code extraction with pattern recognition
30+
- CodeLlama-34b-instruct for accurate translations
31+
- Enterprise inference endpoints
32+
- Keycloak authentication for secure API access
33+
- Comprehensive error handling and logging
34+
- File validation and size limits
35+
- CORS enabled for web integration
36+
- Health check endpoints
37+
- Modular architecture (config + models + services)
38+
39+
**Frontend**
40+
41+
- Side-by-side code comparison interface
42+
- Language selection dropdowns (6 languages)
43+
- PDF file upload with drag-and-drop support
44+
- Real-time character counter with limits
45+
- Modern, responsive design with Tailwind CSS
46+
- Built with Vite for fast development
47+
- Live status updates
48+
- Copy to clipboard functionality
49+
- Mobile-friendly
50+
51+
---
52+
53+
## Architecture
54+
55+
Below is the architecture as it consists of a server that waits for code input or PDF uploads. Once code is provided, the server calls the CodeLlama model to translate the code to the target language.
56+
57+
```mermaid
58+
graph TB
59+
subgraph "User Interface"
60+
A[React Frontend<br/>Port 3000]
61+
A1[Code Input]
62+
A2[PDF Upload]
63+
A3[Language Selection]
64+
end
65+
66+
subgraph "FastAPI Backend"
67+
B[API Server<br/>Port 5001]
68+
C[PDF Service]
69+
D[API Client]
70+
end
71+
72+
subgraph "External Services"
73+
E[Keycloak Auth]
74+
F[CodeLlama-34b Model]
75+
end
76+
77+
A1 --> B
78+
A2 --> B
79+
A3 --> B
80+
B --> C
81+
C -->|Extracted Code| B
82+
B --> D
83+
D -->|Get Token| E
84+
E -->|Access Token| D
85+
D -->|Translate Code + Token| F
86+
F -->|Translated Code| D
87+
D --> B
88+
B --> A
89+
90+
style A fill:#e1f5ff
91+
style B fill:#fff4e1
92+
style F fill:#e1ffe1
93+
```
94+
95+
This application is built with enterprise inference capabilities using Keycloak for authentication and CodeLlama-34b-instruct for code translation.
96+
97+
**Service Components:**
98+
99+
1. **React Web UI (Port 3000)** - Provides side-by-side code comparison interface with language selection, PDF upload, and real-time translation results
100+
101+
2. **FastAPI Backend (Port 5001)** - Handles code validation, PDF extraction, Keycloak authentication, and orchestrates code translation through CodeLlama model
102+
103+
**Typical Flow:**
104+
105+
1. User enters code or uploads a PDF through the web UI.
106+
2. The backend validates the input and extracts code if needed.
107+
3. The backend authenticates with Keycloak and calls CodeLlama model.
108+
4. The model translates the code to the target language.
109+
5. The translated code is returned and displayed to the user.
110+
6. User can copy the translated code with one click.
111+
112+
---
113+
114+
## Prerequisites
115+
116+
### System Requirements
117+
118+
Before you begin, ensure you have the following installed:
119+
120+
- **Docker and Docker Compose**
121+
- **Enterprise inference endpoint access** (Keycloak authentication)
122+
123+
### Verify Docker Installation
124+
125+
```bash
126+
# Check Docker version
127+
docker --version
128+
129+
# Check Docker Compose version
130+
docker compose version
131+
132+
# Verify Docker is running
133+
docker ps
134+
```
135+
---
136+
137+
## Quick Start Deployment
138+
139+
### Clone the Repository
140+
141+
```bash
142+
git clone https://github.com/opea-project/GenAIExamples.git
143+
cd GenAIExamples/CodeTranslation
144+
```
145+
146+
### Set up the Environment
147+
148+
This application requires an `.env` file in the root directory for proper configuration. Create it with the commands below:
149+
150+
```bash
151+
# Create the .env file
152+
cat > .env << EOF
153+
# Backend API Configuration
154+
BACKEND_PORT=5001
155+
156+
# Required - Enterprise/Keycloak Configuration
157+
BASE_URL=https://api.example.com
158+
KEYCLOAK_CLIENT_ID=api
159+
KEYCLOAK_CLIENT_SECRET=your_client_secret
160+
161+
# Required - Model Configuration
162+
INFERENCE_MODEL_ENDPOINT=CodeLlama-34b-Instruct-hf
163+
INFERENCE_MODEL_NAME=codellama/CodeLlama-34b-Instruct-hf
164+
165+
# LLM Settings
166+
LLM_TEMPERATURE=0.2
167+
LLM_MAX_TOKENS=4096
168+
169+
# Code Translation Settings
170+
MAX_CODE_LENGTH=10000
171+
MAX_FILE_SIZE=10485760
172+
173+
# CORS Configuration
174+
CORS_ALLOW_ORIGINS=["http://localhost:5173", "http://localhost:3000"]
175+
EOF
176+
```
177+
178+
Or manually create `.env` with:
179+
180+
```bash
181+
# Backend API Configuration
182+
BACKEND_PORT=5001
183+
184+
# Required - Enterprise/Keycloak Configuration
185+
BASE_URL=https://api.example.com
186+
KEYCLOAK_CLIENT_ID=api
187+
KEYCLOAK_CLIENT_SECRET=your_client_secret
188+
189+
# Required - Model Configuration
190+
INFERENCE_MODEL_ENDPOINT=CodeLlama-34b-Instruct-hf
191+
INFERENCE_MODEL_NAME=codellama/CodeLlama-34b-Instruct-hf
192+
193+
# LLM Settings
194+
LLM_TEMPERATURE=0.2
195+
LLM_MAX_TOKENS=4096
196+
197+
# Code Translation Settings
198+
MAX_CODE_LENGTH=10000
199+
MAX_FILE_SIZE=10485760
200+
201+
# CORS Configuration
202+
CORS_ALLOW_ORIGINS=["http://localhost:5173", "http://localhost:3000"]
203+
```
204+
205+
**Note**: The docker-compose.yaml file automatically loads environment variables from `.env` for the backend service.
206+
207+
### Running the Application
208+
209+
Start both API and UI services together with Docker Compose:
210+
211+
```bash
212+
# From the CodeTranslation directory
213+
docker compose up --build
214+
215+
# Or run in detached mode (background)
216+
docker compose up -d --build
217+
```
218+
219+
The API will be available at: `http://localhost:5001`
220+
The UI will be available at: `http://localhost:3000`
221+
222+
**View logs**:
223+
224+
```bash
225+
# All services
226+
docker compose logs -f
227+
228+
# Backend only
229+
docker compose logs -f backend
230+
231+
# Frontend only
232+
docker compose logs -f frontend
233+
```
234+
235+
**Verify the services are running**:
236+
237+
```bash
238+
# Check API health
239+
curl http://localhost:5001/health
240+
241+
# Check if containers are running
242+
docker compose ps
243+
```
244+
245+
## User Interface
246+
247+
**Using the Application**
248+
249+
Make sure you are at the localhost:3000 url
250+
251+
You will be directed to the main page which has each feature
252+
253+
![User Interface](images/ui.png)
254+
255+
The interface provides:
256+
257+
Translate code:
258+
259+
- Select source language from dropdown (Java, C, C++, Python, Rust, Go)
260+
- Select target language from dropdown
261+
- Enter or paste your code in the left textarea
262+
- Click "Translate Code" button
263+
- View translated code in the right textarea
264+
- Click "Copy" to copy the result
265+
266+
Upload a PDF:
267+
268+
- Scroll to the "Alternative: Upload PDF" section
269+
- Drag and drop a PDF file, or
270+
- Click "browse" to select a file
271+
- Wait for code extraction to complete
272+
- Extracted code appears in the source code box
273+
274+
**UI Configuration**
275+
276+
When running with Docker Compose, the UI automatically connects to the backend API. The frontend is available at `http://localhost:3000` and the API at `http://localhost:5001`.
277+
278+
279+
For production deployments, you may want to configure a reverse proxy or update the API URL in the frontend configuration.
280+
281+
### Stopping the Application
282+
283+
284+
```bash
285+
docker compose down
286+
```
287+
288+
---
289+
290+
## Troubleshooting
291+
292+
For comprehensive troubleshooting guidance, common issues, and solutions, refer to:
293+
294+
[Troubleshooting Guide - TROUBLESHOOTING.md](./TROUBLESHOOTING.md)

0 commit comments

Comments
 (0)