Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,8 +41,10 @@ visitor_data_cache = {}
|
|
| 41 |
def download_data_from_hf():
|
| 42 |
global visitor_data_cache
|
| 43 |
if not HF_TOKEN_READ:
|
|
|
|
| 44 |
return False
|
| 45 |
try:
|
|
|
|
| 46 |
hf_hub_download(
|
| 47 |
repo_id=REPO_ID,
|
| 48 |
filename=HF_DATA_FILE_PATH,
|
|
@@ -53,17 +55,20 @@ def download_data_from_hf():
|
|
| 53 |
force_download=True,
|
| 54 |
etag_timeout=10
|
| 55 |
)
|
|
|
|
| 56 |
with _data_lock:
|
| 57 |
try:
|
| 58 |
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
| 59 |
visitor_data_cache = json.load(f)
|
|
|
|
| 60 |
except (FileNotFoundError, json.JSONDecodeError) as e:
|
|
|
|
| 61 |
visitor_data_cache = {}
|
| 62 |
return True
|
| 63 |
except RepositoryNotFoundError:
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
return False
|
| 68 |
|
| 69 |
def load_visitor_data():
|
|
@@ -73,11 +78,15 @@ def load_visitor_data():
|
|
| 73 |
try:
|
| 74 |
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
| 75 |
visitor_data_cache = json.load(f)
|
|
|
|
| 76 |
except FileNotFoundError:
|
|
|
|
| 77 |
visitor_data_cache = {}
|
| 78 |
except json.JSONDecodeError:
|
|
|
|
| 79 |
visitor_data_cache = {}
|
| 80 |
except Exception as e:
|
|
|
|
| 81 |
visitor_data_cache = {}
|
| 82 |
return visitor_data_cache
|
| 83 |
|
|
@@ -87,14 +96,17 @@ def save_visitor_data(data):
|
|
| 87 |
visitor_data_cache.update(data)
|
| 88 |
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
| 89 |
json.dump(visitor_data_cache, f, ensure_ascii=False, indent=4)
|
|
|
|
| 90 |
upload_data_to_hf_async()
|
| 91 |
except Exception as e:
|
| 92 |
-
|
| 93 |
|
| 94 |
def upload_data_to_hf():
|
| 95 |
if not HF_TOKEN_WRITE:
|
|
|
|
| 96 |
return
|
| 97 |
if not os.path.exists(DATA_FILE):
|
|
|
|
| 98 |
return
|
| 99 |
|
| 100 |
try:
|
|
@@ -102,8 +114,10 @@ def upload_data_to_hf():
|
|
| 102 |
with _data_lock:
|
| 103 |
file_content_exists = os.path.getsize(DATA_FILE) > 0
|
| 104 |
if not file_content_exists:
|
|
|
|
| 105 |
return
|
| 106 |
|
|
|
|
| 107 |
api.upload_file(
|
| 108 |
path_or_fileobj=DATA_FILE,
|
| 109 |
path_in_repo=HF_DATA_FILE_PATH,
|
|
@@ -112,8 +126,9 @@ def upload_data_to_hf():
|
|
| 112 |
token=HF_TOKEN_WRITE,
|
| 113 |
commit_message=f"Update visitor data {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
| 114 |
)
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
-
|
| 117 |
|
| 118 |
def upload_data_to_hf_async():
|
| 119 |
upload_thread = threading.Thread(target=upload_data_to_hf, daemon=True)
|
|
@@ -121,9 +136,11 @@ def upload_data_to_hf_async():
|
|
| 121 |
|
| 122 |
def periodic_backup():
|
| 123 |
if not HF_TOKEN_WRITE:
|
|
|
|
| 124 |
return
|
| 125 |
while True:
|
| 126 |
time.sleep(3600)
|
|
|
|
| 127 |
upload_data_to_hf()
|
| 128 |
|
| 129 |
def verify_telegram_data(init_data_str):
|
|
@@ -146,12 +163,13 @@ def verify_telegram_data(init_data_str):
|
|
| 146 |
auth_date = int(parsed_data.get('auth_date', [0])[0])
|
| 147 |
current_time = int(time.time())
|
| 148 |
if current_time - auth_date > 86400:
|
| 149 |
-
|
| 150 |
return parsed_data, True
|
| 151 |
else:
|
| 152 |
-
|
| 153 |
return parsed_data, False
|
| 154 |
except Exception as e:
|
|
|
|
| 155 |
return None, False
|
| 156 |
|
| 157 |
TEMPLATE = """
|
|
@@ -626,7 +644,7 @@ TEMPLATE = """
|
|
| 626 |
|
| 627 |
const modal = document.getElementById("saveModal");
|
| 628 |
const saveCardBtn = document.getElementById("save-card-btn");
|
| 629 |
-
const closeBtn = document
|
| 630 |
|
| 631 |
if (saveCardBtn && modal && closeBtn) {
|
| 632 |
saveCardBtn.addEventListener('click', (e) => {
|
|
@@ -884,6 +902,7 @@ dp = Dispatcher()
|
|
| 884 |
|
| 885 |
def generate_bot_response(query: str, context: str):
|
| 886 |
if not GEMINI_API_KEY:
|
|
|
|
| 887 |
return "Извините, функция ответа с использованием AI временно недоступна."
|
| 888 |
|
| 889 |
lower_query = query.lower()
|
|
@@ -928,21 +947,35 @@ ASSISTANT RESPONSE:"""
|
|
| 928 |
return response.text.strip()
|
| 929 |
|
| 930 |
except Exception as e:
|
|
|
|
| 931 |
return "Извините, произошла ошибка при обработке вашего запроса."
|
| 932 |
|
| 933 |
@dp.message()
|
| 934 |
async def handle_message(message: types.Message):
|
|
|
|
| 935 |
user_query = message.text
|
| 936 |
if not user_query:
|
|
|
|
| 937 |
return
|
| 938 |
|
| 939 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 940 |
|
| 941 |
-
|
|
|
|
| 942 |
|
| 943 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 944 |
|
| 945 |
-
await message.answer(response_text)
|
| 946 |
|
| 947 |
@app.route('/')
|
| 948 |
def index():
|
|
@@ -965,6 +998,7 @@ def verify_data():
|
|
| 965 |
user_json_str = unquote(user_data_parsed['user'][0])
|
| 966 |
user_info_dict = json.loads(user_json_str)
|
| 967 |
except Exception as e:
|
|
|
|
| 968 |
user_info_dict = {}
|
| 969 |
|
| 970 |
if is_valid:
|
|
@@ -988,9 +1022,11 @@ def verify_data():
|
|
| 988 |
save_visitor_data(user_entry)
|
| 989 |
return jsonify({"status": "ok", "verified": True, "user": user_info_dict}), 200
|
| 990 |
else:
|
|
|
|
| 991 |
return jsonify({"status": "error", "verified": False, "message": "Invalid data"}), 403
|
| 992 |
|
| 993 |
except Exception as e:
|
|
|
|
| 994 |
return jsonify({"status": "error", "message": "Internal server error"}), 500
|
| 995 |
|
| 996 |
@app.route('/admin')
|
|
@@ -1015,20 +1051,59 @@ def admin_trigger_upload():
|
|
| 1015 |
return jsonify({"status": "ok", "message": "Загрузка данных на Hugging Face запущена в фоновом режиме."})
|
| 1016 |
|
| 1017 |
def start_bot_polling():
|
|
|
|
| 1018 |
try:
|
| 1019 |
asyncio.run(dp.start_polling(bot))
|
|
|
|
| 1020 |
except Exception as e:
|
| 1021 |
-
|
| 1022 |
|
| 1023 |
if __name__ == '__main__':
|
| 1024 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1025 |
load_visitor_data()
|
| 1026 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1027 |
if HF_TOKEN_WRITE:
|
| 1028 |
backup_thread = threading.Thread(target=periodic_backup, daemon=True)
|
| 1029 |
backup_thread.start()
|
|
|
|
|
|
|
|
|
|
| 1030 |
|
| 1031 |
bot_thread = threading.Thread(target=start_bot_polling, daemon=True)
|
| 1032 |
bot_thread.start()
|
|
|
|
| 1033 |
|
|
|
|
| 1034 |
app.run(host=HOST, port=PORT, debug=False)
|
|
|
|
| 41 |
def download_data_from_hf():
|
| 42 |
global visitor_data_cache
|
| 43 |
if not HF_TOKEN_READ:
|
| 44 |
+
logging.warning("HF_TOKEN_READ not set. Skipping Hugging Face download.")
|
| 45 |
return False
|
| 46 |
try:
|
| 47 |
+
logging.info(f"Attempting to download {HF_DATA_FILE_PATH} from {REPO_ID}...")
|
| 48 |
hf_hub_download(
|
| 49 |
repo_id=REPO_ID,
|
| 50 |
filename=HF_DATA_FILE_PATH,
|
|
|
|
| 55 |
force_download=True,
|
| 56 |
etag_timeout=10
|
| 57 |
)
|
| 58 |
+
logging.info("Data file successfully downloaded from Hugging Face.")
|
| 59 |
with _data_lock:
|
| 60 |
try:
|
| 61 |
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
| 62 |
visitor_data_cache = json.load(f)
|
| 63 |
+
logging.info("Successfully loaded downloaded data into cache.")
|
| 64 |
except (FileNotFoundError, json.JSONDecodeError) as e:
|
| 65 |
+
logging.error(f"Error reading downloaded data file: {e}. Starting with empty cache.")
|
| 66 |
visitor_data_cache = {}
|
| 67 |
return True
|
| 68 |
except RepositoryNotFoundError:
|
| 69 |
+
logging.error(f"Hugging Face repository '{REPO_ID}' not found. Cannot download data.")
|
| 70 |
except Exception as e:
|
| 71 |
+
logging.error(f"Error downloading data from Hugging Face: {e}")
|
| 72 |
return False
|
| 73 |
|
| 74 |
def load_visitor_data():
|
|
|
|
| 78 |
try:
|
| 79 |
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
| 80 |
visitor_data_cache = json.load(f)
|
| 81 |
+
logging.info("Visitor data loaded from local JSON.")
|
| 82 |
except FileNotFoundError:
|
| 83 |
+
logging.warning(f"{DATA_FILE} not found locally. Starting with empty data.")
|
| 84 |
visitor_data_cache = {}
|
| 85 |
except json.JSONDecodeError:
|
| 86 |
+
logging.error(f"Error decoding {DATA_FILE}. Starting with empty data.")
|
| 87 |
visitor_data_cache = {}
|
| 88 |
except Exception as e:
|
| 89 |
+
logging.error(f"Unexpected error loading visitor data: {e}")
|
| 90 |
visitor_data_cache = {}
|
| 91 |
return visitor_data_cache
|
| 92 |
|
|
|
|
| 96 |
visitor_data_cache.update(data)
|
| 97 |
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
| 98 |
json.dump(visitor_data_cache, f, ensure_ascii=False, indent=4)
|
| 99 |
+
logging.info(f"Visitor data successfully saved to {DATA_FILE}.")
|
| 100 |
upload_data_to_hf_async()
|
| 101 |
except Exception as e:
|
| 102 |
+
logging.error(f"Error saving visitor data: {e}")
|
| 103 |
|
| 104 |
def upload_data_to_hf():
|
| 105 |
if not HF_TOKEN_WRITE:
|
| 106 |
+
logging.warning("HF_TOKEN_WRITE not set. Skipping Hugging Face upload.")
|
| 107 |
return
|
| 108 |
if not os.path.exists(DATA_FILE):
|
| 109 |
+
logging.warning(f"{DATA_FILE} does not exist. Skipping upload.")
|
| 110 |
return
|
| 111 |
|
| 112 |
try:
|
|
|
|
| 114 |
with _data_lock:
|
| 115 |
file_content_exists = os.path.getsize(DATA_FILE) > 0
|
| 116 |
if not file_content_exists:
|
| 117 |
+
logging.warning(f"{DATA_FILE} is empty. Skipping upload.")
|
| 118 |
return
|
| 119 |
|
| 120 |
+
logging.info(f"Attempting to upload {DATA_FILE} to {REPO_ID}/{HF_DATA_FILE_PATH}...")
|
| 121 |
api.upload_file(
|
| 122 |
path_or_fileobj=DATA_FILE,
|
| 123 |
path_in_repo=HF_DATA_FILE_PATH,
|
|
|
|
| 126 |
token=HF_TOKEN_WRITE,
|
| 127 |
commit_message=f"Update visitor data {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
| 128 |
)
|
| 129 |
+
logging.info("Visitor data successfully uploaded to Hugging Face.")
|
| 130 |
except Exception as e:
|
| 131 |
+
logging.error(f"Error uploading data to Hugging Face: {e}")
|
| 132 |
|
| 133 |
def upload_data_to_hf_async():
|
| 134 |
upload_thread = threading.Thread(target=upload_data_to_hf, daemon=True)
|
|
|
|
| 136 |
|
| 137 |
def periodic_backup():
|
| 138 |
if not HF_TOKEN_WRITE:
|
| 139 |
+
logging.info("Periodic backup disabled: HF_TOKEN_WRITE not set.")
|
| 140 |
return
|
| 141 |
while True:
|
| 142 |
time.sleep(3600)
|
| 143 |
+
logging.info("Initiating periodic backup...")
|
| 144 |
upload_data_to_hf()
|
| 145 |
|
| 146 |
def verify_telegram_data(init_data_str):
|
|
|
|
| 163 |
auth_date = int(parsed_data.get('auth_date', [0])[0])
|
| 164 |
current_time = int(time.time())
|
| 165 |
if current_time - auth_date > 86400:
|
| 166 |
+
logging.warning(f"Telegram InitData is older than 1 hour (Auth Date: {auth_date}, Current: {current_time}).")
|
| 167 |
return parsed_data, True
|
| 168 |
else:
|
| 169 |
+
logging.warning(f"Data verification failed. Calculated: {calculated_hash}, Received: {received_hash}")
|
| 170 |
return parsed_data, False
|
| 171 |
except Exception as e:
|
| 172 |
+
logging.error(f"Error verifying Telegram data: {e}")
|
| 173 |
return None, False
|
| 174 |
|
| 175 |
TEMPLATE = """
|
|
|
|
| 644 |
|
| 645 |
const modal = document.getElementById("saveModal");
|
| 646 |
const saveCardBtn = document.getElementById("save-card-btn");
|
| 647 |
+
const closeBtn = document(Id("modal-close-btn");
|
| 648 |
|
| 649 |
if (saveCardBtn && modal && closeBtn) {
|
| 650 |
saveCardBtn.addEventListener('click', (e) => {
|
|
|
|
| 902 |
|
| 903 |
def generate_bot_response(query: str, context: str):
|
| 904 |
if not GEMINI_API_KEY:
|
| 905 |
+
logging.warning("GEMINI_API_KEY not set. Cannot use AI.")
|
| 906 |
return "Извините, функция ответа с использованием AI временно недоступна."
|
| 907 |
|
| 908 |
lower_query = query.lower()
|
|
|
|
| 947 |
return response.text.strip()
|
| 948 |
|
| 949 |
except Exception as e:
|
| 950 |
+
logging.error(f"Error generating AI response: {e}")
|
| 951 |
return "Извините, произошла ошибка при обработке вашего запроса."
|
| 952 |
|
| 953 |
@dp.message()
|
| 954 |
async def handle_message(message: types.Message):
|
| 955 |
+
logging.info(f"Received message from user {message.from_user.id}: {message.text}")
|
| 956 |
user_query = message.text
|
| 957 |
if not user_query:
|
| 958 |
+
logging.warning("Received empty message.")
|
| 959 |
return
|
| 960 |
|
| 961 |
+
try:
|
| 962 |
+
await message.answer("Думаю...")
|
| 963 |
+
logging.info("Sent 'Thinking...' message.")
|
| 964 |
+
|
| 965 |
+
context = TEMPLATE
|
| 966 |
+
|
| 967 |
+
response_text = generate_bot_response(user_query, context)
|
| 968 |
|
| 969 |
+
await message.answer(response_text)
|
| 970 |
+
logging.info(f"Sent AI response to user {message.from_user.id}")
|
| 971 |
|
| 972 |
+
except Exception as e:
|
| 973 |
+
logging.exception(f"Error handling message from user {message.from_user.id}")
|
| 974 |
+
try:
|
| 975 |
+
await message.answer("Извините, произошла внутренняя ошибка при обработке вашего сообщения.")
|
| 976 |
+
except Exception:
|
| 977 |
+
logging.error("Failed to send error message to user.")
|
| 978 |
|
|
|
|
| 979 |
|
| 980 |
@app.route('/')
|
| 981 |
def index():
|
|
|
|
| 998 |
user_json_str = unquote(user_data_parsed['user'][0])
|
| 999 |
user_info_dict = json.loads(user_json_str)
|
| 1000 |
except Exception as e:
|
| 1001 |
+
logging.error(f"Could not parse user JSON: {e}")
|
| 1002 |
user_info_dict = {}
|
| 1003 |
|
| 1004 |
if is_valid:
|
|
|
|
| 1022 |
save_visitor_data(user_entry)
|
| 1023 |
return jsonify({"status": "ok", "verified": True, "user": user_info_dict}), 200
|
| 1024 |
else:
|
| 1025 |
+
logging.warning(f"Verification failed for user: {user_info_dict.get('id')}")
|
| 1026 |
return jsonify({"status": "error", "verified": False, "message": "Invalid data"}), 403
|
| 1027 |
|
| 1028 |
except Exception as e:
|
| 1029 |
+
logging.exception("Error in /verify endpoint")
|
| 1030 |
return jsonify({"status": "error", "message": "Internal server error"}), 500
|
| 1031 |
|
| 1032 |
@app.route('/admin')
|
|
|
|
| 1051 |
return jsonify({"status": "ok", "message": "Загрузка данных на Hugging Face запущена в фоновом режиме."})
|
| 1052 |
|
| 1053 |
def start_bot_polling():
|
| 1054 |
+
logging.info("Starting bot polling thread...")
|
| 1055 |
try:
|
| 1056 |
asyncio.run(dp.start_polling(bot))
|
| 1057 |
+
logging.info("Bot polling stopped.")
|
| 1058 |
except Exception as e:
|
| 1059 |
+
logging.exception("Bot polling failed")
|
| 1060 |
|
| 1061 |
if __name__ == '__main__':
|
| 1062 |
+
logging.info("---")
|
| 1063 |
+
logging.info("--- MORSHEN GROUP SERVER (FLASK + AIOGRAM) ---")
|
| 1064 |
+
logging.info("---")
|
| 1065 |
+
logging.info(f"Flask server starting on http://{HOST}:{PORT}")
|
| 1066 |
+
logging.info(f"Using Bot Token ID: {BOT_TOKEN.split(':')[0]}")
|
| 1067 |
+
logging.info(f"Visitor data file: {DATA_FILE}")
|
| 1068 |
+
logging.info(f"Hugging Face Repo: {REPO_ID}")
|
| 1069 |
+
logging.info(f"HF Data Path: {HF_DATA_FILE_PATH}")
|
| 1070 |
+
if not HF_TOKEN_READ or not HF_TOKEN_WRITE:
|
| 1071 |
+
logging.warning("---")
|
| 1072 |
+
logging.warning("--- WARNING: HUGGING FACE TOKEN(S) NOT SET ---")
|
| 1073 |
+
logging.warning("--- Backup/restore functionality will be limited. Set HF_TOKEN_READ and HF_TOKEN_WRITE environment variables.")
|
| 1074 |
+
logging.warning("---")
|
| 1075 |
+
else:
|
| 1076 |
+
logging.info("--- Hugging Face tokens found.")
|
| 1077 |
+
logging.info("--- Attempting initial data download from Hugging Face...")
|
| 1078 |
+
download_data_from_hf()
|
| 1079 |
+
|
| 1080 |
+
if not GEMINI_API_KEY:
|
| 1081 |
+
logging.warning("---")
|
| 1082 |
+
logging.warning("--- WARNING: GEMINI_API_KEY NOT SET ---")
|
| 1083 |
+
logging.warning("--- AI response functionality will be limited or unavailable. Set GEMINI_API_KEY environment variable.")
|
| 1084 |
+
logging.warning("---")
|
| 1085 |
+
else:
|
| 1086 |
+
logging.info("--- GEMINI_API_KEY found.")
|
| 1087 |
+
logging.info(f"--- Using AI model: {GENAI_MODEL}")
|
| 1088 |
+
|
| 1089 |
load_visitor_data()
|
| 1090 |
|
| 1091 |
+
logging.warning("---")
|
| 1092 |
+
logging.warning("--- SECURITY WARNING ---")
|
| 1093 |
+
logging.warning("--- The /admin route and its sub-routes are NOT protected.")
|
| 1094 |
+
logging.warning("--- Implement proper authentication before deploying.")
|
| 1095 |
+
logging.warning("---")
|
| 1096 |
+
|
| 1097 |
if HF_TOKEN_WRITE:
|
| 1098 |
backup_thread = threading.Thread(target=periodic_backup, daemon=True)
|
| 1099 |
backup_thread.start()
|
| 1100 |
+
logging.info("--- Periodic backup thread started (every hour).")
|
| 1101 |
+
else:
|
| 1102 |
+
logging.info("--- Periodic backup disabled (HF_TOKEN_WRITE missing).")
|
| 1103 |
|
| 1104 |
bot_thread = threading.Thread(target=start_bot_polling, daemon=True)
|
| 1105 |
bot_thread.start()
|
| 1106 |
+
logging.info("--- Telegram bot polling thread initiated.")
|
| 1107 |
|
| 1108 |
+
logging.info("--- Server Ready ---")
|
| 1109 |
app.run(host=HOST, port=PORT, debug=False)
|