Spaces:
Sleeping
Sleeping
| # 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() |