fix_app.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import re
  2. import os
  3. file_path = "app.py"
  4. with open(file_path, "r", encoding="utf-8", errors="replace") as f:
  5. content = f.read()
  6. # Replace encoding issues
  7. content = content.replace("Lange Franois", "Francois Lange")
  8. content = content.replace("Lange FranA ois", "Francois Lange")
  9. # 1. Update get_db_connection
  10. old_db_conn = """def get_db_connection(login_path):
  11. try:
  12. import os
  13. db_host = os.environ.get('DB_HOST')
  14. # Check if environment variables exist for this login path
  15. db_user = os.environ.get(f'{login_path.upper()}_USER') or os.environ.get('DB_USER')
  16. db_pass = os.environ.get(f'{login_path.upper()}_PASS') or os.environ.get('DB_PASS')
  17. if db_host and db_user and db_pass:
  18. return pymysql.connect(
  19. host=db_host,
  20. user=db_user,
  21. password=db_pass,
  22. database='food_db',
  23. cursorclass=pymysql.cursors.DictCursor
  24. )
  25. conf = myloginpath.parse(login_path)
  26. if not conf or not conf.get('user'):
  27. st.error(f"⚠️ MySQL configuration missing for `{login_path}`. If you are testing locally on Windows, this app must be run on the Ubuntu server where `mysql_config_editor` is properly configured.")
  28. return None
  29. return pymysql.connect(
  30. host=conf.get('host', '127.0.0.1'),
  31. user=conf.get('user'),
  32. password=conf.get('password'),
  33. database='food_db',
  34. cursorclass=pymysql.cursors.DictCursor
  35. )
  36. except Exception as e:
  37. st.error(f"Connection Failed: {e}")
  38. return None"""
  39. new_db_conn = """def get_db_connection(login_path):
  40. try:
  41. import os
  42. db_host = os.environ.get('DB_HOST')
  43. # Check if environment variables exist for this login path
  44. db_user = os.environ.get(f'{login_path.upper()}_USER') or os.environ.get('DB_USER')
  45. db_pass = os.environ.get(f'{login_path.upper()}_PASS') or os.environ.get('DB_PASS')
  46. if db_host and db_user and db_pass:
  47. return pymysql.connect(
  48. host=db_host,
  49. user=db_user,
  50. password=db_pass,
  51. database='food_db',
  52. cursorclass=pymysql.cursors.DictCursor
  53. )
  54. conf = myloginpath.parse(login_path)
  55. if not conf or not conf.get('user'):
  56. st.error(f"⚠️ MySQL configuration missing for `{login_path}`. If you are testing locally on Windows, this app must be run on the Ubuntu server where `mysql_config_editor` is properly configured.")
  57. notifier.send_alert(f"MySQL Config Missing: {login_path}")
  58. return None
  59. return pymysql.connect(
  60. host=conf.get('host', '127.0.0.1'),
  61. user=conf.get('user'),
  62. password=conf.get('password'),
  63. database='food_db',
  64. cursorclass=pymysql.cursors.DictCursor
  65. )
  66. except Exception as e:
  67. st.error(f"Connection Failed: {e}")
  68. notifier.send_alert(f"Database Connection Failed ({login_path}): {e}")
  69. return None"""
  70. content = content.replace(old_db_conn, new_db_conn)
  71. # 2. Update db_cursor
  72. old_db_cursor = """@contextmanager
  73. def db_cursor(login_path: str):
  74. conn = get_db_connection(login_path)
  75. if not conn:
  76. yield None
  77. return
  78. try:
  79. with conn.cursor() as cursor:
  80. yield cursor
  81. conn.commit()
  82. except Exception as e:
  83. conn.rollback()
  84. st.error(f"Database query error: {e}")
  85. raise e
  86. finally:
  87. conn.close()"""
  88. new_db_cursor = """@contextmanager
  89. def db_cursor(login_path: str):
  90. conn = get_db_connection(login_path)
  91. if not conn:
  92. yield None
  93. return
  94. try:
  95. with conn.cursor() as cursor:
  96. yield cursor
  97. conn.commit()
  98. except Exception as e:
  99. conn.rollback()
  100. st.error(f"Database query error: {e}")
  101. notifier.send_alert(f"Database Query Error ({login_path}): {e}")
  102. raise e
  103. finally:
  104. conn.close()"""
  105. content = content.replace(old_db_cursor, new_db_cursor)
  106. # 3. Update query_plate_allergens exception
  107. content = content.replace(
  108. " except Exception:\n pass\n return table_data",
  109. " except Exception as e:\n notifier.send_alert(f\"LLM query_plate_allergens error: {e}\")\n pass\n return table_data"
  110. )
  111. # 4. Update detect_allergens_from_text exception
  112. content = content.replace(
  113. " except Exception:\n pass\n return detected",
  114. " except Exception as e:\n notifier.send_alert(f\"LLM detect_allergens_from_text error: {e}\")\n pass\n return detected"
  115. )
  116. with open(file_path, "w", encoding="utf-8") as f:
  117. f.write(content)
  118. print("app.py successfully updated and encoding sanitized.")