web_ui.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import os
  2. from multiprocessing import Process
  3. import signal
  4. import time
  5. import gradio as gr
  6. from channel import channel_factory
  7. from common import const
  8. from config import load_config, conf
  9. from plugins import *
  10. current_process_instance = None
  11. def start_channel(channel_name: str):
  12. channel = channel_factory.create_channel(channel_name)
  13. available_channels = [
  14. "wx",
  15. "terminal",
  16. "wechatmp",
  17. "wechatmp_service",
  18. "wechatcom_app",
  19. "wework",
  20. "wechatcom_service",
  21. const.FEISHU,
  22. const.DINGTALK
  23. ]
  24. if channel_name in available_channels:
  25. PluginManager().load_plugins()
  26. channel.startup()
  27. def run():
  28. try:
  29. # load config
  30. load_config()
  31. # create channel
  32. channel_name = conf().get("channel_type", "wx")
  33. start_channel(channel_name)
  34. except Exception as e:
  35. logger.error("App startup failed!")
  36. logger.exception(e)
  37. def start_run():
  38. global current_process_instance
  39. if current_process_instance is not None and current_process_instance.is_alive():
  40. os.kill(current_process_instance.pid, signal.SIGTERM) # 杀掉当前进程
  41. current_process_instance.join() # 等待当前进程结束
  42. current_process_instance = Process(target=run)
  43. current_process_instance.start()
  44. time.sleep(10) # 等待进程启动
  45. return f"重启成功!!"
  46. def get_qrcode_image():
  47. image_path = 'wx_qrcode.png'
  48. if os.path.exists(image_path):
  49. return image_path
  50. else:
  51. return None
  52. def verify_login(username, password):
  53. correct_username = conf().get("web_ui_username", "dow")
  54. correct_password = conf().get("web_ui_password", "dify-on-wechat")
  55. if username == correct_username and password == correct_password:
  56. return True
  57. return False
  58. def login(username, password):
  59. if verify_login(username, password):
  60. return (
  61. gr.update(visible=False),
  62. gr.update(visible=True),
  63. gr.update(visible=True),
  64. gr.update(visible=True),
  65. gr.update(visible=True),
  66. gr.update(visible=False), # Hide username input
  67. gr.update(visible=False), # Hide password input
  68. gr.update(visible=False) # Hide login button
  69. )
  70. else:
  71. return (
  72. "用户名或密码错误",
  73. gr.update(visible=False),
  74. gr.update(visible=False),
  75. gr.update(visible=False),
  76. gr.update(visible=False),
  77. gr.update(visible=True), # Show username input
  78. gr.update(visible=True), # Show password input
  79. gr.update(visible=True) # Show login button
  80. )
  81. with gr.Blocks() as demo:
  82. username_input = gr.Textbox(label="用户名")
  83. password_input = gr.Textbox(label="密码", type="password")
  84. login_button = gr.Button("登录")
  85. login_status = gr.Textbox(label="登录状态", value="", interactive=False)
  86. qrcode_image = gr.Image(value=get_qrcode_image(), label="微信二维码", width=400, height=400, visible=False)
  87. restart_status = gr.Textbox(label="状态", value="启动成功", visible=False)
  88. with gr.Row():
  89. restart_button = gr.Button("异常退出后请点击此按钮重启", visible=False)
  90. refresh_button = gr.Button("登录前请点击此按钮刷新二维码", visible=False) # 添加手动刷新的按钮
  91. login_button.click(
  92. login,
  93. inputs=[username_input, password_input],
  94. outputs=[
  95. login_status,
  96. qrcode_image,
  97. restart_button,
  98. refresh_button,
  99. restart_status,
  100. username_input,
  101. password_input,
  102. login_button
  103. ]
  104. )
  105. restart_button.click(start_run, outputs=restart_status)
  106. def refresh_image():
  107. return get_qrcode_image()
  108. refresh_button.click(refresh_image, outputs=qrcode_image) # 手动刷新按钮的点击事件
  109. if __name__ == "__main__":
  110. start_run()
  111. load_config()
  112. demo.launch(server_name="0.0.0.0", server_port=conf().get("web_ui_port", 7860))