Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from exa_py import Exa | |
| import traceback | |
| # Get API key from environment variable | |
| EXA_API_KEY = os.getenv("EXA_API_KEY") | |
| print(f"=== DEBUG INFO ===") | |
| print(f"API Key status: {'Set' if EXA_API_KEY else 'Not set'}") | |
| if EXA_API_KEY: | |
| print(f"API Key length: {len(EXA_API_KEY)}") | |
| print(f"API Key starts with: {EXA_API_KEY[:8]}...") | |
| if not EXA_API_KEY: | |
| raise ValueError("EXA_API_KEY environment variable is required") | |
| try: | |
| exa = Exa(EXA_API_KEY) | |
| print("Exa client initialized successfully") | |
| except Exception as e: | |
| print(f"Error initializing Exa client: {e}") | |
| raise | |
| def search_trainers(requirements, num_results=5): | |
| """Search for LinkedIn profiles matching the requirements""" | |
| try: | |
| print(f"\n=== SEARCH DEBUG ===") | |
| print(f"Query: {requirements}") | |
| print(f"Num results: {num_results}") | |
| print("Making API call...") | |
| response = exa.search_and_contents( | |
| query=requirements, | |
| type="auto", | |
| category="linkedin profile", | |
| num_results=num_results, | |
| text={"max_characters": 2000}, | |
| summary=True | |
| ) | |
| print(f"API call successful!") | |
| print(f"Number of results: {len(response.results)}") | |
| # Print first result for debugging | |
| if response.results: | |
| first_result = response.results[0] | |
| print(f"First result title: {first_result.title}") | |
| print(f"First result URL: {first_result.url}") | |
| return response.results | |
| except Exception as e: | |
| print(f"=== SEARCH ERROR ===") | |
| print(f"Error: {e}") | |
| print(f"Error type: {type(e).__name__}") | |
| print(f"Traceback: {traceback.format_exc()}") | |
| return [] | |
| def find_trainers(requirements, num_trainers): | |
| """Main function to find trainers and format results for Gradio""" | |
| try: | |
| # Validate input | |
| if not requirements.strip(): | |
| return "Please enter your trainer requirements." | |
| # Ensure num_trainers is within reasonable bounds | |
| num_trainers = max(1, min(int(num_trainers), 20)) | |
| print(f"\n=== FIND TRAINERS ===") | |
| print(f"Requirements: {requirements}") | |
| print(f"Number: {num_trainers}") | |
| # Search for trainers | |
| trainers = search_trainers(requirements, num_trainers) | |
| if not trainers: | |
| return f""" | |
| ## No Results Found | |
| **Search Details:** | |
| - Query: "{requirements}" | |
| - Number requested: {num_trainers} | |
| - API Status: β Connected | |
| **Try these suggestions:** | |
| 1. **Simplify your search**: Try just "trainer" or "coach" | |
| 2. **Remove location**: Try without city/country restrictions | |
| 3. **Use different keywords**: "leadership trainer", "business coach", "corporate trainer" | |
| **Example working queries:** | |
| - "leadership trainer" | |
| - "sales coach" | |
| - "business trainer" | |
| - "corporate training expert" | |
| Check the logs tab for detailed error information. | |
| """ | |
| # Format results | |
| results = f"## β Found {len(trainers)} Matching Trainers\n\n" | |
| for i, trainer in enumerate(trainers, 1): | |
| results += f"### {i}. {trainer.title}\n" | |
| results += f"**LinkedIn:** [View Profile]({trainer.url})\n\n" | |
| if trainer.summary: | |
| results += f"**Summary:**\n{trainer.summary}\n\n" | |
| if trainer.text: | |
| # Limit text preview to 300 characters for better readability | |
| text_preview = trainer.text[:300] + "..." if len(trainer.text) > 300 else trainer.text | |
| results += f"**Profile Details:**\n{text_preview}\n\n" | |
| results += "---\n\n" | |
| return results | |
| except Exception as e: | |
| error_msg = f""" | |
| ## β Error Occurred | |
| **Error:** {str(e)} | |
| **Type:** {type(e).__name__} | |
| **Troubleshooting:** | |
| 1. Check the Logs tab for detailed error information | |
| 2. Try a simpler search query | |
| 3. Verify your API key is working | |
| Please check the application logs for more details. | |
| """ | |
| print(f"FIND_TRAINERS ERROR: {e}") | |
| print(f"Full traceback: {traceback.format_exc()}") | |
| return error_msg | |
| # Test the API connection on startup | |
| def test_api_connection(): | |
| try: | |
| print("\n=== TESTING API CONNECTION ===") | |
| test_response = exa.search( | |
| query="test", | |
| num_results=1 | |
| ) | |
| print("β API connection test successful!") | |
| return True | |
| except Exception as e: | |
| print(f"β API connection test failed: {e}") | |
| return False | |
| # Test on startup | |
| api_working = test_api_connection() | |
| # Create Gradio interface | |
| def create_trainer_finder_app(): | |
| with gr.Blocks(title="Trainer Finder", theme=gr.themes.Soft()) as app: | |
| gr.Markdown("# π― Trainer Finder") | |
| gr.Markdown("Find specialized trainers based on your requirements using Exa API") | |
| # Add status indicators | |
| api_status = "β API Connected" if api_working else "β API Connection Failed" | |
| env_status = "β Environment Variable Set" if EXA_API_KEY else "β Environment Variable Missing" | |
| gr.Markdown(f""" | |
| **System Status:** | |
| - {env_status} | |
| - {api_status} | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| requirements_input = gr.Textbox( | |
| label="Training Requirements", | |
| placeholder="Start simple: try 'leadership trainer' or 'sales coach'", | |
| lines=3, | |
| value="leadership trainer" # Default value for testing | |
| ) | |
| num_trainers_input = gr.Slider( | |
| minimum=1, | |
| maximum=10, # Reduced for faster testing | |
| value=3, # Reduced default | |
| step=1, | |
| label="Number of trainers to find" | |
| ) | |
| search_button = gr.Button("π Find Trainers", variant="primary") | |
| with gr.Row(): | |
| results_output = gr.Markdown(label="Results") | |
| # Connect the button to the function | |
| search_button.click( | |
| fn=find_trainers, | |
| inputs=[requirements_input, num_trainers_input], | |
| outputs=results_output | |
| ) | |
| # Simplified examples for better success rate | |
| gr.Examples( | |
| examples=[ | |
| ["leadership trainer", 3], | |
| ["sales coach", 3], | |
| ["business trainer", 3], | |
| ["executive coach", 3] | |
| ], | |
| inputs=[requirements_input, num_trainers_input] | |
| ) | |
| return app | |
| # Create and launch the app | |
| if __name__ == "__main__": | |
| app = create_trainer_finder_app() | |
| app.launch() |