Deploy-Auto / main.py
Phupingbut1989's picture
Create main.py
b928ff4 verified
Raw History Blame Contribute Delete
15 kB
# -*- coding: utf-8 -*-
"""
==================================================
🚀 DEPLOY-AUTO — Cristal AI All-in-One File
เวอร์ชัน: 1.0.0
เจ้าของ: TPS Group · Thanwa Phupingbut (Rufio D'noto)
ใช้งาน: รันไฟล์นี้ได้เลย หรือ Deploy บน Railway, Vercel, Render, Hang ฯลฯ
==================================================
"""
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
import sys
# ==========================================
# ✅ สร้างแอป — สำคัญที่สุด ต้องมาก่อน!
# ==========================================
BASE_DIR = Path(__file__).resolve().parent
app = FastAPI(
title="Cristal AI",
version="1.0.0",
description="Cristal is the creator of world-class AI"
)
# ==========================================
# 📁 สร้างโฟลเดอร์และไฟล์อัตโนมัติ
# ==========================================
def ensure_folders():
"""สร้างโฟลเดอร์ที่จำเป็นถ้ายังไม่มี"""
folders = [
BASE_DIR / "templates",
BASE_DIR / "static" / "css",
BASE_DIR / "static" / "js"
]
for f in folders:
f.mkdir(parents=True, exist_ok=True)
ensure_folders()
# ==========================================
# 📝 เนื้อหาไฟล์หน้าเว็บ — ฝังในโค้ดเลย
# ==========================================
FILES = {
"templates/base.html": """<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Cristal AI{% endblock %}</title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<nav class="navbar">
<div class="nav-container">
<a href="/" class="logo">◆ Cristal</a>
<div class="nav-menu">
<a href="/" class="{% if page == 'home' %}active{% endif %}">Home</a>
<a href="/chat" class="{% if page == 'chat' %}active{% endif %}">Chat</a>
</div>
<div class="nav-actions">
<a href="/login" class="btn-login">เข้าสู่ระบบ</a>
<a href="/signup" class="btn-signup">สมัครใช้งาน</a>
</div>
</div>
</nav>
<main>{% block content %}{% endblock %}</main>
<footer class="footer">
<p>© 2026 Cristal AI · TPS Group</p>
</footer>
<script src="/static/js/main.js"></script>
</body>
</html>""",
"templates/index.html": """{% extends "base.html" %}
{% block title %}Cristal AI — ปัญญาประดิษฐ์ระดับโลก{% endblock %}
{% block content %}
<section class="hero">
<div class="hero-content">
<h1>Cristal คือผู้สร้าง<br><span>ปัญญาประดิษฐ์ระดับโลก</span></h1>
<p>ออกแบบเทคโนโลยี การสนทนา และอนาคตของ AI ไปพร้อมกับคุณ</p>
<div class="hero-buttons">
<a href="/signup" class="btn-primary">เริ่มใช้งานฟรี</a>
<a href="/chat" class="btn-secondary">คุยกับ Cristal</a>
</div>
</div>
</section>
{% endblock %}""",
"templates/signup.html": """{% extends "base.html" %}
{% block title %}สมัครสมาชิก · Cristal AI{% endblock %}
{% block content %}
<section class="auth-section">
<div class="auth-card">
<h1>สร้างบัญชี Cristal</h1>
<p>เข้าร่วมระบบ AI ระดับโลก</p>
<form class="auth-form" onsubmit="event.preventDefault(); alert('✅ สมัครสำเร็จ!');">
<input type="text" placeholder="ชื่อ-นามสกุล" required>
<input type="email" placeholder="อีเมล" required>
<input type="password" placeholder="รหัสผ่าน" required>
<button type="submit" class="btn-primary full-width">สมัครใช้งานฟรี</button>
</form>
<p class="auth-switch">มีบัญชีอยู่แล้ว? <a href="/login">เข้าสู่ระบบ</a></p>
</div>
</section>
{% endblock %}""",
"templates/login.html": """{% extends "base.html" %}
{% block title %}เข้าสู่ระบบ · Cristal AI{% endblock %}
{% block content %}
<section class="auth-section">
<div class="auth-card">
<h1>ยินดีต้อนรับกลับมา</h1>
<p>เข้าสู่ระบบเพื่อใช้งาน Cristal AI</p>
<form class="auth-form" onsubmit="event.preventDefault(); alert('✅ เข้าสู่ระบบสำเร็จ!');">
<input type="email" placeholder="อีเมล" required>
<input type="password" placeholder="รหัสผ่าน" required>
<button type="submit" class="btn-primary full-width">เข้าสู่ระบบ</button>
</form>
<p class="auth-switch">ยังไม่มีบัญชี? <a href="/signup">สมัครใช้งาน</a></p>
</div>
</section>
{% endblock %}""",
"templates/chat.html": """{% extends "base.html" %}
{% block title %}พูดคุย · Cristal AI{% endblock %}
{% block content %}
<section class="chat-container">
<div class="chat-box">
<div class="chat-header">
<h2>🤖 Cristal AI — พร้อมช่วยเหลือ</h2>
</div>
<div class="chat-messages" id="chatMessages">
<div class="ai-message">
<div class="ai-avatar">🤖</div>
<div class="message-bubble">สวัสดีครับ! ผมคือ Cristal AI ✨<br>พิมพ์ข้อความด้านล่างเพื่อคุยกับผมได้เลยครับ</div>
</div>
</div>
<div class="chat-input">
<input type="text" id="messageInput" placeholder="พิมพ์ข้อความ...">
<button id="sendBtn" class="btn-primary">ส่ง</button>
</div>
</div>
</section>
<script>
const input = document.getElementById('messageInput');
const messages = document.getElementById('chatMessages');
const sendBtn = document.getElementById('sendBtn');
function addMessage(text, isUser = false) {
const div = document.createElement('div');
div.className = isUser ? 'user-message' : 'ai-message';
div.innerHTML = isUser
? `<div class="message-bubble user-bubble">${escapeHtml(text)}</div>`
: `<div class="ai-avatar">🤖</div><div class="message-bubble">${text}</div>`;
messages.appendChild(div);
messages.scrollTop = messages.scrollHeight;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
async function sendMessage() {
const text = input.value.trim();
if (!text) return;
addMessage(text, true);
input.value = '';
const formData = new FormData();
formData.append('message', text);
try {
const res = await fetch('/api/chat/send', {
method: 'POST',
body: formData
});
const data = await res.json();
addMessage(data.reply);
} catch (err) {
addMessage('❌ ขออภัย ระบบไม่ตอบสนอง กรุณาลองใหม่อีกครั้ง');
}
}
sendBtn.addEventListener('click', sendMessage);
input.addEventListener('keypress', e => e.key === 'Enter' && sendMessage());
</script>
{% endblock %}""",
"static/css/style.css": """* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
body {
background: #0f0f1a;
color: #fff;
min-height: 100vh;
}
.navbar {
background: #12121f;
border-bottom: 1px solid #2a2a3f;
padding: 1rem 2rem;
position: sticky;
top: 0;
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 1rem;
}
.logo {
font-size: 1.3rem;
font-weight: 700;
color: #fff;
text-decoration: none;
}
.nav-menu a {
color: #b0b0c0;
text-decoration: none;
margin: 0 0.8rem;
}
.nav-menu a:hover, .nav-menu a.active {
color: #8b5cf6;
}
.nav-actions a {
text-decoration: none;
padding: 0.6rem 1.2rem;
border-radius: 8px;
margin-left: 0.5rem;
}
.btn-login { color: #fff; border: 1px solid transparent; }
.btn-login:hover { border-color: #444; }
.btn-signup { background: #8b5cf6; color: #fff; font-weight: 500; }
.hero {
max-width: 800px;
margin: 6rem auto;
text-align: center;
padding: 2rem;
}
.hero h1 {
font-size: 3rem;
line-height: 1.2;
margin-bottom: 1.5rem;
}
.hero h1 span {
background: linear-gradient(135deg, #8b5cf6, #d946ef);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.hero p { font-size: 1.2rem; color: #9ca3af; margin-bottom: 2.5rem; }
.hero-buttons a {
display: inline-block;
padding: 1rem 2rem;
border-radius: 10px;
text-decoration: none;
font-weight: 600;
margin: 0 0.5rem;
}
.btn-primary { background: #8b5cf6; color: white; border: none; cursor: pointer; transition: transform 0.2s; }
.btn-primary:hover { transform: translateY(-2px); }
.btn-secondary { background: #1f1f2e; color: white; border: 1px solid #333; }
.auth-section {
max-width: 420px;
margin: 4rem auto;
padding: 2rem;
}
.auth-card h1 { font-size: 1.8rem; margin-bottom: 0.5rem; }
.auth-card p { color: #9ca3af; margin-bottom: 2rem; }
.auth-form input {
width: 100%;
padding: 0.9rem 1rem;
margin-bottom: 1rem;
background: #1f1f2e;
border: 1px solid #333;
border-radius: 8px;
color: #fff;
font-size: 1rem;
outline: none;
}
.auth-form input:focus { border-color: #8b5cf6; }
.full-width { width: 100%; }
.auth-switch { text-align: center; margin-top: 1.5rem; color: #9ca3af; }
.auth-switch a { color: #8b5cf6; text-decoration: none; }
.chat-container {
max-width: 700px;
margin: 2rem auto;
padding: 0 1rem;
}
.chat-box {
background: #12121f;
border-radius: 16px;
border: 1px solid #2a2a3f;
overflow: hidden;
}
.chat-header {
padding: 1.2rem 1.5rem;
border-bottom: 1px solid #2a2a3f;
}
.chat-header h2 { font-size: 1.1rem; }
.chat-messages {
height: 450px;
overflow-y: auto;
padding: 1.5rem;
background: #0f0f1a;
}
.ai-message, .user-message {
display: flex;
margin-bottom: 1.2rem;
gap: 0.8rem;
}
.user-message { flex-direction: row-reverse; }
.ai-avatar { font-size: 1.8rem; flex-shrink: 0; }
.message-bubble {
background: #1f1f2e;
padding: 0.9rem 1.2rem;
border-radius: 12px;
line-height: 1.5;
max-width: 80%;
}
.user-bubble { background: #8b5cf6; }
.chat-input {
display: flex;
gap: 0.8rem;
padding: 1.2rem;
border-top: 1px solid #2a2a3f;
}
.chat-input input {
flex: 1;
padding: 0.9rem 1.2rem;
background: #1f1f2e;
border: 1px solid #333;
border-radius: 8px;
color: #fff;
font-size: 1rem;
outline: none;
}
.chat-input input:focus { border-color: #8b5cf6; }
.footer {
text-align: center;
padding: 2rem;
color: #6b7280;
font-size: 0.9rem;
border-top: 1px solid #2a2a3f;
margin-top: 4rem;
}
@media(max-width: 640px) {
.hero h1 { font-size: 2rem; }
.nav-container { justify-content: center; text-align: center; }
.nav-menu { width: 100%; margin: 0.5rem 0; }
}""",
"static/js/main.js": """console.log("✨ Cristal AI System Loaded");
"""
}
# ==========================================
# 💾 เขียนไฟล์อัตโนมัติเมื่อรันครั้งแรก
# ==========================================
def write_files():
"""สร้างไฟล์หน้าเว็บอัตโนมัติ"""
for path, content in FILES.items():
file_path = BASE_DIR / path
if not file_path.exists():
file_path.write_text(content, encoding="utf-8")
print(f"✅ สร้างไฟล์: {path}")
write_files()
# ตั้งค่า templates
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
# ==========================================
# 🌐 หน้าเว็บ
# ==========================================
@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
# ==========================================
@app.post("/api/chat/send")
def chat_send(message: str = Form(...)):
return {
"success": True,
"reply": f"Cristal: คุณพูดว่า — '{message}' 🤖✨\nระบบทำงานปกติแล้วครับ! ✅",
"version": "1.0.0"
}
@app.get("/api/status")
def api_status():
return {
"status": "online",
"name": "Cristal AI",
"version": "1.0.0",
"deploy": "Deploy-Auto ✅",
"note": "All-in-One Single File — Ready to Deploy"
}
# ==========================================
# ✅ รันเซิร์ฟเวอร์
# ==========================================
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", 8080))
print(f"\n🚀 ========================================")
print(f"✨ Cristal AI กำลังทำงานแล้วครับ!")
print(f"🌐 เปิดที่: http://localhost:{port}")
print(f"📖 API Status: http://localhost:{port}/api/status")
print(f"🚀 Deploy-Auto — Ready to Deploy")
print(f"=========================================\n")
uvicorn.run(app, host="0.0.0.0", port=port)