Download app.py from syempuna/dev: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/syempuna/dev/resolve/main/app.py
- Command line
-
hf download hf://spaces/syempuna/dev/app.py
-
curl -L -o app.py https://huggingface.co/spaces/syempuna/dev/resolve/main/app.py
2.48 kB
| 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 ===== | |
| def root(): | |
| return { | |
| "message": "Chatbot & Translator API", | |
| "endpoints": ["/chat", "/translate"], | |
| "docs": "/docs" | |
| } | |
| # ===== Endpoint Chatbot ===== | |
| 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 ===== | |
| 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 ===== | |
| 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) |