Deploy-Auto / License
Phupingbut1989's picture
Create License
39c66f6 verified
Raw History Blame Contribute Delete
3.42 kB
---
# 📄 ไฟล์ที่ 2: main.py
```python
# -*- coding: utf-8 -*-
"""
🌐 CRISTAL AI — Deploy-Auto Main Application
TPS Group · Genai
Version: 1.0.0
"""
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pathlib import Path
import os
# ==========================================
# ✅ สร้างแอปพลิเคชัน — สำคัญ: ต้องมาก่อนใช้งาน!
# ==========================================
BASE_DIR = Path(__file__).resolve().parent
app = FastAPI(
title="Cristal AI",
version="1.0.0",
description="Cristal is the creator of world-class AI"
)
# ==========================================
# 📁 ตั้งค่าโฟลเดอร์ไฟล์
# ==========================================
try:
app.mount("/static", StaticFiles(directory=str(BASE_DIR / "static")), name="static")
except Exception:
pass
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
# ==========================================
# 🌐 หน้าเว็บ — Public Routes
# ==========================================
@app.get("/", response_class=HTMLResponse)
def home(request: Request):
return templates.TemplateResponse(request, "index.html", {"page": "home"})
@app.get("/signup", response_class=HTMLResponse)
def signup_page(request: Request):
return templates.TemplateResponse(request, "signup.html", {"page": "signup"})
@app.get("/login", response_class=HTMLResponse)
def login_page(request: Request):
return templates.TemplateResponse(request, "login.html", {"page": "login"})
@app.get("/chat", response_class=HTMLResponse)
def chat_page(request: Request):
return templates.TemplateResponse(request, "chat.html", {"page": "chat"})
# ==========================================
# 💬 API Endpoints
# ==========================================
@app.post("/api/chat/send")
def chat_send(message: str = Form(...)):
return {
"success": True,
"reply": f"Cristal: คุณพูดว่า — '{message}' 🤖✨",
"provider": "demo",
"version": "1.0.0"
}
@app.get("/api/status")
def api_status():
return {
"status": "online",
"name": "Cristal AI",
"version": "1.0.0",
"deploy": "Deploy-Auto ✅",
"owner": "TPS Group"
}
# ==========================================
# 🔧 เครื่องมือเพิ่ม API ได้เอง
# ==========================================
def add_api(method: str, path: str, handler):
"""
เพิ่ม API ใหม่ได้ง่ายๆ
ตัวอย่าง: add_api("GET", "/hello", lambda: {"msg": "สวัสดี"})
"""
if method.upper() == "GET":
app.get(path)(handler)
elif method.upper() == "POST":
app.post(path)(handler)
# ตัวอย่างการใช้งาน
# add_api("GET", "/hello", lambda: {"message": "สวัสดีจาก Cristal AI!"})
# ==========================================
# ✅ รันเซิร์ฟเวอร์
# ==========================================
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", 8080))
uvicorn.run(app, host="0.0.0.0", port=port)