Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions sample_solutions/CodeTranslation/api/services/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_inference_client(self):

def translate_code(self, source_code: str, source_lang: str, target_lang: str) -> str:
"""
Translate code from one language to another using CodeLlama-34b-instruct
Translate code from one language to another using an instruct model.

Args:
source_code: Code to translate
Expand All @@ -48,42 +48,41 @@ def translate_code(self, source_code: str, source_lang: str, target_lang: str) -
try:
client = self.get_inference_client()

# Create prompt for code translation
prompt = f"""Translate the following {source_lang} code to {target_lang}.
Only output the translated code without any explanations or markdown formatting.

{source_lang} code:
```
{source_code}
```

{target_lang} code:
```"""

logger.info(f"Translating code from {source_lang} to {target_lang}")

# Use completions endpoint for CodeLlama
response = client.completions.create(
response = client.chat.completions.create(
model=config.INFERENCE_MODEL_NAME,
prompt=prompt,
messages=[
{
"role": "system",
"content": (
f"You are an expert code translator. "
f"Translate {source_lang} code to {target_lang}. "
f"Output only the translated code with no explanations or markdown."
),
},
{
"role": "user",
"content": f"Translate this {source_lang} code to {target_lang}:\n\n{source_code}",
},
],
max_tokens=config.LLM_MAX_TOKENS,
temperature=config.LLM_TEMPERATURE,
stop=["```"] # Stop at closing code block
)

# Handle response structure
if hasattr(response, 'choices') and len(response.choices) > 0:
choice = response.choices[0]
if hasattr(choice, 'text'):
translated_code = choice.text.strip()
logger.info(f"Successfully translated code ({len(translated_code)} characters)")
return translated_code
else:
logger.error(f"Unexpected response structure: {type(choice)}, {choice}")
return ""
else:
logger.error(f"Unexpected response: {type(response)}, {response}")
return ""
if response.choices:
translated_code = response.choices[0].message.content.strip()
# Strip markdown code fences if model wraps output anyway
if translated_code.startswith("```"):
lines = translated_code.splitlines()
translated_code = "\n".join(
lines[1:-1] if lines[-1].strip() == "```" else lines[1:]
).strip()
logger.info(f"Successfully translated code ({len(translated_code)} characters)")
return translated_code

logger.error(f"Empty choices in response: {response}")
return ""
except Exception as e:
logger.error(f"Error translating code: {str(e)}", exc_info=True)
raise
Expand Down
9 changes: 8 additions & 1 deletion sample_solutions/CodeTranslation/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ services:
build:
context: ./api
dockerfile: Dockerfile
args:
- http_proxy=${http_proxy}
- https_proxy=${https_proxy}
- no_proxy=${no_proxy}
container_name: code-trans-backend
ports:
- "5001:5001"
Expand Down Expand Up @@ -32,14 +36,17 @@ services:
build:
context: ./ui
dockerfile: Dockerfile
args:
- http_proxy=${http_proxy}
- https_proxy=${https_proxy}
- no_proxy=${no_proxy}
container_name: code-trans-frontend
ports:
- "3000:8080"
depends_on:
- backend
networks:
- code-trans-network
restart: unless-stopped

networks:
code-trans-network:
Expand Down