File size: 4,992 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
# 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()