Spaces:
Runtime error
Runtime error
| import requests | |
| import io | |
| import base64 | |
| from PIL import Image | |
| from typing import Dict, Any, Optional | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| class DermaScanAPIClient: | |
| """Client للتواصل مع الـ API الخارجية (https://omarelrayes-api.hf.space/)""" | |
| def __init__(self, base_url: str = "https://omarelrayes-api.hf.space"): | |
| self.base_url = base_url.rstrip('/') | |
| self.timeout = 120 | |
| def health_check(self) -> bool: | |
| """فحص حالة الـ API الخارجية""" | |
| try: | |
| response = requests.get(f"{self.base_url}/health", timeout=10) | |
| return response.status_code == 200 | |
| except Exception as e: | |
| logger.error(f"Health check failed: {e}") | |
| return False | |
| def set_role(self, thread_id: str, role: str) -> Dict[str, Any]: | |
| """تعيين الدور (patient/doctor)""" | |
| try: | |
| response = requests.post( | |
| f"{self.base_url}/set-role", | |
| json={"thread_id": thread_id, "role": role}, | |
| timeout=30 | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except Exception as e: | |
| logger.error(f"set_role failed: {e}") | |
| return {"ok": False, "error": str(e)} | |
| def upload_and_analyze_image(self, image: Image.Image, thread_id: str) -> Dict[str, Any]: | |
| """رفع صورة وتحليلها عبر الـ API الخارجية""" | |
| try: | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format='PNG') | |
| img_byte_arr.seek(0) | |
| files = {'file': ('image.png', img_byte_arr, 'image/png')} | |
| data = {'thread_id': thread_id} | |
| response = requests.post( | |
| f"{self.base_url}/analyze-image", | |
| files=files, | |
| data=data, | |
| timeout=self.timeout | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.Timeout: | |
| return {"ok": False, "error": "API request timed out"} | |
| except requests.exceptions.RequestException as e: | |
| return {"ok": False, "error": f"API request failed: {str(e)}"} | |
| except Exception as e: | |
| logger.error(f"Unexpected error: {e}", exc_info=True) | |
| return {"ok": False, "error": str(e)} | |
| def book_appointment(self, thread_id: str, city: str = "") -> Dict[str, Any]: | |
| """حجز موعد""" | |
| try: | |
| response = requests.post( | |
| f"{self.base_url}/book-appointment", | |
| json={"thread_id": thread_id, "city": city}, | |
| timeout=30 | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except Exception as e: | |
| return {"ok": False, "error": str(e)} | |
| def get_image(self, name: str, thread_id: str) -> Dict[str, Any]: | |
| """الحصول على صورة من الجلسة""" | |
| try: | |
| response = requests.get( | |
| f"{self.base_url}/image", | |
| params={"name": name, "thread_id": thread_id}, | |
| timeout=30 | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except Exception as e: | |
| return {"ok": False, "error": str(e)} |