Phupingbut1989 commited on
Commit
39c66f6
·
verified ·
1 Parent(s): b928ff4

Create License

Browse files
Files changed (1) hide show
  1. License +106 -0
License ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+
4
+ # 📄 ไฟล์ที่ 2: main.py
5
+ ```python
6
+ # -*- coding: utf-8 -*-
7
+ """
8
+ 🌐 CRISTAL AI — Deploy-Auto Main Application
9
+ TPS Group · Genai
10
+ Version: 1.0.0
11
+ """
12
+
13
+ from fastapi import FastAPI, Request, Form
14
+ from fastapi.responses import HTMLResponse, JSONResponse
15
+ from fastapi.staticfiles import StaticFiles
16
+ from fastapi.templating import Jinja2Templates
17
+ from pathlib import Path
18
+ import os
19
+
20
+ # ==========================================
21
+ # ✅ สร้างแอปพลิเคชัน — สำคัญ: ต้องมาก่อนใช้งาน!
22
+ # ==========================================
23
+ BASE_DIR = Path(__file__).resolve().parent
24
+ app = FastAPI(
25
+ title="Cristal AI",
26
+ version="1.0.0",
27
+ description="Cristal is the creator of world-class AI"
28
+ )
29
+
30
+ # ==========================================
31
+ # 📁 ตั้งค่าโฟลเดอร์ไฟล์
32
+ # ==========================================
33
+ try:
34
+ app.mount("/static", StaticFiles(directory=str(BASE_DIR / "static")), name="static")
35
+ except Exception:
36
+ pass
37
+
38
+ templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
39
+
40
+ # ==========================================
41
+ # 🌐 หน้าเว็บ — Public Routes
42
+ # ==========================================
43
+
44
+ @app.get("/", response_class=HTMLResponse)
45
+ def home(request: Request):
46
+ return templates.TemplateResponse(request, "index.html", {"page": "home"})
47
+
48
+ @app.get("/signup", response_class=HTMLResponse)
49
+ def signup_page(request: Request):
50
+ return templates.TemplateResponse(request, "signup.html", {"page": "signup"})
51
+
52
+ @app.get("/login", response_class=HTMLResponse)
53
+ def login_page(request: Request):
54
+ return templates.TemplateResponse(request, "login.html", {"page": "login"})
55
+
56
+ @app.get("/chat", response_class=HTMLResponse)
57
+ def chat_page(request: Request):
58
+ return templates.TemplateResponse(request, "chat.html", {"page": "chat"})
59
+
60
+ # ==========================================
61
+ # 💬 API Endpoints
62
+ # ==========================================
63
+
64
+ @app.post("/api/chat/send")
65
+ def chat_send(message: str = Form(...)):
66
+ return {
67
+ "success": True,
68
+ "reply": f"Cristal: คุณพูดว่า — '{message}' 🤖✨",
69
+ "provider": "demo",
70
+ "version": "1.0.0"
71
+ }
72
+
73
+ @app.get("/api/status")
74
+ def api_status():
75
+ return {
76
+ "status": "online",
77
+ "name": "Cristal AI",
78
+ "version": "1.0.0",
79
+ "deploy": "Deploy-Auto ✅",
80
+ "owner": "TPS Group"
81
+ }
82
+
83
+ # ==========================================
84
+ # 🔧 เครื่องมือเพิ่ม API ได้เอง
85
+ # ==========================================
86
+ def add_api(method: str, path: str, handler):
87
+ """
88
+ เพิ่ม API ใหม่ได้ง่ายๆ
89
+ ตัวอย่าง: add_api("GET", "/hello", lambda: {"msg": "สวัสดี"})
90
+ """
91
+ if method.upper() == "GET":
92
+ app.get(path)(handler)
93
+ elif method.upper() == "POST":
94
+ app.post(path)(handler)
95
+
96
+ # ตัวอย่างการใช้งาน
97
+ # add_api("GET", "/hello", lambda: {"message": "สวัสดีจาก Cristal AI!"})
98
+
99
+ # ==========================================
100
+ # ✅ รันเซิร์ฟเวอร์
101
+ # ==========================================
102
+ if __name__ == "__main__":
103
+ import uvicorn
104
+ port = int(os.getenv("PORT", 8080))
105
+ uvicorn.run(app, host="0.0.0.0", port=port)
106
+