Spaces:
Runtime error
Runtime error
File size: 3,409 Bytes
e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 e669702 55b65d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 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)} |