FinnNick commited on
Commit
4289245
·
verified ·
1 Parent(s): 7a49d66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -15
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
- from openai import OpenAI
16
- self.llm = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
17
- print("BasicAgent initialized.")
 
 
 
18
  def __call__(self, question: str) -> str:
19
- prompt = f"""Answer the following concisely and exactly:
20
- {question}"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  try:
22
- response = self.llm.chat.completions.create(
23
- model="gpt-4o",
24
- messages=[
25
- {"role": "system", "content": "You are an expert AI assistant."},
26
- {"role": "user", "content": prompt}
27
- ],
28
- temperature=0.2,
29
- )
30
- return response.choices[0].message.content.strip()
31
  except Exception as e:
32
- return f"ERROR: {e}"
 
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):