#!/usr/bin/env python3 """ Local Test Script for HuggingFace Spaces App Tests the app.py before deploying to HuggingFace """ import sys import os def test_imports(): """Test if required packages are available""" print("="*80) print("Testing Package Imports...") print("="*80) required_packages = { 'gradio': 'Gradio UI Framework', 'huggingface_hub': 'HuggingFace Hub Client' } all_passed = True for package, description in required_packages.items(): try: __import__(package) print(f"✅ {description}: INSTALLED") except ImportError: print(f"❌ {description}: NOT FOUND") print(f" Install with: pip install {package}") all_passed = False return all_passed def test_app_file(): """Test if app.py exists and is valid Python""" print("\n" + "="*80) print("Testing app.py File...") print("="*80) if not os.path.exists('app.py'): print("❌ app.py not found in current directory") print(" Make sure you're in the correct folder") return False print("✅ app.py file exists") # Try to parse it try: with open('app.py', 'r') as f: code = f.read() compile(code, 'app.py', 'exec') print("✅ app.py is valid Python code") # Check for key components if 'PPTScriptGenerator' in code: print("✅ PPTScriptGenerator class found") else: print("⚠️ Warning: PPTScriptGenerator class not found") if 'create_gradio_interface' in code: print("✅ create_gradio_interface function found") else: print("⚠️ Warning: create_gradio_interface function not found") if 'InferenceClient' in code: print("✅ InferenceClient usage found") else: print("⚠️ Warning: InferenceClient not found") return True except SyntaxError as e: print(f"❌ Syntax error in app.py: {e}") return False def test_requirements(): """Test if requirements file exists""" print("\n" + "="*80) print("Testing Requirements File...") print("="*80) req_files = ['requirements_hf.txt', 'requirements.txt'] found = False for req_file in req_files: if os.path.exists(req_file): print(f"✅ {req_file} found") with open(req_file, 'r') as f: content = f.read() print(f" Contents:\n{content}") found = True break if not found: print("❌ No requirements file found") print(" Need either requirements_hf.txt or requirements.txt") return False return True def test_readme(): """Test if README exists""" print("\n" + "="*80) print("Testing README File...") print("="*80) readme_files = ['README_HF.md', 'README.md'] found = False for readme in readme_files: if os.path.exists(readme): print(f"✅ {readme} found") with open(readme, 'r') as f: first_lines = ''.join(f.readlines()[:10]) if '---' in first_lines and 'title:' in first_lines: print("✅ YAML frontmatter detected") else: print("⚠️ Warning: YAML frontmatter not found (needed for HF Spaces)") found = True break if not found: print("⚠️ Warning: README file not found (recommended for HF Spaces)") return True # Not critical def test_app_launch(): """Test if app can be imported and launched""" print("\n" + "="*80) print("Testing App Launch...") print("="*80) try: # Try importing the app import app as app_module print("✅ app.py imported successfully") # Check if it has the expected components if hasattr(app_module, 'PPTScriptGenerator'): print("✅ PPTScriptGenerator class accessible") # Try instantiating try: generator = app_module.PPTScriptGenerator() print("✅ PPTScriptGenerator instantiated") except Exception as e: print(f"⚠️ Warning: Could not instantiate PPTScriptGenerator: {e}") print(" (This might be okay - could need HF_TOKEN)") if hasattr(app_module, 'create_gradio_interface'): print("✅ create_gradio_interface function accessible") return True except Exception as e: print(f"❌ Error importing app.py: {e}") return False def test_gradio_creation(): """Test if Gradio interface can be created""" print("\n" + "="*80) print("Testing Gradio Interface Creation...") print("="*80) try: import app as app_module demo = app_module.create_gradio_interface() print("✅ Gradio interface created successfully") print("\n💡 You can now launch the app locally with:") print(" python app.py") print("\n Or test in this session:") print(" >>> import app") print(" >>> demo = app.create_gradio_interface()") print(" >>> demo.launch()") return True except Exception as e: print(f"❌ Error creating Gradio interface: {e}") return False def main(): """Run all tests""" print("\n" + "🔍 HUGGINGFACE SPACES APP - PRE-DEPLOYMENT TEST") print("="*80) print("") results = { 'imports': test_imports(), 'app_file': test_app_file(), 'requirements': test_requirements(), 'readme': test_readme(), 'app_launch': test_app_launch(), 'gradio_creation': test_gradio_creation() } # Summary print("\n" + "="*80) print("TEST SUMMARY") print("="*80) passed = sum(1 for v in results.values() if v) total = len(results) for test_name, result in results.items(): status = "✅ PASSED" if result else "❌ FAILED" print(f"{test_name.upper():25s}: {status}") print("="*80) print(f"Results: {passed}/{total} tests passed") if passed == total: print("\n🎉 All tests passed! Your app is ready to deploy to HuggingFace Spaces!") print("\n📋 Next Steps:") print(" 1. Go to https://huggingface.co/new-space") print(" 2. Create a new Space with SDK: Gradio") print(" 3. Upload these files:") print(" - app.py") print(" - requirements_hf.txt (rename to requirements.txt)") print(" - README_HF.md (rename to README.md)") print(" 4. Wait for build (1-2 minutes)") print(" 5. Your app will be live!") print("\n See DEPLOYMENT_GUIDE.md for detailed instructions") else: print("\n⚠️ Some tests failed. Please fix the errors above before deploying.") print(" Common fixes:") print(" - Install missing packages: pip install gradio huggingface_hub") print(" - Ensure app.py is in current directory") print(" - Check for syntax errors in app.py") print("="*80) return passed == total if __name__ == "__main__": success = main() sys.exit(0 if success else 1)