Spaces:
Runtime error
Runtime error
Download providers.py from Expanded-Repetition/ammo-card: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/Expanded-Repetition/ammo-card/resolve/main/providers.py
- Command line
-
hf download hf://spaces/Expanded-Repetition/ammo-card/providers.py
-
curl -L -o providers.py https://huggingface.co/spaces/Expanded-Repetition/ammo-card/resolve/main/providers.py
3.28 kB
| from __future__ import annotations | |
| from typing import Iterable | |
| DEFAULT_BASE_URL = "https://api.groq.com/openai/v1" | |
| def _client(api_key: str, base_url: str, timeout: float = 75.0): | |
| try: | |
| from openai import OpenAI | |
| except ImportError as exc: | |
| raise RuntimeError("مكتبة openai غير مثبتة. شغّل: pip install -r requirements.txt") from exc | |
| if not (api_key or "").strip(): | |
| raise ValueError("أدخل مفتاح API أولاً.") | |
| if not (base_url or "").strip().startswith(("https://", "http://")): | |
| raise ValueError("عنوان API يجب أن يبدأ بـ https:// أو http://") | |
| return OpenAI(api_key=api_key.strip(), base_url=base_url.strip().rstrip("/"), timeout=timeout, max_retries=1) | |
| def list_models(api_key: str, base_url: str) -> list[str]: | |
| """Ask the provider for its actual model IDs; never invent a model list.""" | |
| client = _client(api_key, base_url, timeout=30) | |
| response = client.models.list() | |
| ids = sorted({item.id for item in response.data if getattr(item, "id", None)}) | |
| if not ids: | |
| raise RuntimeError("المزوّد لم يُرجع قائمة نماذج. اكتب Model ID يدوياً من لوحة المزوّد.") | |
| return ids | |
| def test_connection(api_key: str, base_url: str, model: str) -> str: | |
| if not (model or "").strip(): | |
| raise ValueError("اختر نموذجاً أو اكتب Model ID قبل الاختبار.") | |
| client = _client(api_key, base_url, timeout=30) | |
| response = client.chat.completions.create( | |
| model=model.strip(), | |
| messages=[{"role": "user", "content": "Reply with exactly: AMO_OK"}], | |
| max_tokens=16, | |
| temperature=0, | |
| ) | |
| text = (response.choices[0].message.content or "").strip() | |
| return f"تم الاتصال بالنموذج `{model}`. الرد التجريبي: {text[:120]}" | |
| def stream_chat( | |
| api_key: str, | |
| base_url: str, | |
| model: str, | |
| messages: list[dict], | |
| temperature: float = 0.3, | |
| ) -> Iterable[str]: | |
| if not (model or "").strip(): | |
| raise ValueError("اكتب Model ID من مزوّد الخدمة.") | |
| client = _client(api_key, base_url) | |
| stream = client.chat.completions.create( | |
| model=model.strip(), | |
| messages=messages, | |
| temperature=max(0.0, min(float(temperature), 1.0)), | |
| stream=True, | |
| ) | |
| for chunk in stream: | |
| if not getattr(chunk, "choices", None): | |
| continue | |
| delta = chunk.choices[0].delta | |
| piece = getattr(delta, "content", None) | |
| if isinstance(piece, str) and piece: | |
| yield piece | |
| def complete( | |
| api_key: str, | |
| base_url: str, | |
| model: str, | |
| messages: list[dict], | |
| temperature: float = 0.2, | |
| ) -> str: | |
| if not (model or "").strip(): | |
| raise ValueError("اكتب Model ID من مزوّد الخدمة.") | |
| client = _client(api_key, base_url, timeout=120) | |
| response = client.chat.completions.create( | |
| model=model.strip(), messages=messages, | |
| temperature=max(0.0, min(float(temperature), 1.0)), | |
| ) | |
| content = response.choices[0].message.content | |
| if not isinstance(content, str) or not content.strip(): | |
| raise RuntimeError("المزوّد أرجع استجابة فارغة.") | |
| return content.strip() | |