app.py
CHANGED
|
@@ -1,9 +1,13 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import uvicorn
|
| 4 |
-
from translator import translate
|
| 5 |
|
| 6 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# ===== Model untuk request/response =====
|
| 9 |
class ChatRequest(BaseModel):
|
|
@@ -14,22 +18,72 @@ class ChatRequest(BaseModel):
|
|
| 14 |
temperature: float = 0.7
|
| 15 |
top_p: float = 0.95
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class TranslateRequest(BaseModel):
|
| 18 |
text: str
|
| 19 |
direction: str # "ID → EN" atau "EN → ID"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# ===== Endpoint Chatbot =====
|
| 22 |
-
@app.post("/chat")
|
| 23 |
def chat_endpoint(req: ChatRequest):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# ===== Endpoint Translator =====
|
| 28 |
-
@app.post("/translate")
|
| 29 |
def translate_endpoint(req: TranslateRequest):
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ====== Run local (Space akan otomatis pakai uvicorn) ======
|
| 34 |
if __name__ == "__main__":
|
| 35 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import uvicorn
|
| 4 |
+
from translator import translate # Pastikan file translator.py ada
|
| 5 |
|
| 6 |
+
app = FastAPI(
|
| 7 |
+
title="Chatbot & Translator API",
|
| 8 |
+
description="API untuk chatbot dan translator Indonesia-English",
|
| 9 |
+
version="1.0.0"
|
| 10 |
+
)
|
| 11 |
|
| 12 |
# ===== Model untuk request/response =====
|
| 13 |
class ChatRequest(BaseModel):
|
|
|
|
| 18 |
temperature: float = 0.7
|
| 19 |
top_p: float = 0.95
|
| 20 |
|
| 21 |
+
class ChatResponse(BaseModel):
|
| 22 |
+
response: str
|
| 23 |
+
status: str = "success"
|
| 24 |
+
|
| 25 |
class TranslateRequest(BaseModel):
|
| 26 |
text: str
|
| 27 |
direction: str # "ID → EN" atau "EN → ID"
|
| 28 |
|
| 29 |
+
class TranslateResponse(BaseModel):
|
| 30 |
+
translation: str
|
| 31 |
+
original_text: str
|
| 32 |
+
direction: str
|
| 33 |
+
status: str = "success"
|
| 34 |
+
|
| 35 |
+
# ===== Root endpoint =====
|
| 36 |
+
@app.get("/")
|
| 37 |
+
def root():
|
| 38 |
+
return {
|
| 39 |
+
"message": "Chatbot & Translator API",
|
| 40 |
+
"endpoints": ["/chat", "/translate"],
|
| 41 |
+
"docs": "/docs"
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
# ===== Endpoint Chatbot =====
|
| 45 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 46 |
def chat_endpoint(req: ChatRequest):
|
| 47 |
+
try:
|
| 48 |
+
# TODO: Implementasi actual chatbot logic di sini
|
| 49 |
+
# Contoh dengan InferenceClient atau model lainnya
|
| 50 |
+
response_text = f"Simulasi jawaban untuk: {req.message}"
|
| 51 |
+
|
| 52 |
+
return ChatResponse(
|
| 53 |
+
response=response_text,
|
| 54 |
+
status="success"
|
| 55 |
+
)
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return ChatResponse(
|
| 58 |
+
response=f"Error: {str(e)}",
|
| 59 |
+
status="error"
|
| 60 |
+
)
|
| 61 |
|
| 62 |
# ===== Endpoint Translator =====
|
| 63 |
+
@app.post("/translate", response_model=TranslateResponse)
|
| 64 |
def translate_endpoint(req: TranslateRequest):
|
| 65 |
+
try:
|
| 66 |
+
result = translate(req.text, req.direction)
|
| 67 |
+
|
| 68 |
+
return TranslateResponse(
|
| 69 |
+
translation=result,
|
| 70 |
+
original_text=req.text,
|
| 71 |
+
direction=req.direction,
|
| 72 |
+
status="success"
|
| 73 |
+
)
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return TranslateResponse(
|
| 76 |
+
translation="Translation failed",
|
| 77 |
+
original_text=req.text,
|
| 78 |
+
direction=req.direction,
|
| 79 |
+
status="error"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# ===== Health check endpoint =====
|
| 83 |
+
@app.get("/health")
|
| 84 |
+
def health_check():
|
| 85 |
+
return {"status": "healthy", "message": "API is running"}
|
| 86 |
|
| 87 |
# ====== Run local (Space akan otomatis pakai uvicorn) ======
|
| 88 |
if __name__ == "__main__":
|
| 89 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|