syncmaster8 / fix_model_issue.py
aseelflihan's picture
Initial commit without node_modules
33d3592
# fix_model_issue.py - Fix the OpenRouter model issue
import os
from dotenv import load_dotenv
def fix_openrouter_model():
"""Fix OpenRouter model configuration"""
print("πŸ”§ Fixing OpenRouter model configuration...")
# Reload environment variables
load_dotenv(override=True)
# Check current configuration
current_model = os.getenv("OPENROUTER_MODEL")
print(f"πŸ“‹ Current OPENROUTER_MODEL: {current_model}")
# Update .env file if needed
env_file = ".env"
try:
with open(env_file, 'r', encoding='utf-8') as f:
content = f.read()
# Replace old model with new one
old_models = [
"meta-llama-3-70b-instruct",
"meta-llama/llama-3-70b-instruct",
"meta-llama-3.1-70b-instruct"
]
new_model = "meta-llama/llama-3.2-3b-instruct:free"
updated = False
for old_model in old_models:
if old_model in content:
content = content.replace(f"OPENROUTER_MODEL={old_model}", f"OPENROUTER_MODEL={new_model}")
updated = True
print(f"βœ… Replaced {old_model} with {new_model}")
if updated:
with open(env_file, 'w', encoding='utf-8') as f:
f.write(content)
print("βœ… .env file updated successfully")
else:
print("ℹ️ No old models found in .env file")
# Reload environment variables again
load_dotenv(override=True)
# Verify the change
updated_model = os.getenv("OPENROUTER_MODEL")
print(f"βœ… Updated OPENROUTER_MODEL: {updated_model}")
return True
except Exception as e:
print(f"❌ Error updating .env file: {str(e)}")
return False
def test_openrouter_with_new_model():
"""Test OpenRouter with the new model"""
print("\nπŸ§ͺ Testing OpenRouter with new model...")
try:
from translator import get_translator
# Force reload translator
import importlib
import translator
importlib.reload(translator)
# Get fresh translator instance
translator_instance = get_translator()
print(f"πŸ“‹ Translator OpenRouter model: {translator_instance.openrouter_model}")
# Test simple translation
test_text = "Hello, this is a test."
try:
result, error = translator_instance._openrouter_complete(f"Translate to Arabic: {test_text}")
if result:
print(f"βœ… OpenRouter test successful!")
print(f"πŸ“ Result: {result[:100]}...")
return True
else:
print(f"❌ OpenRouter test failed: {error}")
return False
except Exception as e:
print(f"❌ OpenRouter test error: {str(e)}")
return False
except Exception as e:
print(f"❌ Error testing OpenRouter: {str(e)}")
return False
def clear_python_cache():
"""Clear Python cache files"""
print("\n🧹 Clearing Python cache...")
import shutil
import glob
try:
# Remove __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 .pyc files
pyc_files = glob.glob("**/*.pyc", recursive=True)
for pyc_file in pyc_files:
os.remove(pyc_file)
print(f"πŸ—‘οΈ Removed {pyc_file}")
print("βœ… Python cache cleared")
return True
except Exception as e:
print(f"❌ Error clearing cache: {str(e)}")
return False
def main():
"""Main function to fix the model issue"""
print("=" * 60)
print("πŸš€ OpenRouter Model Fix Tool")
print("=" * 60)
# Step 1: Fix model configuration
fix_success = fix_openrouter_model()
# Step 2: Clear Python cache
cache_success = clear_python_cache()
# Step 3: Test with new model
test_success = test_openrouter_with_new_model()
print("\n" + "=" * 60)
print("πŸ“Š Fix Results:")
print(f" Model Config: {'βœ… FIXED' if fix_success else '❌ FAILED'}")
print(f" Cache Clear: {'βœ… CLEARED' if cache_success else '❌ FAILED'}")
print(f" OpenRouter Test: {'βœ… WORKING' if test_success else '❌ FAILED'}")
if fix_success and test_success:
print("\nπŸŽ‰ OpenRouter model issue has been fixed!")
print("πŸ’‘ You can now restart your Streamlit app.")
else:
print("\n⚠️ Some issues remain. Please check the logs above.")
return fix_success and test_success
if __name__ == "__main__":
main()