Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,24 +12,41 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
| 16 |
-
self.
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
response = self.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
-
return response.choices[0].message.content.strip()
|
| 31 |
except Exception as e:
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
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 |
+
self.api_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
|
| 19 |
+
print("✅ BasicAgent with Gemini API initialized.")
|
| 20 |
+
|
| 21 |
def __call__(self, question: str) -> str:
|
| 22 |
+
print(f"🔍 Обрабатывается вопрос: {question[:50]}...")
|
| 23 |
+
|
| 24 |
+
headers = {
|
| 25 |
+
"Content-Type": "application/json",
|
| 26 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
payload = {
|
| 30 |
+
"contents": [
|
| 31 |
+
{
|
| 32 |
+
"parts": [
|
| 33 |
+
{"text": question}
|
| 34 |
+
]
|
| 35 |
+
}
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
try:
|
| 40 |
+
response = requests.post(self.api_url, headers=headers, json=payload, timeout=20)
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
result = response.json()
|
| 43 |
+
# Получение текста ответа
|
| 44 |
+
reply = result["candidates"][0]["content"]["parts"][0]["text"]
|
| 45 |
+
print(f"✅ Ответ: {reply[:50]}...")
|
| 46 |
+
return reply
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
+
print(f"⚠️ Ошибка при вызове Gemini API: {e}")
|
| 49 |
+
return "Ошибка при получении ответа от Gemini."
|
| 50 |
|
| 51 |
|
| 52 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|