| |
| """ |
| 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: |
| with open('app.py', 'r') as f: |
| code = f.read() |
| |
| compile(code, 'app.py', 'exec') |
| print("β
app.py is valid Python code") |
| |
| |
| 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 |
|
|
|
|
| def test_app_launch(): |
| """Test if app can be imported and launched""" |
| print("\n" + "="*80) |
| print("Testing App Launch...") |
| print("="*80) |
| |
| try: |
| |
| import app as app_module |
| print("β
app.py imported successfully") |
| |
| |
| if hasattr(app_module, 'PPTScriptGenerator'): |
| print("β
PPTScriptGenerator class accessible") |
| |
| |
| 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() |
| } |
| |
| |
| 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) |
|
|