Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,32 +23,36 @@ except Exception as e:
|
|
| 23 |
|
| 24 |
def call_huggingface_api(prompt, api_key):
|
| 25 |
"""
|
| 26 |
-
Hugging Face Serverless Inference API (
|
| 27 |
OpenAI uyumlu chat/completions formatı kullanır.
|
| 28 |
-
Birden fazla model deneyerek en stabil olanı bulur.
|
| 29 |
"""
|
| 30 |
if not api_key:
|
| 31 |
return "⚠️ HF Token girilmedi."
|
| 32 |
if not api_key.startswith("hf_"):
|
| 33 |
return "⚠️ Token 'hf_' ile başlamalıdır."
|
| 34 |
|
| 35 |
-
# Sırayla deneyeceğimiz modeller
|
| 36 |
models = [
|
|
|
|
|
|
|
| 37 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 38 |
-
"meta-llama/Llama-3.2-3B-Instruct",
|
| 39 |
-
"HuggingFaceH4/zephyr-7b-beta",
|
| 40 |
"microsoft/Phi-3-mini-4k-instruct"
|
| 41 |
]
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
headers = {
|
| 44 |
"Authorization": f"Bearer {api_key}",
|
| 45 |
"Content-Type": "application/json"
|
| 46 |
}
|
| 47 |
|
| 48 |
for model_id in models:
|
| 49 |
-
# Yeni Router Endpoint (OpenAI uyumlu)
|
| 50 |
-
url = f"https://router.huggingface.co/hf-inference/models/{model_id}/v1/chat/completions"
|
| 51 |
-
|
| 52 |
payload = {
|
| 53 |
"model": model_id,
|
| 54 |
"messages": [
|
|
@@ -56,40 +60,55 @@ def call_huggingface_api(prompt, api_key):
|
|
| 56 |
{"role": "user", "content": prompt}
|
| 57 |
],
|
| 58 |
"max_tokens": 512,
|
| 59 |
-
"temperature": 0.3
|
| 60 |
-
"stream": False
|
| 61 |
}
|
| 62 |
|
| 63 |
try:
|
| 64 |
-
print(f"📡
|
| 65 |
-
response = requests.post(url, headers=headers, json=payload, timeout=
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if response.status_code == 200:
|
| 68 |
result = response.json()
|
|
|
|
| 69 |
if "choices" in result and len(result["choices"]) > 0:
|
| 70 |
return result["choices"][0]["message"]["content"].strip()
|
| 71 |
return f"❌ Beklenmedik yanıt: {result}"
|
| 72 |
|
| 73 |
-
elif response.status_code
|
| 74 |
-
print(f"⚠️ {model_id} yükleniyor
|
| 75 |
continue
|
| 76 |
|
| 77 |
elif response.status_code == 404:
|
| 78 |
-
print(f"⚠️ {model_id}
|
| 79 |
continue
|
| 80 |
|
| 81 |
elif response.status_code == 401:
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
else:
|
| 85 |
-
|
|
|
|
| 86 |
continue
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
-
print(f"
|
| 90 |
continue
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def summarize_with_api(text: str, api_key: str) -> str:
|
| 95 |
"""Metni özetler."""
|
|
|
|
| 23 |
|
| 24 |
def call_huggingface_api(prompt, api_key):
|
| 25 |
"""
|
| 26 |
+
Hugging Face Serverless Inference API (Router Endpoint).
|
| 27 |
OpenAI uyumlu chat/completions formatı kullanır.
|
|
|
|
| 28 |
"""
|
| 29 |
if not api_key:
|
| 30 |
return "⚠️ HF Token girilmedi."
|
| 31 |
if not api_key.startswith("hf_"):
|
| 32 |
return "⚠️ Token 'hf_' ile başlamalıdır."
|
| 33 |
|
| 34 |
+
# Sırayla deneyeceğimiz modeller
|
| 35 |
models = [
|
| 36 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
| 37 |
+
"meta-llama/Llama-3.3-70B-Instruct",
|
| 38 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
|
|
|
|
|
|
| 39 |
"microsoft/Phi-3-mini-4k-instruct"
|
| 40 |
]
|
| 41 |
|
| 42 |
+
# Doğru endpoint: /v1/chat/completions
|
| 43 |
+
url = "https://router.huggingface.co/v1/chat/completions"
|
| 44 |
+
|
| 45 |
+
print("=" * 50)
|
| 46 |
+
print(f"🔗 API URL: {url}")
|
| 47 |
+
print(f"🔑 Token: {api_key[:10]}...")
|
| 48 |
+
print("=" * 50)
|
| 49 |
+
|
| 50 |
headers = {
|
| 51 |
"Authorization": f"Bearer {api_key}",
|
| 52 |
"Content-Type": "application/json"
|
| 53 |
}
|
| 54 |
|
| 55 |
for model_id in models:
|
|
|
|
|
|
|
|
|
|
| 56 |
payload = {
|
| 57 |
"model": model_id,
|
| 58 |
"messages": [
|
|
|
|
| 60 |
{"role": "user", "content": prompt}
|
| 61 |
],
|
| 62 |
"max_tokens": 512,
|
| 63 |
+
"temperature": 0.3
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
try:
|
| 67 |
+
print(f"\n📡 [{model_id}] İstek gönderiliyor...")
|
| 68 |
+
response = requests.post(url, headers=headers, json=payload, timeout=90)
|
| 69 |
+
|
| 70 |
+
print(f"📥 [{model_id}] HTTP Status: {response.status_code}")
|
| 71 |
|
| 72 |
if response.status_code == 200:
|
| 73 |
result = response.json()
|
| 74 |
+
print(f"✅ [{model_id}] BAŞARILI!")
|
| 75 |
if "choices" in result and len(result["choices"]) > 0:
|
| 76 |
return result["choices"][0]["message"]["content"].strip()
|
| 77 |
return f"❌ Beklenmedik yanıt: {result}"
|
| 78 |
|
| 79 |
+
elif response.status_code in [503, 529]:
|
| 80 |
+
print(f"⚠️ [{model_id}] Model meşgul/yükleniyor...")
|
| 81 |
continue
|
| 82 |
|
| 83 |
elif response.status_code == 404:
|
| 84 |
+
print(f"⚠️ [{model_id}] Model bulunamadı")
|
| 85 |
continue
|
| 86 |
|
| 87 |
elif response.status_code == 401:
|
| 88 |
+
print(f"❌ [{model_id}] YETKİSİZ! Token kontrol edin.")
|
| 89 |
+
print(f" Yanıt: {response.text[:300]}")
|
| 90 |
+
return "❌ Token geçersiz. Lütfen yeni bir 'Read' token oluşturun."
|
| 91 |
+
|
| 92 |
+
elif response.status_code == 422:
|
| 93 |
+
print(f"⚠️ [{model_id}] Format hatası: {response.text[:200]}")
|
| 94 |
+
continue
|
| 95 |
|
| 96 |
else:
|
| 97 |
+
error_text = response.text[:300] if len(response.text) > 300 else response.text
|
| 98 |
+
print(f"❌ [{model_id}] HATA ({response.status_code}): {error_text}")
|
| 99 |
continue
|
| 100 |
|
| 101 |
+
except requests.exceptions.Timeout:
|
| 102 |
+
print(f"⏰ [{model_id}] Zaman aşımı (90sn)")
|
| 103 |
+
continue
|
| 104 |
except Exception as e:
|
| 105 |
+
print(f"💥 [{model_id}] İstisna: {e}")
|
| 106 |
continue
|
| 107 |
|
| 108 |
+
print("\n" + "=" * 50)
|
| 109 |
+
print("❌ TÜM MODELLER BAŞARISIZ!")
|
| 110 |
+
print("=" * 50)
|
| 111 |
+
return "❌ Tüm modeller başarısız. Token'ınızı kontrol edin veya daha sonra deneyin."
|
| 112 |
|
| 113 |
def summarize_with_api(text: str, api_key: str) -> str:
|
| 114 |
"""Metni özetler."""
|