| | |
| | """Troubleshoot common issues with the FINESE SCHOOL application. |
| | |
| | This script helps diagnose common configuration issues and provides |
| | suggestions for fixing them. |
| | """ |
| |
|
| | import os |
| | import sys |
| | from dotenv import load_dotenv |
| |
|
| | |
| | load_dotenv() |
| |
|
| | def check_environment_variables(): |
| | """Check if required environment variables are set.""" |
| | print("π Checking environment variables...") |
| | |
| | |
| | api_type = os.getenv("API_TYPE", "huggingface").lower() |
| | print(f"β
API_TYPE is set to: {api_type}") |
| | |
| | |
| | if api_type == "google": |
| | api_key = os.getenv("GOOGLE_API_KEY") |
| | if not api_key: |
| | print("β GOOGLE_API_KEY is not set") |
| | print(" Please set your Google API key in the .env file") |
| | return False |
| | else: |
| | print(f"β
GOOGLE_API_KEY is set (length: {len(api_key)} characters)") |
| | elif api_type == "openai": |
| | api_key = os.getenv("OPENAI_API_KEY") |
| | if not api_key: |
| | print("β OPENAI_API_KEY is not set") |
| | print(" Please set your OpenAI API key in the .env file") |
| | return False |
| | else: |
| | print(f"β
OPENAI_API_KEY is set (length: {len(api_key)} characters)") |
| | else: |
| | api_key = os.getenv("HUGGINGFACE_API_KEY") |
| | if not api_key: |
| | print("β HUGGINGFACE_API_KEY is not set") |
| | print(" Please set your Hugging Face API key in the .env file") |
| | return False |
| | else: |
| | print(f"β
HUGGINGFACE_API_KEY is set (length: {len(api_key)} characters)") |
| | |
| | model_name = os.getenv("MODEL_NAME") |
| | if model_name: |
| | print(f"β
MODEL_NAME is set to: {model_name}") |
| | else: |
| | print("β οΈ MODEL_NAME is not set, using default model") |
| | |
| | temperature = os.getenv("TEMPERATURE", "0.3") |
| | print(f"β
TEMPERATURE is set to: {temperature}") |
| | |
| | max_tokens = os.getenv("MAX_TOKENS", "2048") |
| | print(f"β
MAX_TOKENS is set to: {max_tokens}") |
| | |
| | is_docker = os.getenv("IS_DOCKER", "false") |
| | print(f"β
IS_DOCKER is set to: {is_docker}") |
| | |
| | return True |
| |
|
| | def check_dependencies(): |
| | """Check if required dependencies are installed.""" |
| | print("\nπ Checking dependencies...") |
| | |
| | dependencies = [ |
| | "streamlit", |
| | "pdfkit", |
| | "python-dotenv", |
| | "langchain", |
| | "pydantic", |
| | "pygments" |
| | ] |
| | |
| | |
| | api_type = os.getenv("API_TYPE", "huggingface").lower() |
| | if api_type == "google": |
| | dependencies.append("langchain-google-genai") |
| | elif api_type == "openai": |
| | dependencies.append("langchain-openai") |
| | else: |
| | dependencies.append("langchain-huggingface") |
| | |
| | missing_deps = [] |
| | for dep in dependencies: |
| | try: |
| | __import__(dep) |
| | print(f"β
{dep} is installed") |
| | except ImportError as e: |
| | print(f"β {dep} is missing: {e}") |
| | missing_deps.append(dep) |
| | |
| | if missing_deps: |
| | print(f"\nπ§ To install missing dependencies, run:") |
| | print(f" pip install {' '.join(missing_deps)}") |
| | return False |
| | |
| | return True |
| |
|
| | def check_model_access(): |
| | """Check if we can access the configured model.""" |
| | print("\nπ Checking model access...") |
| | |
| | api_type = os.getenv("API_TYPE", "huggingface").lower() |
| | |
| | if api_type == "google": |
| | try: |
| | from langchain_google_genai import GoogleGenerativeAI |
| | api_key = os.getenv("GOOGLE_API_KEY") |
| | if not api_key: |
| | print("β Cannot check model access without GOOGLE_API_KEY") |
| | return False |
| | |
| | model_name = os.getenv("MODEL_NAME", "gemini-1.5-flash") |
| | print(f" Testing access to Google model: {model_name}") |
| | |
| | llm = GoogleGenerativeAI( |
| | model=model_name, |
| | google_api_key=api_key |
| | ) |
| | |
| | print(" Sending test request...") |
| | response = llm.invoke("Say 'Hello, FINESE SCHOOL!' in one word.") |
| | print(f"β
Successfully connected to Google Generative AI") |
| | print(f" Test response: {response.content.strip()}") |
| | return True |
| | except Exception as e: |
| | print(f"β Failed to access Google model: {str(e)}") |
| | print("\nπ‘ Troubleshooting tips:") |
| | print(" 1. Check that your API key is valid") |
| | print(" 2. Verify the model name is correct by running scripts/list_models.py") |
| | print(" 3. Check your internet connection") |
| | return False |
| | |
| | elif api_type == "openai": |
| | try: |
| | from langchain_openai import ChatOpenAI |
| | api_key = os.getenv("OPENAI_API_KEY") |
| | if not api_key: |
| | print("β Cannot check model access without OPENAI_API_KEY") |
| | return False |
| | |
| | model_name = os.getenv("MODEL_NAME", "gpt-3.5-turbo") |
| | print(f" Testing access to OpenAI model: {model_name}") |
| | |
| | llm = ChatOpenAI( |
| | model_name=model_name, |
| | openai_api_key=api_key |
| | ) |
| | |
| | print(" Sending test request...") |
| | response = llm.invoke("Say 'Hello, FINESE SCHOOL!' in one word.") |
| | print(f"β
Successfully connected to OpenAI") |
| | print(f" Test response: {response.content.strip()}") |
| | return True |
| | except Exception as e: |
| | print(f"β Failed to access OpenAI model: {str(e)}") |
| | print("\nπ‘ Troubleshooting tips:") |
| | print(" 1. Check that your API key is valid") |
| | print(" 2. Verify the model name is correct") |
| | print(" 3. Check your internet connection") |
| | return False |
| | |
| | else: |
| | try: |
| | from langchain_huggingface import HuggingFaceEndpoint |
| | api_key = os.getenv("HUGGINGFACE_API_KEY") |
| | if not api_key: |
| | print("β Cannot check model access without HUGGINGFACE_API_KEY") |
| | return False |
| | |
| | model_name = os.getenv("MODEL_NAME", "mistralai/Mistral-7B-Instruct-v0.2") |
| | print(f" Testing access to Hugging Face model: {model_name}") |
| | |
| | llm = HuggingFaceEndpoint( |
| | repo_id=model_name, |
| | huggingfacehub_api_token=api_key, |
| | temperature=0.1, |
| | max_new_tokens=100 |
| | ) |
| | |
| | print(" Sending test request...") |
| | response = llm.invoke("Say 'Hello, FINESE SCHOOL!' in one word.") |
| | print(f"β
Successfully connected to Hugging Face Inference API") |
| | print(f" Test response: {response.strip()}") |
| | return True |
| | except Exception as e: |
| | print(f"β Failed to access Hugging Face model: {str(e)}") |
| | print("\nπ‘ Troubleshooting tips:") |
| | print(" 1. Check that your API key is valid") |
| | print(" 2. Verify the model name is correct") |
| | print(" 3. Check your internet connection") |
| | print(" 4. Make sure you haven't exceeded rate limits") |
| | return False |
| |
|
| | def check_wkhtmltopdf(): |
| | """Check if wkhtmltopdf is installed for PDF generation.""" |
| | print("\nπ Checking PDF generation support...") |
| | |
| | try: |
| | import pdfkit |
| | print("β
pdfkit is installed") |
| | except ImportError: |
| | print("β pdfkit is not installed") |
| | return False |
| | |
| | try: |
| | |
| | config = pdfkit.configuration() |
| | print("β
wkhtmltopdf is configured") |
| | return True |
| | except OSError: |
| | print("β οΈ wkhtmltopdf is not installed or not in PATH") |
| | print(" PDF export functionality will be limited") |
| | print("\nπ§ To install wkhtmltopdf:") |
| | print(" Windows: Download from https://wkhtmltopdf.org/downloads.html") |
| | print(" macOS: brew install --cask wkhtmltopdf") |
| | print(" Linux: sudo apt-get install wkhtmltopdf") |
| | return True |
| |
|
| | def main(): |
| | """Main troubleshooting function.""" |
| | print("π οΈ FINESE SCHOOL Troubleshooting Script") |
| | print("=" * 50) |
| | |
| | checks = [ |
| | check_environment_variables, |
| | check_dependencies, |
| | check_model_access, |
| | check_wkhtmltopdf |
| | ] |
| | |
| | results = [] |
| | for check in checks: |
| | try: |
| | results.append(check()) |
| | except Exception as e: |
| | print(f"β Check failed with exception: {e}") |
| | results.append(False) |
| | |
| | print("\nπ Summary") |
| | print("=" * 50) |
| | |
| | if all(results): |
| | print("β
All checks passed! You should be able to run FINESE SCHOOL.") |
| | print("\nπ To start the application, run:") |
| | print(" streamlit run src/app.py") |
| | else: |
| | passed = sum(results) |
| | total = len(results) |
| | print(f"β οΈ {passed}/{total} checks passed.") |
| | if passed > 0: |
| | print("β
Some functionality may work, but fix the issues above for full functionality.") |
| | else: |
| | print("β Critical issues found. Please address them before running the application.") |
| | print("\nπ For more help, check the README.md file or open an issue on GitHub.") |
| |
|
| | if __name__ == "__main__": |
| | main() |