Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,44 @@
|
|
| 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 |
-
<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=["*"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|