Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,26 +15,20 @@ class BasicAgent:
|
|
| 15 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 16 |
if not self.api_key:
|
| 17 |
raise ValueError("❌ Переменная окружения GEMINI_API_KEY не найдена.")
|
| 18 |
-
|
| 19 |
-
|
| 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"
|
| 27 |
|
| 28 |
-
headers = {
|
| 29 |
-
"Content-Type": "application/json"
|
| 30 |
-
}
|
| 31 |
|
| 32 |
payload = {
|
| 33 |
"contents": [
|
| 34 |
{
|
| 35 |
-
"parts": [
|
| 36 |
-
{"text": question}
|
| 37 |
-
]
|
| 38 |
}
|
| 39 |
]
|
| 40 |
}
|
|
@@ -42,22 +36,20 @@ class BasicAgent:
|
|
| 42 |
try:
|
| 43 |
response = requests.post(self.api_url, headers=headers, json=payload, timeout=30)
|
| 44 |
response.raise_for_status()
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
print(f"✅ Ответ: {answer[:80]}")
|
| 50 |
return answer
|
| 51 |
|
| 52 |
-
except requests.exceptions.
|
| 53 |
-
print(
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
-
print(
|
| 58 |
-
|
| 59 |
-
print("Ответ сервера:", response.text[:500])
|
| 60 |
-
return "Ошибка при получении ответа от Gemini."
|
| 61 |
|
| 62 |
|
| 63 |
|
|
|
|
| 15 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 16 |
if not self.api_key:
|
| 17 |
raise ValueError("❌ Переменная окружения GEMINI_API_KEY не найдена.")
|
| 18 |
+
|
| 19 |
+
self.model_name = "gemini-1.5-pro"
|
|
|
|
| 20 |
self.api_url = f"https://generativelanguage.googleapis.com/v1/models/{self.model_name}:generateContent?key={self.api_key}"
|
| 21 |
+
print(f"✅ Gemini API URL: {self.api_url}")
|
|
|
|
| 22 |
|
| 23 |
def __call__(self, question: str) -> str:
|
| 24 |
+
print(f"🤖 Вопрос: {question[:80]}")
|
| 25 |
|
| 26 |
+
headers = {"Content-Type": "application/json"}
|
|
|
|
|
|
|
| 27 |
|
| 28 |
payload = {
|
| 29 |
"contents": [
|
| 30 |
{
|
| 31 |
+
"parts": [{"text": question}]
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
]
|
| 34 |
}
|
|
|
|
| 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("❌ Другая ошибка:", e)
|
| 52 |
+
return "Произошла ошибка при подключении к Gemini API."
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
|