# 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()