| |
| """ |
| Temporary app.py for debugging Hugging Face Space issues |
| Replace your current app.py with this file temporarily |
| """ |
|
|
| import os |
| import pandas as pd |
| import faiss |
| import google.generativeai as genai |
| import numpy as np |
| import gradio as gr |
|
|
| def run_all_checks(): |
| """Run all diagnostic checks and return results""" |
| results = [] |
| |
| def add_result(text): |
| results.append(text) |
| print(text) |
| |
| add_result("π DEBUGGING HUGGING FACE SPACE") |
| add_result("=" * 50) |
| |
| |
| add_result("\nπ ENVIRONMENT CHECK") |
| add_result("=" * 30) |
| |
| |
| api_keys = { |
| "GOOGLE_API_KEY": os.environ.get("GOOGLE_API_KEY"), |
| "gemini_api": os.environ.get("gemini_api"), |
| "GEMINI_API_KEY": os.environ.get("GEMINI_API_KEY"), |
| } |
| |
| found_key = None |
| for key_name, key_value in api_keys.items(): |
| if key_value: |
| add_result(f"β
{key_name}: {key_value[:10]}...") |
| found_key = key_value |
| else: |
| add_result(f"β {key_name}: Not found") |
| |
| if not found_key: |
| add_result("β No API key found in any expected environment variable") |
| return "\n".join(results) |
| |
| |
| try: |
| genai.configure(api_key=found_key) |
| add_result("β
Gemini API configured successfully") |
| except Exception as e: |
| add_result(f"β Gemini API configuration failed: {e}") |
| return "\n".join(results) |
| |
| |
| add_result("\nπ FILE CHECK") |
| add_result("=" * 30) |
| |
| add_result(f"Current directory: {os.getcwd()}") |
| add_result(f"Directory contents: {os.listdir('.')}") |
| |
| files_to_check = [ |
| "research_chunks.faiss", |
| "chunk_metadata.csv", |
| "requirements.txt" |
| ] |
| |
| all_files_exist = True |
| for file_path in files_to_check: |
| if os.path.exists(file_path): |
| size = os.path.getsize(file_path) |
| add_result(f"β
{file_path}: {size:,} bytes") |
| else: |
| add_result(f"β {file_path}: NOT FOUND") |
| if file_path in ["research_chunks.faiss", "chunk_metadata.csv"]: |
| all_files_exist = False |
| |
| if not all_files_exist: |
| add_result("\nβ CRITICAL: Missing required data files!") |
| add_result("You need to upload:") |
| add_result("- research_chunks.faiss (FAISS vector index)") |
| add_result("- chunk_metadata.csv (document metadata)") |
| return "\n".join(results) |
| |
| |
| add_result("\nπ FAISS INDEX CHECK") |
| add_result("=" * 30) |
| |
| try: |
| index = faiss.read_index("research_chunks.faiss") |
| add_result(f"β
FAISS index loaded: {index.ntotal:,} vectors") |
| add_result(f"β
Index dimension: {index.d}") |
| add_result(f"β
Index type: {type(index).__name__}") |
| except Exception as e: |
| add_result(f"β FAISS index loading failed: {e}") |
| return "\n".join(results) |
| |
| |
| add_result("\nπ METADATA CHECK") |
| add_result("=" * 30) |
| |
| try: |
| metadata = pd.read_csv("chunk_metadata.csv") |
| add_result(f"β
Metadata loaded: {len(metadata):,} rows") |
| add_result(f"β
Columns ({len(metadata.columns)}): {list(metadata.columns)[:5]}...") |
| add_result(f"β
Unique records: {metadata['record_id'].nunique():,}") |
| |
| |
| required_cols = ['record_id', 'text', 'title'] |
| missing_cols = [col for col in required_cols if col not in metadata.columns] |
| if missing_cols: |
| add_result(f"β οΈ Missing required columns: {missing_cols}") |
| else: |
| add_result("β
All required columns present") |
| |
| |
| add_result("\nπ Sample data:") |
| for i, row in metadata.head(2).iterrows(): |
| add_result(f"Row {i}: {row.get('title', 'No title')}") |
| add_result(f" Text preview: {str(row.get('text', 'No text'))[:100]}...") |
| |
| except Exception as e: |
| add_result(f"β Metadata loading failed: {e}") |
| return "\n".join(results) |
| |
| |
| add_result("\nπ§ EMBEDDING API TEST") |
| add_result("=" * 30) |
| |
| try: |
| test_query = "agricultural research methods" |
| add_result(f"Testing with query: '{test_query}'") |
| |
| embed_result = genai.embed_content( |
| model="models/embedding-001", |
| content=test_query, |
| task_type="retrieval_query" |
| ) |
| |
| embedding = np.array([embed_result['embedding']], dtype="float32") |
| add_result(f"β
Embedding created: shape {embedding.shape}") |
| add_result(f"β
First 5 values: {embedding[0][:5]}") |
| |
| except Exception as e: |
| add_result(f"β Embedding API test failed: {e}") |
| return "\n".join(results) |
| |
| |
| add_result("\nπ FULL SEARCH TEST") |
| add_result("=" * 30) |
| |
| try: |
| distances, indices = index.search(embedding, k=5) |
| add_result(f"β
Search completed") |
| add_result(f"β
Indices: {indices[0]}") |
| add_result(f"β
Distances: {distances[0]}") |
| |
| |
| valid_indices = [idx for idx in indices[0] if idx != -1 and idx < len(metadata)] |
| add_result(f"β
Valid results: {len(valid_indices)}/5") |
| |
| if valid_indices: |
| sample_idx = valid_indices[0] |
| sample_row = metadata.iloc[sample_idx] |
| similarity = 1 / (1 + distances[0][0]) |
| add_result(f"\nπ Best match (similarity: {similarity:.3f}):") |
| add_result(f" Title: {sample_row.get('title', 'N/A')}") |
| add_result(f" Text: {str(sample_row.get('text', 'N/A'))[:200]}...") |
| |
| except Exception as e: |
| add_result(f"β Full search test failed: {e}") |
| return "\n".join(results) |
| |
| |
| add_result("\nπ PYTHON ENVIRONMENT") |
| add_result("=" * 30) |
| |
| import sys |
| add_result(f"Python version: {sys.version}") |
| add_result(f"Platform: {sys.platform}") |
| |
| try: |
| import pkg_resources |
| installed = [pkg.project_name for pkg in pkg_resources.working_set] |
| required = ['gradio', 'faiss-cpu', 'google-generativeai', 'pandas', 'numpy', 'plotly'] |
| missing = [pkg for pkg in required if pkg not in installed] |
| if missing: |
| add_result(f"β οΈ Missing packages: {missing}") |
| else: |
| add_result("β
All required packages installed") |
| except: |
| add_result("β οΈ Could not check installed packages") |
| |
| add_result("\nπ ALL TESTS COMPLETED!") |
| add_result("\nIf you see this message, your system should be working!") |
| add_result("You can now replace this debug app.py with your original app.py") |
| |
| return "\n".join(results) |
|
|
| def create_debug_interface(): |
| """Create a simple Gradio interface for debugging""" |
| |
| with gr.Blocks(title="Debug Hugging Face Space") as app: |
| gr.HTML(""" |
| <div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #ff6b6b, #4ecdc4); color: white; border-radius: 10px;"> |
| <h1>π§ Hugging Face Space Debugger</h1> |
| <p>This will help identify why your search isn't working</p> |
| </div> |
| """) |
| |
| with gr.Row(): |
| run_btn = gr.Button("π Run Full Diagnostic", variant="primary", size="lg") |
| |
| with gr.Row(): |
| output = gr.Textbox( |
| label="Diagnostic Results", |
| lines=30, |
| max_lines=50, |
| interactive=False, |
| show_copy_button=True |
| ) |
| |
| |
| app.load(run_all_checks, outputs=output) |
| run_btn.click(run_all_checks, outputs=output) |
| |
| gr.HTML(""" |
| <div style="margin-top: 20px; padding: 15px; background: #f0f8ff; border-radius: 5px;"> |
| <h3>π What This Checks:</h3> |
| <ul> |
| <li><strong>API Key:</strong> Verifies Google Gemini API key is set correctly</li> |
| <li><strong>Files:</strong> Checks if FAISS index and metadata CSV exist</li> |
| <li><strong>Data:</strong> Validates file contents and structure</li> |
| <li><strong>Search:</strong> Tests the complete search pipeline</li> |
| <li><strong>Environment:</strong> Verifies Python packages and setup</li> |
| </ul> |
| <p><strong>Next Steps:</strong> Once all tests pass, replace this debug app.py with your original app.py</p> |
| </div> |
| """) |
| |
| return app |
|
|
| if __name__ == "__main__": |
| |
| print("Running initial diagnostics...") |
| run_all_checks() |
| |
| |
| app = create_debug_interface() |
| app.launch( |
| share=True, |
| server_name="0.0.0.0", |
| server_port=7860, |
| show_error=True |
| ) |