Spaces:
Build error
Build error
Download strain_api.py from sp103107/growing-guide: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/spaces/sp103107/growing-guide/resolve/main/strain_api.py
- Command line
-
hf download hf://spaces/sp103107/growing-guide/strain_api.py
-
curl -L -o strain_api.py https://huggingface.co/spaces/sp103107/growing-guide/resolve/main/strain_api.py
4.57 kB
| import requests | |
| import json | |
| from typing import List, Dict, Optional | |
| from pathlib import Path | |
| class StrainAPI: | |
| def __init__(self, base_url: str, api_key: Optional[str] = None): | |
| """ | |
| Initialize the Strain API client. | |
| Args: | |
| base_url (str): The base URL of the API | |
| api_key (str, optional): API key for authentication | |
| """ | |
| self.base_url = base_url.rstrip('/') | |
| self.api_key = api_key | |
| self.headers = { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': f'Bearer {api_key}' if api_key else None | |
| } | |
| def process_strain(self, strain_data: Dict) -> Dict: | |
| """ | |
| Process a single strain. | |
| Args: | |
| strain_data (dict): Data for a single strain | |
| Returns: | |
| dict: Processed strain data | |
| """ | |
| 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(self, strains: List[Dict]) -> Dict: | |
| """ | |
| Process multiple strains in batch. | |
| Args: | |
| strains (list): List of strain data dictionaries | |
| Returns: | |
| dict: Batch processing results | |
| """ | |
| 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: | |
| """ | |
| Get system debug status. | |
| Returns: | |
| dict: System debug information | |
| """ | |
| 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. | |
| Returns: | |
| dict: Health check results | |
| """ | |
| 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_to_json(data: Dict, filename: str): | |
| """Helper function to save results to JSON file""" | |
| path = Path("results") | |
| path.mkdir(exist_ok=True) | |
| with open(path / filename, 'w') as f: | |
| json.dump(data, f, indent=2) | |
| # Example usage | |
| if __name__ == "__main__": | |
| # Initialize API client | |
| api = StrainAPI( | |
| base_url="https://your-api-base-url", | |
| api_key="your-api-key" # Optional | |
| ) | |
| # Example: Process single strain | |
| single_strain = { | |
| "name": "Blue Dream", | |
| "type": "Hybrid", | |
| "thc": 18.0, | |
| "cbd": 0.1 | |
| } | |
| result = api.process_strain(single_strain) | |
| save_to_json(result, "single_strain_result.json") | |
| # Example: Process batch of strains | |
| batch_strains = [ | |
| { | |
| "name": "OG Kush", | |
| "type": "Indica", | |
| "thc": 23.0, | |
| "cbd": 0.3 | |
| }, | |
| { | |
| "name": "Sour Diesel", | |
| "type": "Sativa", | |
| "thc": 20.0, | |
| "cbd": 0.2 | |
| } | |
| ] | |
| batch_result = api.process_batch(batch_strains) | |
| save_to_json(batch_result, "batch_result.json") | |
| # Check system status | |
| debug_status = api.get_debug_status() | |
| save_to_json(debug_status, "debug_status.json") | |
| # Perform health check | |
| health_status = api.health_check() | |
| save_to_json(health_status, "health_status.json") |