Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -12,12 +13,29 @@ 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 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
|
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
+
print("Initializing Gemini Agent...")
|
| 17 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 18 |
+
if not api_key:
|
| 19 |
+
raise ValueError("GOOGLE_API_KEY environment variable not set. Please set it in your Hugging Face Space secrets.")
|
| 20 |
+
|
| 21 |
+
genai.configure(api_key=api_key)
|
| 22 |
+
self.model = genai.GenerativeModel('gemini-pro')
|
| 23 |
+
print("Gemini Agent initialized successfully.")
|
| 24 |
+
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 27 |
+
try:
|
| 28 |
+
response = self.model.generate_content(question)
|
| 29 |
+
answer = response.text
|
| 30 |
+
print(f"Agent returning Gemini answer (first 50 chars): {answer[:50]}...")
|
| 31 |
+
return answer
|
| 32 |
+
except ValueError:
|
| 33 |
+
# This can happen if the response was blocked.
|
| 34 |
+
print("Response was blocked or content is not available.")
|
| 35 |
+
return "The response was blocked by the content filter."
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Error calling Gemini API: {e}")
|
| 38 |
+
return f"Error during Gemini API call: {e}"
|
| 39 |
|
| 40 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 41 |
"""
|