File size: 2,478 Bytes
8ed73f7
 
 
d00d9dd
1bf1a21
d00d9dd
 
 
 
 
8ed73f7
 
 
 
 
 
 
 
 
 
d00d9dd
 
 
 
8ed73f7
 
 
 
d00d9dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ed73f7
d00d9dd
8ed73f7
d00d9dd
27976eb
 
 
d00d9dd
 
 
 
 
 
 
27976eb
d00d9dd
 
8ed73f7
 
d00d9dd
8ed73f7
d00d9dd
27976eb
d00d9dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ed73f7
 
1bf1a21
d00d9dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
from translator import translate  # Pastikan file translator.py ada

app = FastAPI(
    title="Chatbot & Translator API",
    description="API untuk chatbot dan translator Indonesia-English",
    version="1.0.0"
)

# ===== Model untuk request/response =====
class ChatRequest(BaseModel):
    message: str
    history: list[dict] = []
    system_message: str = "You are a friendly chatbot."
    max_tokens: int = 512
    temperature: float = 0.7
    top_p: float = 0.95

class ChatResponse(BaseModel):
    response: str
    status: str = "success"

class TranslateRequest(BaseModel):
    text: str
    direction: str  # "ID → EN" atau "EN → ID"

class TranslateResponse(BaseModel):
    translation: str
    original_text: str
    direction: str
    status: str = "success"

# ===== Root endpoint =====
@app.get("/")
def root():
    return {
        "message": "Chatbot & Translator API", 
        "endpoints": ["/chat", "/translate"],
        "docs": "/docs"
    }

# ===== Endpoint Chatbot =====
@app.post("/chat", response_model=ChatResponse)
def chat_endpoint(req: ChatRequest):
    try:
        # TODO: Implementasi actual chatbot logic di sini
        # Contoh dengan InferenceClient atau model lainnya
        response_text = f"Simulasi jawaban untuk: {req.message}"
        
        return ChatResponse(
            response=response_text,
            status="success"
        )
    except Exception as e:
        return ChatResponse(
            response=f"Error: {str(e)}",
            status="error"
        )

# ===== Endpoint Translator =====
@app.post("/translate", response_model=TranslateResponse)
def translate_endpoint(req: TranslateRequest):
    try:
        result = translate(req.text, req.direction)
        
        return TranslateResponse(
            translation=result,
            original_text=req.text,
            direction=req.direction,
            status="success"
        )
    except Exception as e:
        return TranslateResponse(
            translation="Translation failed",
            original_text=req.text,
            direction=req.direction,
            status="error"
        )

# ===== Health check endpoint =====
@app.get("/health")
def health_check():
    return {"status": "healthy", "message": "API is running"}

# ====== Run local (Space akan otomatis pakai uvicorn) ======
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)