commit 5469707c2db10be148bf0658513c48f163a1dd36 Author: bladeclara42 <71927457+bladeclara42@users.noreply.github.com> Date: Tue May 13 22:43:56 2025 +0700 Initial Commit diff --git a/.env b/.env new file mode 100644 index 0000000..3c5188b --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +OPENAI_API_KEY=sk-e2f00b9fed01443b87407513ab14c494 +OPENAI_MODEL=deepseek-chat +OPENAI_API_BASE=https://api.deepseek.com/v1 # (optional override if needed) diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3c847cc --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +OPENAI_API_KEY=sk-your-real-openai-api-key +OPENAI_MODEL=gpt-3.5-turbo +OPENAI_API_BASE=https://api.openai.com/v1 # (optional override if needed) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/app/__pycache__/main.cpython-313.pyc b/app/__pycache__/main.cpython-313.pyc new file mode 100644 index 0000000..60050dd Binary files /dev/null and b/app/__pycache__/main.cpython-313.pyc differ diff --git a/app/api/v1/__pycache__/translate.cpython-313.pyc b/app/api/v1/__pycache__/translate.cpython-313.pyc new file mode 100644 index 0000000..1e9e5e3 Binary files /dev/null and b/app/api/v1/__pycache__/translate.cpython-313.pyc differ diff --git a/app/api/v1/translate.py b/app/api/v1/translate.py new file mode 100644 index 0000000..acdd88c --- /dev/null +++ b/app/api/v1/translate.py @@ -0,0 +1,10 @@ +from fastapi import APIRouter +from app.models.translation import TranslationRequest, TranslationResponse +from app.services.translator import translate_text + +router = APIRouter() + +@router.post("/", response_model=TranslationResponse) +async def translate(request: TranslationRequest): + translated_text = await translate_text(request.text, request.target_language) + return TranslationResponse(translated_text=translated_text) diff --git a/app/core/__pycache__/config.cpython-313.pyc b/app/core/__pycache__/config.cpython-313.pyc new file mode 100644 index 0000000..eff13df Binary files /dev/null and b/app/core/__pycache__/config.cpython-313.pyc differ diff --git a/app/core/__pycache__/deepseek_client.cpython-313.pyc b/app/core/__pycache__/deepseek_client.cpython-313.pyc new file mode 100644 index 0000000..27ebb7c Binary files /dev/null and b/app/core/__pycache__/deepseek_client.cpython-313.pyc differ diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 0000000..0c13826 --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,10 @@ +import os +from dotenv import load_dotenv + +# Load .env variables +load_dotenv() + +# Read environment variables with defaults +OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "") +OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo") +OPENAI_API_BASE = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1") diff --git a/app/core/deepseek_client.py b/app/core/deepseek_client.py new file mode 100644 index 0000000..1c25d5e --- /dev/null +++ b/app/core/deepseek_client.py @@ -0,0 +1,27 @@ +# app/services/openai_service.py +import openai +from openai import OpenAI +from app.core.config import OPENAI_API_KEY, OPENAI_MODEL +from app.core.config import OPENAI_API_BASE + +# Set OpenAI API key from the environment +openai.api_key = OPENAI_API_KEY +openai.api_base = OPENAI_API_BASE + +print(openai.api_key) +print(OPENAI_MODEL) +print(OPENAI_API_BASE) + +async def chat_with_openai(messages: list): + # Use the model from environment variable or fallback to default + model = OPENAI_MODEL + client = OpenAI(api_key=openai.api_key, base_url=openai.api_base) + + response = client.chat.completions.create( + model="deepseek-chat", # Or the model you want + messages=messages, # Update this according to the new API syntax + max_tokens=100, # Example parameter + stream=False + ) + + return response.choices[0].message.content diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..4786958 --- /dev/null +++ b/app/main.py @@ -0,0 +1,7 @@ +from fastapi import FastAPI +from app.api.v1 import translate + +app = FastAPI() + +# Include your routes +app.include_router(translate.router, prefix="/api/v1/translate", tags=["translate"]) diff --git a/app/models/__pycache__/translation.cpython-313.pyc b/app/models/__pycache__/translation.cpython-313.pyc new file mode 100644 index 0000000..e78bd61 Binary files /dev/null and b/app/models/__pycache__/translation.cpython-313.pyc differ diff --git a/app/models/translation.py b/app/models/translation.py new file mode 100644 index 0000000..e999d81 --- /dev/null +++ b/app/models/translation.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel + +class TranslationRequest(BaseModel): + text: str + target_language: str # Example: 'id' for Indonesian, 'en' for English, etc. + +class TranslationResponse(BaseModel): + translated_text: str diff --git a/app/services/__pycache__/translator.cpython-313.pyc b/app/services/__pycache__/translator.cpython-313.pyc new file mode 100644 index 0000000..75270ac Binary files /dev/null and b/app/services/__pycache__/translator.cpython-313.pyc differ diff --git a/app/services/translator.py b/app/services/translator.py new file mode 100644 index 0000000..a829117 --- /dev/null +++ b/app/services/translator.py @@ -0,0 +1,9 @@ +from app.core.deepseek_client import chat_with_openai + +async def translate_text(text: str, target_language: str) -> str: + messages = [ + {"role": "system", "content": f"Translate the following text into {target_language}. Output only the translated text without explanation."}, + {"role": "user", "content": text}, + ] + translated_text = await chat_with_openai(messages) + return translated_text diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6def115 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +fastapi +uvicorn +openai +python-dotenv