Spaces:
Sleeping
Sleeping
File size: 6,636 Bytes
33d3592 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | # force_reset_models.py - Force reset all AI models and cache
import os
import sys
import shutil
import glob
from dotenv import load_dotenv
def force_clear_all_cache():
"""Force clear all Python cache and compiled files"""
print("π§Ή Force clearing all cache...")
try:
# Remove all __pycache__ directories
pycache_dirs = glob.glob("**/__pycache__", recursive=True)
for cache_dir in pycache_dirs:
shutil.rmtree(cache_dir, ignore_errors=True)
print(f"ποΈ Removed {cache_dir}")
# Remove all .pyc files
pyc_files = glob.glob("**/*.pyc", recursive=True)
for pyc_file in pyc_files:
try:
os.remove(pyc_file)
print(f"ποΈ Removed {pyc_file}")
except:
pass
# Remove .streamlit cache if exists
streamlit_cache = ".streamlit"
if os.path.exists(streamlit_cache):
shutil.rmtree(streamlit_cache, ignore_errors=True)
print(f"ποΈ Removed {streamlit_cache}")
print("β
All cache cleared")
return True
except Exception as e:
print(f"β Error clearing cache: {str(e)}")
return False
def force_reload_environment():
"""Force reload environment variables"""
print("π Force reloading environment...")
try:
# Clear environment variables
env_vars_to_clear = [
'OPENROUTER_MODEL',
'OPENROUTER_API_KEY',
'GROQ_API_KEY',
'GEMINI_API_KEY'
]
for var in env_vars_to_clear:
if var in os.environ:
del os.environ[var]
print(f"ποΈ Cleared {var} from environment")
# Reload from .env file
load_dotenv(override=True)
# Verify reload
for var in env_vars_to_clear:
value = os.getenv(var)
if value:
if 'KEY' in var:
print(f"β
Reloaded {var}: {value[:20]}...{value[-10:]}")
else:
print(f"β
Reloaded {var}: {value}")
else:
print(f"β οΈ {var}: Not found")
return True
except Exception as e:
print(f"β Error reloading environment: {str(e)}")
return False
def force_reimport_modules():
"""Force reimport all AI-related modules"""
print("π Force reimporting modules...")
try:
# Modules to reimport
modules_to_reload = [
'translator',
'ai_questions',
'exporter'
]
for module_name in modules_to_reload:
if module_name in sys.modules:
del sys.modules[module_name]
print(f"ποΈ Removed {module_name} from sys.modules")
# Force reimport
import importlib
try:
import translator
importlib.reload(translator)
print("β
Reloaded translator module")
except Exception as e:
print(f"β οΈ Could not reload translator: {str(e)}")
try:
import ai_questions
importlib.reload(ai_questions)
print("β
Reloaded ai_questions module")
except Exception as e:
print(f"β οΈ Could not reload ai_questions: {str(e)}")
return True
except Exception as e:
print(f"β Error reimporting modules: {str(e)}")
return False
def test_after_reset():
"""Test AI models after reset"""
print("π§ͺ Testing after reset...")
try:
from translator import get_translator
translator = get_translator()
print(f"π OpenRouter Model: {translator.openrouter_model}")
# Test OpenRouter
result, error = translator._openrouter_complete("Test message")
if result:
print("β
OpenRouter working after reset!")
return True
else:
print(f"β OpenRouter still failing: {error}")
return False
except Exception as e:
print(f"β Test failed: {str(e)}")
return False
def create_fresh_env_file():
"""Create a fresh .env file with correct values"""
print("π Creating fresh .env file...")
env_content = """GEMINI_API_KEY=AIzaSyAS7JtrXjlNjyuo3RG5z6rkwocCwFy1YuA
GROQ_API_KEY=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
OPENROUTER_API_KEY=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
OPENROUTER_MODEL=meta-llama/llama-3.2-3b-instruct:free
GROQ_WHISPER_MODEL=whisper-large-v3
# Google Docs API Configuration
# Get these from: https://console.cloud.google.com/
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
"""
try:
with open('.env', 'w', encoding='utf-8') as f:
f.write(env_content)
print("β
Fresh .env file created")
return True
except Exception as e:
print(f"β Error creating .env file: {str(e)}")
return False
def main():
"""Main force reset function"""
print("π Force Reset AI Models")
print("=" * 60)
# Step 1: Create fresh .env file
env_success = create_fresh_env_file()
# Step 2: Clear all cache
cache_success = force_clear_all_cache()
# Step 3: Reload environment
env_reload_success = force_reload_environment()
# Step 4: Reimport modules
import_success = force_reimport_modules()
# Step 5: Test after reset
test_success = test_after_reset()
print("\n" + "=" * 60)
print("π Force Reset Results:")
print(f" Fresh .env: {'β
CREATED' if env_success else 'β FAILED'}")
print(f" Cache Clear: {'β
CLEARED' if cache_success else 'β FAILED'}")
print(f" Env Reload: {'β
RELOADED' if env_reload_success else 'β FAILED'}")
print(f" Module Import: {'β
IMPORTED' if import_success else 'β FAILED'}")
print(f" Final Test: {'β
WORKING' if test_success else 'β FAILED'}")
if all([env_success, cache_success, env_reload_success, test_success]):
print("\nπ Force reset successful! AI models should work now.")
print("π‘ Now restart your Streamlit app: streamlit run app.py")
else:
print("\nβ οΈ Some issues remain. Check the logs above.")
return test_success
if __name__ == "__main__":
main() |