Skip to content

Commit

Permalink
adding translation module
Browse files Browse the repository at this point in the history
  • Loading branch information
suparious committed Jun 21, 2024
1 parent e9913c6 commit ef5ad53
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.modules.wikipedia_query_module import WikipediaQueryModule
from app.modules.product_comparison_module import ProductComparisonModule
from app.modules.agentic_reflection_module import AgenticReflectionModule
from app.modules.translation_module import TranslationModule
from srt_core.config import Config
from srt_core.utils.logger import Logger
import uvicorn
Expand Down Expand Up @@ -67,13 +68,23 @@
agentic_reflection_module = None
logger.info(f"Agentic Reflection module could not be initialized: {e}. Reflection functionality is disabled.")

try:
translation_module = TranslationModule(config, logger)
except ImportError as e:
translation_module = None
logger.info(f"Translation module could not be initialized: {e}. Translation functionality is disabled.")

class ChatRequest(BaseModel):
message: str

class ChatResponse(BaseModel):
response: str

class TranslationRequest(BaseModel):
text: str
source_language: str
target_language: str

@app.get("/", summary="Health Check", tags=["Health"])
def health_check():
logger.debug("Health check endpoint called")
Expand Down Expand Up @@ -172,6 +183,16 @@ def reflective_response(input_message: str):
logger.error(f"Error processing reflective response for message: {input_message}, error: {e}")
raise HTTPException(status_code=500, detail="Error processing reflective response")

@app.post("/translate", response_model=ChatResponse, summary="Translate text", tags=["Translation Module"])
async def translate(request: TranslationRequest):
if not translation_module:
raise HTTPException(status_code=501, detail="Translation functionality is disabled.")
try:
translated_text = translation_module.translate(request.text, request.source_language, request.target_language)
return {"response": translated_text}
except Exception as e:
logger.error(f"Error translating text: {e}")
raise HTTPException(status_code=500, detail="Internal Server Error")

if __name__ == "__main__":
uvicorn.run("app.api_service:app", host=server_name, port=server_port, reload=False, app_dir="app/")
17 changes: 17 additions & 0 deletions app/cli_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from app.modules.wikipedia_query_module import WikipediaQueryModule
from app.modules.product_comparison_module import ProductComparisonModule
from app.modules.agentic_reflection_module import AgenticReflectionModule
from app.modules.translation_module import TranslationModule
from srt_core.config import Config
from srt_core.utils.logger import Logger

Expand Down Expand Up @@ -57,6 +58,13 @@ def main():
agentic_reflection_module = None
logger.info(f"Agentic Reflection module could not be initialized: {e}. Reflection functionality is disabled.")

# Initialize TranslationModule with error handling
try:
translation_module = TranslationModule(config, logger)
except ImportError as e:
translation_module = None
logger.info(f"Translation module could not be initialized: {e}. Translation functionality is disabled.")

while True:
user_input = input("> ")

Expand Down Expand Up @@ -141,6 +149,15 @@ def main():
print(f"Error processing reflective response: {e}")
else:
print("Reflection functionality is disabled due to missing dependencies.")

elif user_input.startswith("translate:"):
if translation_module:
_, text, source_language, target_language = user_input.split(",")
translated_text = translation_module.translate(text.strip(), source_language.strip(),
target_language.strip())
print(f"Translated Text: {translated_text}")
else:
print("Translation functionality is disabled due to import issues.")
else:
# Universal chat handling with function calling
try:
Expand Down
21 changes: 21 additions & 0 deletions app/modules/translation_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from llama_cpp_agent import LlamaCppAgent
from llama_cpp_agent.providers import LlamaCppServerProvider
from llama_cpp_agent.llm_output_settings import MessagesFormatterType
from app.modules.base_module import BaseModule

class TranslationModule(BaseModule):
def __init__(self, config, logger):
super().__init__(config, logger, ["llama_cpp_agent"])
if self.dependencies_available:
self.provider = self._initialize_provider()
self.agent = self._initialize_agent(
system_prompt="You are a highly skilled translator. Your task is to translate text from one language to another.",
predefined_messages_formatter_type=MessagesFormatterType.MISTRAL
)

def translate(self, text, source_language, target_language):
prompt = f"Translate the following text from {source_language} to {target_language}:\n\n{text}"
self.logger.debug(f"Translating text: {text} from {source_language} to {target_language}")
response = self.agent.get_chat_response(prompt)
self.logger.debug(f"Translation result: {response.strip()}")
return response.strip()

0 comments on commit ef5ad53

Please sign in to comment.