lukmanaj commited on
Commit
a559e2a
·
verified ·
1 Parent(s): fee6bc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -21,25 +21,47 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
 
22
  class BasicAgent:
23
  def __init__(self):
24
- print("CustomAgent (using Gemini) initialized.")
25
-
26
- # Setup Gemini API
27
- genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
28
- self.model = genai.GenerativeModel('gemini-pro')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  def __call__(self, question: str) -> str:
31
  print(f"Agent received question (first 50 chars): {question[:50]}...")
32
 
33
  try:
34
- response = self.model.generate_content(
35
- f"Answer the following question clearly and concisely: {question}"
 
 
36
  )
37
  answer = response.text.strip()
38
  print(f"Agent returning answer (first 100 chars): {answer[:100]}")
39
  return answer
40
  except Exception as e:
41
- print(f"Error during Gemini API call: {e}")
42
- return "Sorry, there was an error generating the answer."
43
 
44
  def run_and_submit_all( profile: gr.OAuthProfile | None):
45
  """
 
21
 
22
  class BasicAgent:
23
  def __init__(self):
24
+ print("CustomAgent (using Gemini 2.0) initialized.")
25
+
26
+ # Set the environment variable (important for Hugging Face Spaces)
27
+ api_key = os.environ.get("GEMINI_API_KEY")
28
+ if not api_key:
29
+ raise ValueError("GEMINI_API_KEY not found in environment variables.")
30
+
31
+ os.environ["GOOGLE_API_KEY"] = api_key # Needed for google-genai Client
32
+
33
+ # Initialize the Gemini client
34
+ self.client = genai.Client()
35
+
36
+ # Set model ID (you can choose another if needed)
37
+ self.model_id = "gemini-2.0-flash-exp"
38
+
39
+ # (Optional) Define generation config
40
+ self.generation_config = types.GenerateContentConfig(
41
+ temperature=0.4,
42
+ top_p=0.95,
43
+ top_k=20,
44
+ candidate_count=1,
45
+ seed=5,
46
+ presence_penalty=0.0,
47
+ frequency_penalty=0.0,
48
+ )
49
 
50
  def __call__(self, question: str) -> str:
51
  print(f"Agent received question (first 50 chars): {question[:50]}...")
52
 
53
  try:
54
+ response = self.client.models.generate_content(
55
+ model=self.model_id,
56
+ contents=f"Answer the following question clearly and concisely: {question}",
57
+ config=self.generation_config
58
  )
59
  answer = response.text.strip()
60
  print(f"Agent returning answer (first 100 chars): {answer[:100]}")
61
  return answer
62
  except Exception as e:
63
+ print(f"Error during Gemini API call: {str(e)}")
64
+ return f"Error: {str(e)}"
65
 
66
  def run_and_submit_all( profile: gr.OAuthProfile | None):
67
  """