Spaces:
Build error
Build error
Download strain_processor.py from sp103107/growing-guide: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/sp103107/growing-guide/resolve/main/strain_processor.py
- Command line
-
hf download hf://spaces/sp103107/growing-guide/strain_processor.py
-
curl -L -o strain_processor.py https://huggingface.co/spaces/sp103107/growing-guide/resolve/main/strain_processor.py
5.01 kB
| import requests | |
| import json | |
| from typing import List, Dict | |
| from pathlib import Path | |
| import os | |
| from dotenv import load_dotenv | |
| class StrainProcessor: | |
| def __init__(self): | |
| """Initialize the Strain Processor with Hugging Face endpoints""" | |
| load_dotenv() | |
| # Base URL for the Hugging Face space | |
| self.base_url = "https://huggingface.co/spaces/stephenpalazzo/growing-guide" | |
| self.headers = { | |
| "Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}", | |
| "Content-Type": "application/json" | |
| } | |
| def process_single_strain(self, strain_data: Dict) -> Dict: | |
| """ | |
| Process a single strain through the API | |
| Args: | |
| strain_data (dict): Data for a single strain, e.g., | |
| { | |
| "strain_name": "Blue Dream", | |
| "difficulty": "Medium", | |
| "total_time_weeks": 12, | |
| "optimal_temperature": "20-25°C", | |
| "optimal_humidity": "40-60%", | |
| "light_schedule": "18/6 veg, 12/12 flower" | |
| } | |
| """ | |
| endpoint = f"{self.base_url}/api/v2/strains/process" | |
| try: | |
| response = requests.post( | |
| endpoint, | |
| headers=self.headers, | |
| json=strain_data | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error processing strain: {e}") | |
| return {"error": str(e)} | |
| def process_batch_strains(self, strains: List[Dict]) -> Dict: | |
| """ | |
| Process multiple strains in batch | |
| Args: | |
| strains (list): List of strain dictionaries | |
| """ | |
| endpoint = f"{self.base_url}/api/v2/strains/batch" | |
| try: | |
| response = requests.post( | |
| endpoint, | |
| headers=self.headers, | |
| json={"strains": strains} | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error processing batch: {e}") | |
| return {"error": str(e)} | |
| def get_debug_status(self) -> Dict: | |
| """Check system debug status""" | |
| endpoint = f"{self.base_url}/debug" | |
| try: | |
| response = requests.get( | |
| endpoint, | |
| headers=self.headers | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error getting debug status: {e}") | |
| return {"error": str(e)} | |
| def health_check(self) -> Dict: | |
| """Perform health check""" | |
| endpoint = f"{self.base_url}/health" | |
| try: | |
| response = requests.get( | |
| endpoint, | |
| headers=self.headers | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error performing health check: {e}") | |
| return {"error": str(e)} | |
| def save_results(data: Dict, filename: str): | |
| """Helper function to save results to JSON file""" | |
| results_dir = Path("results") | |
| results_dir.mkdir(exist_ok=True) | |
| filepath = results_dir / filename | |
| with open(filepath, 'w') as f: | |
| json.dump(data, f, indent=2) | |
| print(f"Results saved to {filepath}") | |
| # Example usage | |
| if __name__ == "__main__": | |
| processor = StrainProcessor() | |
| # Check system health first | |
| health_status = processor.health_check() | |
| print("Health Status:", health_status) | |
| # Process a single strain | |
| single_strain = { | |
| "strain_name": "Blue Dream", | |
| "difficulty": "Medium", | |
| "total_time_weeks": 12, | |
| "optimal_temperature": "20-25°C", | |
| "optimal_humidity": "40-60%", | |
| "light_schedule": "18/6 veg, 12/12 flower" | |
| } | |
| result = processor.process_single_strain(single_strain) | |
| save_results(result, "single_strain_result.json") | |
| # Process multiple strains | |
| batch_strains = [ | |
| { | |
| "strain_name": "OG Kush", | |
| "difficulty": "Hard", | |
| "total_time_weeks": 10, | |
| "optimal_temperature": "21-26°C", | |
| "optimal_humidity": "45-55%", | |
| "light_schedule": "18/6 veg, 12/12 flower" | |
| }, | |
| { | |
| "strain_name": "Northern Lights", | |
| "difficulty": "Easy", | |
| "total_time_weeks": 8, | |
| "optimal_temperature": "20-25°C", | |
| "optimal_humidity": "40-60%", | |
| "light_schedule": "18/6 veg, 12/12 flower" | |
| } | |
| ] | |
| batch_result = processor.process_batch_strains(batch_strains) | |
| save_results(batch_result, "batch_strains_result.json") | |
| # Get debug status | |
| debug_status = processor.get_debug_status() | |
| save_results(debug_status, "debug_status.json") |