l-g-t commited on
Commit
ce8a899
·
verified ·
1 Parent(s): 97d2d8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -29
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import os
2
- import subprocess
3
  from flask import Flask
4
- import threading
5
- import sys
6
 
7
  # 初始化 Flask
8
  app = Flask(__name__)
@@ -11,32 +9,8 @@ app = Flask(__name__)
11
  def home():
12
  return "Gemini Telegram Bot is Running!"
13
 
14
- def run_bot():
15
- print("准备启动 Bot...")
16
-
17
- # 1. 尝试获取环境变量
18
- tg_token = os.environ.get("TELEGRAM_BOT_API_KEY")
19
- gemini_key = os.environ.get("GEMINI_API_KEYS")
20
-
21
- if not tg_token or not gemini_key:
22
- print("错误:未检测到环境变量!请检查 Space 的 Settings -> Variables and secrets")
23
- return
24
-
25
- # 2. 构造启动命令: python main.py <TG_TOKEN> <GEMINI_KEY>
26
- cmd = ["python", "main.py", tg_token, gemini_key]
27
-
28
- print(f"正在启动 main.py (Key长度检查: TG={len(tg_token)}, Gemini={len(gemini_key)})...")
29
-
30
- # 3. 运行,此时 main.py 和 app.py 在同一目录 /app/ 下
31
- # 使用 Popen 可以将 Bot 的内部日志输出到 HF 的日志中
32
- process = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, text=True)
33
- process.wait()
34
-
35
  if __name__ == "__main__":
36
- # 启动 Bot 线程
37
- bot_thread = threading.Thread(target=run_bot)
38
- bot_thread.start()
39
-
40
  # 启动 Web 服务器监听 7860
41
- print("启动 Web 服务器 (Port 7860)...")
42
  app.run(host='0.0.0.0', port=7860)
 
1
  import os
 
2
  from flask import Flask
3
+ # 移除了 subprocess 和 threading 模块
 
4
 
5
  # 初始化 Flask
6
  app = Flask(__name__)
 
9
  def home():
10
  return "Gemini Telegram Bot is Running!"
11
 
12
+ # 主函数:只运行 Flask 服务器
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  if __name__ == "__main__":
14
+ print("等待 Bot 线程启动...")
 
 
 
15
  # 启动 Web 服务器监听 7860
 
16
  app.run(host='0.0.0.0', port=7860)