Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,16 @@ class BasicAgent:
|
|
| 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 не
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def __call__(self, question: str) -> str:
|
| 23 |
-
print(f"
|
| 24 |
|
| 25 |
headers = {
|
| 26 |
"Content-Type": "application/json"
|
|
@@ -37,15 +40,23 @@ class BasicAgent:
|
|
| 37 |
}
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
response = requests.post(self.api_url, headers=headers, json=payload, timeout=
|
| 41 |
response.raise_for_status()
|
| 42 |
result = response.json()
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"
|
| 48 |
-
|
|
|
|
| 49 |
return "Ошибка при получении ответа от Gemini."
|
| 50 |
|
| 51 |
|
|
|
|
| 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 не найдена.")
|
| 18 |
+
|
| 19 |
+
# Модель и версия API
|
| 20 |
+
self.model_name = "gemini-1.5-pro" # Gemini 2.5 = Gemini 1.5 Pro
|
| 21 |
+
self.api_url = f"https://generativelanguage.googleapis.com/v1/models/{self.model_name}:generateContent?key={self.api_key}"
|
| 22 |
+
|
| 23 |
+
print(f"✅ BasicAgent инициализирован с моделью: {self.model_name}")
|
| 24 |
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
+
print(f"🧠 Вопрос получен: {question[:80]}")
|
| 27 |
|
| 28 |
headers = {
|
| 29 |
"Content-Type": "application/json"
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
try:
|
| 43 |
+
response = requests.post(self.api_url, headers=headers, json=payload, timeout=30)
|
| 44 |
response.raise_for_status()
|
| 45 |
result = response.json()
|
| 46 |
+
|
| 47 |
+
# Извлечение текста ответа
|
| 48 |
+
answer = result["candidates"][0]["content"]["parts"][0]["text"]
|
| 49 |
+
print(f"✅ Ответ: {answer[:80]}")
|
| 50 |
+
return answer
|
| 51 |
+
|
| 52 |
+
except requests.exceptions.RequestException as e:
|
| 53 |
+
print(f"❌ Ошибка HTTP: {e}")
|
| 54 |
+
return "Ошибка при подключении к Gemini API."
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
print(f"❌ Общая ошибка: {e}")
|
| 58 |
+
if 'response' in locals():
|
| 59 |
+
print("Ответ сервера:", response.text[:500])
|
| 60 |
return "Ошибка при получении ответа от Gemini."
|
| 61 |
|
| 62 |
|