aseelflihan commited on
Commit
fd5c46a
Β·
1 Parent(s): 33d3592
Files changed (4) hide show
  1. debug_ask_ai_feature.py +182 -0
  2. restart_clean.py +186 -0
  3. token.json +0 -1
  4. translator.py +7 -7
debug_ask_ai_feature.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # debug_ask_ai_feature.py - Debug the specific Ask AI feature
2
+
3
+ import os
4
+ from dotenv import load_dotenv
5
+ import traceback
6
+
7
+ def debug_ask_ai_feature():
8
+ """Debug the Ask AI feature specifically"""
9
+
10
+ print("πŸ” Debugging Ask AI Feature")
11
+ print("=" * 60)
12
+
13
+ # Load environment
14
+ load_dotenv()
15
+
16
+ print("πŸ“‹ Environment Check:")
17
+ print(f" OPENROUTER_MODEL: {os.getenv('OPENROUTER_MODEL')}")
18
+ print(f" OPENROUTER_API_KEY: {os.getenv('OPENROUTER_API_KEY')[:20]}...")
19
+
20
+ try:
21
+ # Import the modules
22
+ from translator import get_translator
23
+ from ai_questions import get_ai_question_engine
24
+
25
+ print("\nπŸ”§ Getting instances...")
26
+ translator = get_translator()
27
+ question_engine = get_ai_question_engine()
28
+
29
+ print(f"πŸ“‹ Translator model: {translator.openrouter_model}")
30
+ print(f"πŸ“‹ Engine translator model: {question_engine.translator.openrouter_model}")
31
+
32
+ # Test the exact scenario from the UI
33
+ print("\nπŸ§ͺ Testing Ask AI scenario...")
34
+
35
+ selected_text = "Hello, this is and this is a test. I just that I want to see"
36
+ question = "Ψ§Ψ΄Ψ±Ψ­ Ω‡Ψ°Ψ§ Ψ§Ω„Ω†Ψ΅ Ψ¨Ψ§Ω„ΨͺΩΨ΅ΩŠΩ„"
37
+
38
+ segment_info = {
39
+ 'id': 'test_segment',
40
+ 'start_ms': 0,
41
+ 'end_ms': 0
42
+ }
43
+
44
+ print(f"πŸ“ Selected text: {selected_text}")
45
+ print(f"❓ Question: {question}")
46
+ print(f"🎯 Preferred model: OpenRouter AI")
47
+
48
+ # Call process_question with OpenRouter AI specifically
49
+ result = question_engine.process_question(
50
+ selected_text=selected_text,
51
+ question=question,
52
+ segment_info=segment_info,
53
+ ui_language='ar',
54
+ session_id=None,
55
+ preferred_model='OpenRouter AI'
56
+ )
57
+
58
+ if len(result) == 4:
59
+ answer, error, session_id, model_used = result
60
+ else:
61
+ answer, error, session_id = result
62
+ model_used = "Unknown"
63
+
64
+ print(f"\nπŸ“Š Results:")
65
+ print(f" Answer: {'βœ… Success' if answer else '❌ Failed'}")
66
+ print(f" Error: {error}")
67
+ print(f" Model used: {model_used}")
68
+ print(f" Session ID: {session_id}")
69
+
70
+ if answer:
71
+ print(f" Answer preview: {answer[:100]}...")
72
+
73
+ if error and "meta-llama-3-70b-instruct" in str(error):
74
+ print("\n🚨 FOUND THE PROBLEM!")
75
+ print("The old model is being used somewhere in the process")
76
+
77
+ # Let's trace where this is coming from
78
+ print("\nπŸ” Tracing the error source...")
79
+
80
+ # Check the translator's _openrouter_complete method directly
81
+ print("Testing _openrouter_complete directly...")
82
+ test_prompt = "Test prompt"
83
+ response, error = translator._openrouter_complete(test_prompt)
84
+
85
+ if error and "meta-llama-3-70b-instruct" in str(error):
86
+ print("🎯 Error is coming from _openrouter_complete method!")
87
+ print(f"Error: {error}")
88
+ else:
89
+ print("βœ… _openrouter_complete works fine")
90
+ print(f"Response: {response[:50] if response else 'None'}...")
91
+
92
+ return True
93
+
94
+ except Exception as e:
95
+ print(f"πŸ’₯ Exception: {str(e)}")
96
+ print(f"πŸ“‹ Traceback: {traceback.format_exc()}")
97
+
98
+ if "meta-llama-3-70b-instruct" in str(e):
99
+ print("\n🚨 FOUND THE PROBLEM IN EXCEPTION!")
100
+ print("The old model reference is in the exception")
101
+
102
+ return False
103
+
104
+ def test_openrouter_direct_call():
105
+ """Test OpenRouter direct call to see what's happening"""
106
+
107
+ print("\nπŸ” Testing OpenRouter Direct Call")
108
+ print("=" * 60)
109
+
110
+ try:
111
+ from translator import get_translator
112
+ translator = get_translator()
113
+
114
+ print(f"πŸ“‹ Translator model: {translator.openrouter_model}")
115
+ print(f"πŸ”‘ Has API key: {'Yes' if translator.openrouter_api_key else 'No'}")
116
+
117
+ # Test with a simple prompt
118
+ test_prompt = "Hello, respond with 'Test successful' in Arabic."
119
+
120
+ print(f"πŸ“ Test prompt: {test_prompt}")
121
+ print("πŸš€ Calling _openrouter_complete...")
122
+
123
+ response, error = translator._openrouter_complete(test_prompt)
124
+
125
+ print(f"πŸ“Š Results:")
126
+ print(f" Response: {'βœ… Success' if response else '❌ Failed'}")
127
+ print(f" Error: {error}")
128
+
129
+ if response:
130
+ print(f" Response text: {response}")
131
+
132
+ if error:
133
+ print(f" Error details: {error}")
134
+
135
+ if "meta-llama-3-70b-instruct" in str(error):
136
+ print("\n🚨 OLD MODEL FOUND IN ERROR!")
137
+ print("This means the old model is hardcoded somewhere")
138
+
139
+ # Let's check the candidates list
140
+ print("\nπŸ” Checking candidates list in translator...")
141
+
142
+ # We need to inspect the _openrouter_complete method
143
+ import inspect
144
+ source = inspect.getsource(translator._openrouter_complete)
145
+
146
+ if "meta-llama-3-70b-instruct" in source:
147
+ print("🎯 OLD MODEL FOUND IN SOURCE CODE!")
148
+ else:
149
+ print("βœ… No old model in source code")
150
+
151
+ return response is not None
152
+
153
+ except Exception as e:
154
+ print(f"πŸ’₯ Exception: {str(e)}")
155
+ print(f"πŸ“‹ Traceback: {traceback.format_exc()}")
156
+ return False
157
+
158
+ def main():
159
+ """Main debug function"""
160
+
161
+ print("πŸš€ Ask AI Feature Debug Tool")
162
+ print("=" * 60)
163
+
164
+ # Test 1: Debug Ask AI feature
165
+ success1 = debug_ask_ai_feature()
166
+
167
+ # Test 2: Test OpenRouter direct call
168
+ success2 = test_openrouter_direct_call()
169
+
170
+ print("\n" + "=" * 60)
171
+ print("πŸ“Š Debug Results:")
172
+ print(f" Ask AI Feature: {'βœ… PASS' if success1 else '❌ FAIL'}")
173
+ print(f" OpenRouter Direct: {'βœ… PASS' if success2 else '❌ FAIL'}")
174
+
175
+ if not success1 or not success2:
176
+ print("\nπŸ’‘ Next steps:")
177
+ print(" 1. Check for hardcoded model references")
178
+ print(" 2. Clear all caches and restart")
179
+ print(" 3. Check for environment variable conflicts")
180
+
181
+ if __name__ == "__main__":
182
+ main()
restart_clean.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # restart_clean.py - Clean restart tool
2
+
3
+ import os
4
+ import shutil
5
+ import glob
6
+ import subprocess
7
+ import sys
8
+ from dotenv import load_dotenv
9
+
10
+ def clear_python_cache():
11
+ """Clear all Python cache files"""
12
+ print("🧹 Clearing Python cache...")
13
+
14
+ try:
15
+ # Remove __pycache__ directories
16
+ pycache_dirs = glob.glob("**/__pycache__", recursive=True)
17
+ for cache_dir in pycache_dirs:
18
+ shutil.rmtree(cache_dir, ignore_errors=True)
19
+ print(f" πŸ—‘οΈ Removed {cache_dir}")
20
+
21
+ # Remove .pyc files
22
+ pyc_files = glob.glob("**/*.pyc", recursive=True)
23
+ for pyc_file in pyc_files:
24
+ try:
25
+ os.remove(pyc_file)
26
+ print(f" πŸ—‘οΈ Removed {pyc_file}")
27
+ except:
28
+ pass
29
+
30
+ print("βœ… Python cache cleared")
31
+ return True
32
+
33
+ except Exception as e:
34
+ print(f"❌ Error clearing cache: {str(e)}")
35
+ return False
36
+
37
+ def verify_env_config():
38
+ """Verify .env configuration"""
39
+ print("\nπŸ” Verifying .env configuration...")
40
+
41
+ load_dotenv(override=True)
42
+
43
+ config = {
44
+ 'OPENROUTER_MODEL': os.getenv('OPENROUTER_MODEL'),
45
+ 'OPENROUTER_API_KEY': os.getenv('OPENROUTER_API_KEY'),
46
+ 'GROQ_API_KEY': os.getenv('GROQ_API_KEY'),
47
+ 'GEMINI_API_KEY': os.getenv('GEMINI_API_KEY')
48
+ }
49
+
50
+ print("πŸ“‹ Current configuration:")
51
+ for key, value in config.items():
52
+ if value:
53
+ if 'KEY' in key:
54
+ print(f" βœ… {key}: {value[:20]}...{value[-10:] if len(value) > 30 else value}")
55
+ else:
56
+ print(f" βœ… {key}: {value}")
57
+ else:
58
+ print(f" ❌ {key}: Not set")
59
+
60
+ # Check if we're using the better model
61
+ current_model = config['OPENROUTER_MODEL']
62
+ if current_model == 'meta-llama/llama-3.1-8b-instruct:free':
63
+ print("βœ… Using better OpenRouter model (8B)")
64
+ elif current_model == 'meta-llama/llama-3.2-3b-instruct:free':
65
+ print("⚠️ Using poor quality model (3B) - consider upgrading")
66
+ else:
67
+ print(f"ℹ️ Using custom model: {current_model}")
68
+
69
+ return True
70
+
71
+ def test_new_configuration():
72
+ """Test the new configuration"""
73
+ print("\nπŸ§ͺ Testing new configuration...")
74
+
75
+ try:
76
+ # Force reload modules
77
+ if 'translator' in sys.modules:
78
+ del sys.modules['translator']
79
+ if 'ai_questions' in sys.modules:
80
+ del sys.modules['ai_questions']
81
+
82
+ from translator import get_translator
83
+ from ai_questions import get_ai_question_engine
84
+
85
+ # Get fresh instances
86
+ translator = get_translator()
87
+ question_engine = get_ai_question_engine()
88
+
89
+ print(f"πŸ“‹ New translator model: {translator.openrouter_model}")
90
+
91
+ # Test OpenRouter
92
+ test_prompt = "Hello, respond with 'Configuration test successful' in Arabic."
93
+ response, error = translator._openrouter_complete(test_prompt)
94
+
95
+ if response:
96
+ print("βœ… OpenRouter test successful!")
97
+ print(f"πŸ“ Response: {response}")
98
+ return True
99
+ else:
100
+ print(f"❌ OpenRouter test failed: {error}")
101
+ return False
102
+
103
+ except Exception as e:
104
+ print(f"❌ Test failed: {str(e)}")
105
+ return False
106
+
107
+ def kill_streamlit_processes():
108
+ """Kill any running Streamlit processes"""
109
+ print("\nπŸ”„ Stopping Streamlit processes...")
110
+
111
+ try:
112
+ if os.name == 'nt': # Windows
113
+ subprocess.run(['taskkill', '/f', '/im', 'streamlit.exe'],
114
+ capture_output=True, text=True)
115
+ subprocess.run(['taskkill', '/f', '/im', 'python.exe'],
116
+ capture_output=True, text=True)
117
+ else: # Unix/Linux/Mac
118
+ subprocess.run(['pkill', '-f', 'streamlit'],
119
+ capture_output=True, text=True)
120
+
121
+ print("βœ… Streamlit processes stopped")
122
+ return True
123
+
124
+ except Exception as e:
125
+ print(f"⚠️ Could not stop processes: {str(e)}")
126
+ return False
127
+
128
+ def start_streamlit():
129
+ """Start Streamlit with the new configuration"""
130
+ print("\nπŸš€ Starting Streamlit with new configuration...")
131
+
132
+ try:
133
+ # Start Streamlit
134
+ print("πŸ“± Opening Streamlit app...")
135
+ print("🌐 URL: http://localhost:8501")
136
+ print("⏹️ Press Ctrl+C to stop")
137
+
138
+ subprocess.run([sys.executable, '-m', 'streamlit', 'run', 'app.py'],
139
+ check=True)
140
+
141
+ except KeyboardInterrupt:
142
+ print("\n⏹️ Streamlit stopped by user")
143
+ except Exception as e:
144
+ print(f"❌ Error starting Streamlit: {str(e)}")
145
+
146
+ def main():
147
+ """Main restart function"""
148
+
149
+ print("πŸ”„ Clean Restart Tool")
150
+ print("=" * 50)
151
+
152
+ # Step 1: Kill existing processes
153
+ kill_streamlit_processes()
154
+
155
+ # Step 2: Clear cache
156
+ cache_success = clear_python_cache()
157
+
158
+ # Step 3: Verify configuration
159
+ config_success = verify_env_config()
160
+
161
+ # Step 4: Test new configuration
162
+ test_success = test_new_configuration()
163
+
164
+ print("\n" + "=" * 50)
165
+ print("πŸ“Š Restart Results:")
166
+ print(f" Cache Clear: {'βœ… SUCCESS' if cache_success else '❌ FAILED'}")
167
+ print(f" Config Check: {'βœ… SUCCESS' if config_success else '❌ FAILED'}")
168
+ print(f" Test New Config: {'βœ… SUCCESS' if test_success else '❌ FAILED'}")
169
+
170
+ if cache_success and config_success and test_success:
171
+ print("\nπŸŽ‰ Clean restart successful!")
172
+
173
+ # Ask user if they want to start Streamlit
174
+ try:
175
+ choice = input("\nπŸš€ Start Streamlit now? (y/n): ").lower().strip()
176
+ if choice in ['y', 'yes', 'Ω†ΨΉΩ…', '']:
177
+ start_streamlit()
178
+ else:
179
+ print("βœ… Ready to start manually with: streamlit run app.py")
180
+ except KeyboardInterrupt:
181
+ print("\nβœ… Ready to start manually with: streamlit run app.py")
182
+ else:
183
+ print("\n⚠️ Some issues detected. Please check the logs above.")
184
+
185
+ if __name__ == "__main__":
186
+ main()
token.json DELETED
@@ -1 +0,0 @@
1
- {"token": "ya29.a0AS3H6NwkupTfaaRTSlG8nHSa704w-Ci0FCnLzQrh5TfkNlrO5NoHV8nHgmAZNzVQ9QL2wLbyhoupV4nk125svdeAv5o5q1FGlt8WtBmH_FJL_9f6f89JmLTyvjeX8m1iFnj9pTv1DfMipXGghYKgvoLR05bZ1NJJUBwlWE2ZaCgYKAesSARISFQHGX2MiD1KHTpYpYs-cMcwYpfuTZg0175", "refresh_token": "1//0g9JuyLXTLXoyCgYIARAAGBASNwF-L9IrFWZcmX21Dara-R7chigMnoJDSueVi3CllQvlr1sEbndC7_9nzIoCsxWD97G95fix5ik", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "739771741359-gk2mkvimn063a2msd6hmkkksn17iamre.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/documents"], "universe_domain": "googleapis.com", "account": "", "expiry": "2025-08-12T04:36:23Z"}
 
 
translator.py CHANGED
@@ -345,14 +345,14 @@ Text:
345
  "X-Title": self.openrouter_site_title or "LocalApp",
346
  }
347
  # Candidate list prioritizing common free models
348
- # Always include fallback models for better reliability
349
  fallback_models = [
350
- "meta-llama/llama-3.2-3b-instruct:free",
351
- "meta-llama/llama-3.1-8b-instruct:free",
352
- "google/gemma-2-9b-it:free",
353
- "qwen/qwen-2.5-7b-instruct:free",
354
- "microsoft/phi-3-mini-128k-instruct:free",
355
- "huggingfaceh4/zephyr-7b-beta:free"
356
  ]
357
 
358
  candidates = []
 
345
  "X-Title": self.openrouter_site_title or "LocalApp",
346
  }
347
  # Candidate list prioritizing common free models
348
+ # Always include fallback models for better reliability (ordered by quality)
349
  fallback_models = [
350
+ "meta-llama/llama-3.1-8b-instruct:free", # Better quality
351
+ "google/gemma-2-9b-it:free", # Good for Arabic
352
+ "qwen/qwen-2.5-7b-instruct:free", # Multilingual
353
+ "microsoft/phi-3-mini-128k-instruct:free", # Decent quality
354
+ "meta-llama/llama-3.2-3b-instruct:free", # Last resort (poor quality)
355
+ "mistralai/mistral-7b-instruct:free" # Backup
356
  ]
357
 
358
  candidates = []