This repository was archived by the owner on Apr 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (62 loc) · 2.38 KB
/
Copy pathapp.py
File metadata and controls
90 lines (62 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
from main import initialize_chatbot, chatbot_response
import os
import qrcode
import io
import base64
import tempfile
from dotenv import load_dotenv
dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
print(".env を読み込んでいます:", dotenv_path)
load_dotenv(dotenv_path)
app = Flask(__name__)
CORS(app)
retriever, chat_model = initialize_chatbot()
QR_FILE_PATH = os.path.join(tempfile.gettempdir(), 'last_qr.txt')
@app.route("/", methods=["GET"])
def home():
return render_template('index.html')
@app.route("/api/qr", methods=["GET"])
def get_qr_code():
try:
if not os.path.exists(QR_FILE_PATH):
return jsonify({"qr": None, "message": "QR コードはまだ生成されていません。"}), 200
with open(QR_FILE_PATH, "r") as f:
qr_content = f.read().strip()
if not qr_content:
return jsonify({"qr": None, "message": "QRコードが空です。"}), 200
img = qrcode.make(qr_content)
buf = io.BytesIO()
img.save(buf, format='PNG')
img_bytes = buf.getvalue()
qr_data_url = "data:image/png;base64," + base64.b64encode(img_bytes).decode('utf-8')
return jsonify({"qr": qr_data_url}), 200
except Exception as e:
return jsonify({"error": f"QR コードの生成に失敗しました: {str(e)}"}), 500
@app.route("/chat", methods=["POST"])
def chat_endpoint():
data = request.get_json()
if not data or "query" not in data:
return jsonify({"error": "クエリが提供されていません"}), 400
query = data["query"]
try:
response = chatbot_response(query)
if not response or response.strip() == "":
return jsonify({
"response": (
"Maaf, aku belum punya jawaban untuk itu 😔\n"
"Coba tanya hal lain, misalnya:\n"
"- Harga paket internet\n"
"- Cara daftar\n"
"- Info teknisi"
)
}), 200
return jsonify({"response": response.strip()}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8001)