Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,20 +14,21 @@ class BasicAgent:
|
|
| 14 |
def __init__(self):
|
| 15 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 16 |
if not self.api_key:
|
| 17 |
-
raise ValueError("❌ Переменная
|
| 18 |
-
|
| 19 |
-
self.
|
| 20 |
-
self.api_url = f"https://generativelanguage.googleapis.com/v1/models/{self.
|
| 21 |
-
print(f"✅ Gemini API
|
| 22 |
|
| 23 |
def __call__(self, question: str) -> str:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
payload = {
|
| 29 |
"contents": [
|
| 30 |
{
|
|
|
|
| 31 |
"parts": [{"text": question}]
|
| 32 |
}
|
| 33 |
]
|
|
@@ -35,21 +36,26 @@ class BasicAgent:
|
|
| 35 |
|
| 36 |
try:
|
| 37 |
response = requests.post(self.api_url, headers=headers, json=payload, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
response.raise_for_status()
|
| 39 |
|
| 40 |
data = response.json()
|
| 41 |
answer = data["candidates"][0]["content"]["parts"][0]["text"]
|
| 42 |
-
print(f"✅ Ответ Gemini: {answer[:100]}")
|
| 43 |
return answer
|
| 44 |
|
| 45 |
-
except requests.exceptions.HTTPError as e:
|
| 46 |
-
print("❌ HTTP ошибка:", e)
|
| 47 |
-
print("Ответ сервера:", response.text[:500])
|
| 48 |
-
return f"Ошибка HTTP: {e}"
|
| 49 |
-
|
| 50 |
except Exception as e:
|
| 51 |
-
print("❌
|
| 52 |
-
return "
|
| 53 |
|
| 54 |
|
| 55 |
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 16 |
if not self.api_key:
|
| 17 |
+
raise ValueError("❌ Переменная GEMINI_API_KEY не найдена. Установи её в Hugging Face Secrets.")
|
| 18 |
+
|
| 19 |
+
self.model = "gemini-1.5-pro"
|
| 20 |
+
self.api_url = f"https://generativelanguage.googleapis.com/v1/models/{self.model}:generateContent?key={self.api_key}"
|
| 21 |
+
print(f"✅ Используется Gemini API: {self.api_url}")
|
| 22 |
|
| 23 |
def __call__(self, question: str) -> str:
|
| 24 |
+
headers = {
|
| 25 |
+
"Content-Type": "application/json"
|
| 26 |
+
}
|
| 27 |
|
| 28 |
payload = {
|
| 29 |
"contents": [
|
| 30 |
{
|
| 31 |
+
"role": "user",
|
| 32 |
"parts": [{"text": question}]
|
| 33 |
}
|
| 34 |
]
|
|
|
|
| 36 |
|
| 37 |
try:
|
| 38 |
response = requests.post(self.api_url, headers=headers, json=payload, timeout=30)
|
| 39 |
+
|
| 40 |
+
if response.status_code == 429:
|
| 41 |
+
print("❗️Превышен лимит запросов (429). Пауза 10 секунд...")
|
| 42 |
+
time.sleep(10)
|
| 43 |
+
return "Превышен лимит запросов. Попробуйте позже."
|
| 44 |
+
|
| 45 |
+
if response.status_code == 400:
|
| 46 |
+
print("❌ Ошибка 400. Проверь формат запроса.")
|
| 47 |
+
print("Ответ:", response.text[:500])
|
| 48 |
+
return "Ошибка 400: Неверный формат запроса."
|
| 49 |
+
|
| 50 |
response.raise_for_status()
|
| 51 |
|
| 52 |
data = response.json()
|
| 53 |
answer = data["candidates"][0]["content"]["parts"][0]["text"]
|
|
|
|
| 54 |
return answer
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
+
print("❌ Ошибка при вызове Gemini:", e)
|
| 58 |
+
return f"Ошибка при вызове Gemini API: {e}"
|
| 59 |
|
| 60 |
|
| 61 |
|