Nyanpre commited on
Commit
18d58ed
·
verified ·
1 Parent(s): 65aa626

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -125
app.py CHANGED
@@ -1,125 +1,44 @@
1
- <!DOCTYPE html>
2
- <html lang="ja">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>蓮ノ空聖書正典 (Dev)</title>
7
- <style>
8
- body {
9
- background-color: white; color: black; font-family: 'Helvetica Neue', Arial, sans-serif;
10
- display: flex; flex-direction: column; align-items: center; justify-content: center;
11
- min-height: 100vh; margin: 0; text-align: center;
12
- }
13
- .container { max-width: 600px; padding: 20px; }
14
- h1 { font-size: 32px; margin-bottom: 0; }
15
- .subtitle { font-size: 1.2em; font-weight: bold; margin-bottom: 30px; }
16
- #oracle-box {
17
- display: none; border: 4px solid black; padding: 30px; margin: 20px 0;
18
- background: white; min-width: 300px;
19
- }
20
- .oracle-label { font-size: 24px; margin-bottom: 10px; }
21
- .pair-name { font-size: 52px; font-weight: 900; letter-spacing: 2px; }
22
- .peace-msg { color: #d63031; font-weight: bold; font-size: 1.2em; margin-top: 20px; }
23
- .btn {
24
- background: white; border: 2px solid black; padding: 15px 30px;
25
- font-size: 20px; font-weight: bold; cursor: pointer; margin: 10px;
26
- width: 280px; transition: 0.2s;
27
- }
28
- .btn:hover { background: #eee; }
29
- .btn-primary { background: black; color: white; height: 70px; font-size: 24px; }
30
- .share-area { display: none; margin-top: 20px; }
31
- </style>
32
- </head>
33
- <body>
34
- <div class="container">
35
- <h1>⚖️ 蓮ノ空聖書正典</h1>
36
- <p class="subtitle">日付が変わるまであなたの思想は<br>統一されます。</p>
37
-
38
- <button id="draw-btn" class="btn btn-primary" onclick="getOracle()">神託を受ける</button>
39
-
40
- <div id="oracle-box">
41
- <div class="oracle-label">本日の神託</div>
42
- <div id="pair-display" class="pair-name"></div>
43
- <div class="peace-msg">これにより、不毛なカップリング論争は終結ました。</div>
44
- </div>
45
-
46
- <div id="share-area" class="share-area">
47
- <button class="btn" onclick="shareSNS('bsky')">Blueskyで信仰を広める</button><br>
48
- <button class="btn" onclick="shareSNS('x')">Xで信仰を広める</button>
49
- </div>
50
- </div>
51
-
52
- <script>
53
- const HF_URL = "https://nyanpre-cp-oracle-dev.hf.space";
54
- const GITHUB_URL = window.location.origin + window.location.pathname.split('/').slice(0, -1).join('/') + '/';
55
- let currentPair = "";
56
-
57
- // この名前が HTML の onclick="getOracle()" と対応します
58
- async function getOracle() {
59
- console.log("Starting getOracle..."); // ログ出力
60
- const btn = document.getElementById('draw-btn');
61
- btn.innerText = "交信中...";
62
- btn.disabled = true;
63
-
64
- try {
65
- let deviceId = localStorage.getItem('cp_oracle_device_id');
66
- if(!deviceId) {
67
- deviceId = Math.random().toString(36).substring(2, 15);
68
- localStorage.setItem('cp_oracle_device_id', deviceId);
69
- }
70
-
71
- // 1. 実行リクエスト (POST)
72
- const response = await fetch(`${HF_URL}/call/get_oracle`, {
73
- method: "POST",
74
- headers: { "Content-Type": "application/json" },
75
- body: JSON.stringify({ data: [deviceId] })
76
- });
77
-
78
- if (!response.ok) throw new Error('POST request failed');
79
- const { event_id } = await response.json();
80
-
81
- // 2. 結果取得 (GET)
82
- const resultResponse = await fetch(`${HF_URL}/call/get_oracle/${event_id}`);
83
- const resultText = await resultResponse.text();
84
-
85
- const lines = resultText.split('\n');
86
- let resultData = null;
87
- for (const line of lines) {
88
- if (line.startsWith('data: ')) {
89
- resultData = JSON.parse(line.substring(6));
90
- break;
91
- }
92
- }
93
-
94
- if (!resultData) throw new Error("No data received from HF");
95
-
96
- const htmlString = resultData[0];
97
- const match = htmlString.match(/letter-spacing: 2px;'>([^<]+)<\/div>/);
98
- currentPair = match ? match[1] : "運命";
99
-
100
- document.getElementById('pair-display').innerText = currentPair;
101
- document.getElementById('oracle-box').style.display = 'block';
102
- document.getElementById('share-area').style.display = 'block';
103
- btn.style.display = 'none';
104
-
105
- } catch (e) {
106
- console.error("Critical Error:", e);
107
- alert("神との交信に失敗しました。詳細はコンソールを確認してください。");
108
- btn.innerText = "神託を受ける";
109
- btn.disabled = false;
110
- }
111
- }
112
-
113
- function shareSNS(platform) {
114
- const shareUrl = `${GITHUB_URL}${encodeURIComponent(currentPair)}.html`;
115
- const text = `私は「${currentPair}」を信仰しています。\n\n蓮ノ空聖書正典:\n${shareUrl}`;
116
-
117
- if (platform === 'bsky') {
118
- window.open(`https://bsky.app/intent/compose?text=${encodeURIComponent(text)}`, '_blank');
119
- } else {
120
- window.open(`https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}`, '_blank');
121
- }
122
- }
123
- </script>
124
- </body>
125
- </html>
 
1
+ import gradio as gr
2
+ import random
3
+ from datetime import datetime, timedelta, timezone
4
+
5
+ # --- 1. 定数とメンバー設定 ---
6
+ members = ["かほ", "さや", "こず", "るり", "めぐ", "つづ", "ぎん", "すず", "ひめ", "せら", "いず", "さち"]
7
+
8
+ # --- 2. 神託ロジック ---
9
+ def get_personal_daily_oracle(device_id):
10
+ # デバイスIDと日付をシードにして、ユーザーごとに毎日固定の結果を出す
11
+ seed_base = device_id if device_id else "default_fate"
12
+ jst = timezone(timedelta(hours=9))
13
+ today_str = datetime.now(jst).strftime("%Y-%m-%d")
14
+ random.seed(f"{seed_base}_{today_str}")
15
+
16
+ selected = random.sample(members, 2)
17
+ random.shuffle(selected)
18
+ pair_name = f"{selected[0]}{selected[1]}"
19
+
20
+ # GitHub側でパースしやすいように特定の形式のHTMLを返す
21
+ oracle_html = (
22
+ f"<div style='letter-spacing: 2px;'>{pair_name}</div>"
23
+ )
24
+ return oracle_html
25
+
26
+ # --- 3. Gradio UI 設定 ---
27
+ with gr.Blocks() as demo:
28
+ device_id_input = gr.Textbox(visible=False)
29
+ result_output = gr.HTML(visible=False)
30
+
31
+ # 外部(GitHub)から叩かれるボタン(実体は隠れていてもOK)
32
+ draw_btn = gr.Button("get_oracle", visible=False)
33
+
34
+ # 重要:api_name を "get_oracle" に設定
35
+ draw_btn.click(
36
+ fn=get_personal_daily_oracle,
37
+ inputs=[device_id_input],
38
+ outputs=[result_output],
39
+ api_name="get_oracle"
40
+ )
41
+
42
+ if __name__ == "__main__":
43
+ # CORSを許可てGitHubからのアクセスを通す
44
+ demo.launch(cors_allowed_origins=["*"])