Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
-
import
|
|
|
2 |
import numpy as np
|
3 |
import librosa
|
4 |
-
from transformers import pipeline
|
5 |
from datetime import datetime
|
6 |
-
import
|
|
|
|
|
7 |
import requests
|
8 |
-
import json
|
9 |
from dotenv import load_dotenv
|
10 |
|
11 |
# 환경변수 로드
|
@@ -31,6 +32,26 @@ ONCHEON_STORY = """
|
|
31 |
온천천 온천장역에서 장전역까지 걸으며 더 깊은 체험이 가능합니다.
|
32 |
"""
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
class SimpleDB:
|
35 |
def __init__(self, reflections_path="data/reflections.json", wishes_path="data/wishes.json"):
|
36 |
self.reflections_path = reflections_path
|
@@ -349,6 +370,188 @@ def generate_image_from_prompt(prompt):
|
|
349 |
except Exception as e:
|
350 |
print(f"Error generating image: {str(e)}")
|
351 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
|
353 |
def safe_state_update(state, updates):
|
354 |
try:
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
import numpy as np
|
4 |
import librosa
|
|
|
5 |
from datetime import datetime
|
6 |
+
from flask import Flask, send_from_directory, render_template
|
7 |
+
import gradio as gr
|
8 |
+
from transformers import pipeline
|
9 |
import requests
|
|
|
10 |
from dotenv import load_dotenv
|
11 |
|
12 |
# 환경변수 로드
|
|
|
32 |
온천천 온천장역에서 장전역까지 걸으며 더 깊은 체험이 가능합니다.
|
33 |
"""
|
34 |
|
35 |
+
# Flask 앱 초기화
|
36 |
+
app = Flask(__name__)
|
37 |
+
|
38 |
+
# 환경변수 로드
|
39 |
+
load_dotenv()
|
40 |
+
|
41 |
+
# Flask 라우트
|
42 |
+
@app.route('/static/<path:path>')
|
43 |
+
def serve_static(path):
|
44 |
+
return send_from_directory('static', path)
|
45 |
+
|
46 |
+
@app.route('/assets/<path:path>')
|
47 |
+
def serve_assets(path):
|
48 |
+
return send_from_directory('assets', path)
|
49 |
+
|
50 |
+
@app.route('/wishes/<path:path>')
|
51 |
+
def serve_wishes(path):
|
52 |
+
return send_from_directory('data/wishes', path)
|
53 |
+
|
54 |
+
|
55 |
class SimpleDB:
|
56 |
def __init__(self, reflections_path="data/reflections.json", wishes_path="data/wishes.json"):
|
57 |
self.reflections_path = reflections_path
|
|
|
370 |
except Exception as e:
|
371 |
print(f"Error generating image: {str(e)}")
|
372 |
return None
|
373 |
+
|
374 |
+
def create_pwa_files():
|
375 |
+
"""PWA 필요 파일들 생성"""
|
376 |
+
# manifest.json 생성
|
377 |
+
manifest_path = 'static/manifest.json'
|
378 |
+
if not os.path.exists(manifest_path):
|
379 |
+
manifest_data = {
|
380 |
+
"name": "디지털 굿판",
|
381 |
+
"short_name": "디지털 굿판",
|
382 |
+
"description": "현대 도시 속 디지털 의례 공간",
|
383 |
+
"start_url": "/",
|
384 |
+
"display": "standalone",
|
385 |
+
"background_color": "#ffffff",
|
386 |
+
"theme_color": "#000000",
|
387 |
+
"orientation": "portrait",
|
388 |
+
"icons": [
|
389 |
+
{
|
390 |
+
"src": "/static/icons/icon-72x72.png",
|
391 |
+
"sizes": "72x72",
|
392 |
+
"type": "image/png",
|
393 |
+
"purpose": "any maskable"
|
394 |
+
},
|
395 |
+
{
|
396 |
+
"src": "/static/icons/icon-96x96.png",
|
397 |
+
"sizes": "96x96",
|
398 |
+
"type": "image/png",
|
399 |
+
"purpose": "any maskable"
|
400 |
+
},
|
401 |
+
{
|
402 |
+
"src": "/static/icons/icon-128x128.png",
|
403 |
+
"sizes": "128x128",
|
404 |
+
"type": "image/png",
|
405 |
+
"purpose": "any maskable"
|
406 |
+
},
|
407 |
+
{
|
408 |
+
"src": "/static/icons/icon-144x144.png",
|
409 |
+
"sizes": "144x144",
|
410 |
+
"type": "image/png",
|
411 |
+
"purpose": "any maskable"
|
412 |
+
},
|
413 |
+
{
|
414 |
+
"src": "/static/icons/icon-152x152.png",
|
415 |
+
"sizes": "152x152",
|
416 |
+
"type": "image/png",
|
417 |
+
"purpose": "any maskable"
|
418 |
+
},
|
419 |
+
{
|
420 |
+
"src": "/static/icons/icon-192x192.png",
|
421 |
+
"sizes": "192x192",
|
422 |
+
"type": "image/png",
|
423 |
+
"purpose": "any maskable"
|
424 |
+
},
|
425 |
+
{
|
426 |
+
"src": "/static/icons/icon-384x384.png",
|
427 |
+
"sizes": "384x384",
|
428 |
+
"type": "image/png",
|
429 |
+
"purpose": "any maskable"
|
430 |
+
},
|
431 |
+
{
|
432 |
+
"src": "/static/icons/icon-512x512.png",
|
433 |
+
"sizes": "512x512",
|
434 |
+
"type": "image/png",
|
435 |
+
"purpose": "any maskable"
|
436 |
+
}
|
437 |
+
]
|
438 |
+
}
|
439 |
+
with open(manifest_path, 'w', encoding='utf-8') as f:
|
440 |
+
json.dump(manifest_data, f, ensure_ascii=False, indent=2)
|
441 |
+
|
442 |
+
# service-worker.js 생성
|
443 |
+
sw_path = 'static/service-worker.js'
|
444 |
+
if not os.path.exists(sw_path):
|
445 |
+
with open(sw_path, 'w', encoding='utf-8') as f:
|
446 |
+
f.write('''
|
447 |
+
// 캐시 이름 설정
|
448 |
+
const CACHE_NAME = 'digital-gutpan-v1';
|
449 |
+
|
450 |
+
// 캐시할 파일 목록
|
451 |
+
const urlsToCache = [
|
452 |
+
'/',
|
453 |
+
'/static/icons/icon-72x72.png',
|
454 |
+
'/static/icons/icon-96x96.png',
|
455 |
+
'/static/icons/icon-128x128.png',
|
456 |
+
'/static/icons/icon-144x144.png',
|
457 |
+
'/static/icons/icon-152x152.png',
|
458 |
+
'/static/icons/icon-192x192.png',
|
459 |
+
'/static/icons/icon-384x384.png',
|
460 |
+
'/static/icons/icon-512x512.png',
|
461 |
+
'/assets/main_music.mp3'
|
462 |
+
];
|
463 |
+
|
464 |
+
// 서비스 워커 설치 시
|
465 |
+
self.addEventListener('install', event => {
|
466 |
+
event.waitUntil(
|
467 |
+
caches.open(CACHE_NAME)
|
468 |
+
.then(cache => cache.addAll(urlsToCache))
|
469 |
+
.then(() => self.skipWaiting())
|
470 |
+
);
|
471 |
+
});
|
472 |
+
|
473 |
+
// 서비스 워커 활성화 시
|
474 |
+
self.addEventListener('activate', event => {
|
475 |
+
event.waitUntil(
|
476 |
+
caches.keys().then(cacheNames => {
|
477 |
+
return Promise.all(
|
478 |
+
cacheNames.map(cacheName => {
|
479 |
+
if (cacheName !== CACHE_NAME) {
|
480 |
+
return caches.delete(cacheName);
|
481 |
+
}
|
482 |
+
})
|
483 |
+
);
|
484 |
+
}).then(() => self.clients.claim())
|
485 |
+
);
|
486 |
+
});
|
487 |
+
|
488 |
+
// 네트워크 요청 처리
|
489 |
+
self.addEventListener('fetch', event => {
|
490 |
+
event.respondWith(
|
491 |
+
caches.match(event.request)
|
492 |
+
.then(response => {
|
493 |
+
if (response) {
|
494 |
+
return response;
|
495 |
+
}
|
496 |
+
return fetch(event.request);
|
497 |
+
})
|
498 |
+
);
|
499 |
+
});
|
500 |
+
'''.strip())
|
501 |
+
|
502 |
+
# index.html 파일에 화면 꺼짐 방지 스크립트 추가
|
503 |
+
index_path = 'templates/index.html'
|
504 |
+
if not os.path.exists(index_path):
|
505 |
+
with open(index_path, 'w', encoding='utf-8') as f:
|
506 |
+
f.write('''<!DOCTYPE html>
|
507 |
+
<html lang="ko">
|
508 |
+
<head>
|
509 |
+
<meta charset="UTF-8">
|
510 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
511 |
+
<title>디지털 굿판</title>
|
512 |
+
<link rel="manifest" href="/manifest.json">
|
513 |
+
<meta name="theme-color" content="#000000">
|
514 |
+
<meta name="apple-mobile-web-app-capable" content="yes">
|
515 |
+
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
516 |
+
<meta name="apple-mobile-web-app-title" content="디지털 굿판">
|
517 |
+
<link rel="apple-touch-icon" href="/static/icons/icon-152x152.png">
|
518 |
+
<script>
|
519 |
+
// 화면 꺼짐 방지
|
520 |
+
async function preventSleep() {
|
521 |
+
try {
|
522 |
+
if ('wakeLock' in navigator) {
|
523 |
+
const wakeLock = await navigator.wakeLock.request('screen');
|
524 |
+
console.log('화면 켜짐 유지 활성화');
|
525 |
+
|
526 |
+
document.addEventListener('visibilitychange', async () => {
|
527 |
+
if (document.visibilityState === 'visible') {
|
528 |
+
await preventSleep();
|
529 |
+
}
|
530 |
+
});
|
531 |
+
}
|
532 |
+
} catch (err) {
|
533 |
+
console.log('화면 켜짐 유지 실패:', err);
|
534 |
+
}
|
535 |
+
}
|
536 |
+
|
537 |
+
// 서비스 워커 등록
|
538 |
+
if ('serviceWorker' in navigator) {
|
539 |
+
window.addEventListener('load', async () => {
|
540 |
+
try {
|
541 |
+
const registration = await navigator.serviceWorker.register('/service-worker.js');
|
542 |
+
console.log('ServiceWorker 등록 성공:', registration.scope);
|
543 |
+
await preventSleep();
|
544 |
+
} catch (err) {
|
545 |
+
console.log('ServiceWorker 등록 실패:', err);
|
546 |
+
}
|
547 |
+
});
|
548 |
+
}
|
549 |
+
</script>
|
550 |
+
</head>
|
551 |
+
<body>
|
552 |
+
<div id="gradio-app"></div>
|
553 |
+
</body>
|
554 |
+
</html>''')
|
555 |
|
556 |
def safe_state_update(state, updates):
|
557 |
try:
|