Initial Commit

This commit is contained in:
bladeclara42
2025-05-13 22:43:56 +07:00
commit 5469707c2d
16 changed files with 81 additions and 0 deletions

Binary file not shown.

Binary file not shown.

10
app/api/v1/translate.py Normal file
View File

@@ -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)

Binary file not shown.

Binary file not shown.

10
app/core/config.py Normal file
View File

@@ -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")

View File

@@ -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

7
app/main.py Normal file
View File

@@ -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"])

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -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