Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,8 @@ class NativeGeminiAgent:
|
|
| 22 |
|
| 23 |
self.api_url = api_url
|
| 24 |
|
| 25 |
-
#
|
|
|
|
| 26 |
google_search_retrieval = genai.protos.Tool(
|
| 27 |
google_search_retrieval=genai.protos.GoogleSearchRetrieval()
|
| 28 |
)
|
|
@@ -32,9 +33,7 @@ class NativeGeminiAgent:
|
|
| 32 |
self.model = genai.GenerativeModel(
|
| 33 |
model_name=self.model_name,
|
| 34 |
tools=[google_search_retrieval],
|
| 35 |
-
# A more direct prompt, trusting the model's native abilities
|
| 36 |
system_instruction="You are a world-class problem solver. Your goal is to answer the user's question accurately. Use your tools and reasoning abilities to provide a definitive answer.",
|
| 37 |
-
# Safety settings dialed down to allow answering controversial topics if they appear in GAIA
|
| 38 |
safety_settings={
|
| 39 |
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
| 40 |
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
|
@@ -55,8 +54,6 @@ class NativeGeminiAgent:
|
|
| 55 |
def __call__(self, question: str, task_id: str) -> str:
|
| 56 |
print(f"\n{'='*20}\nProcessing Task ID: {task_id}")
|
| 57 |
|
| 58 |
-
# --- This is the core of the new multi-modal logic ---
|
| 59 |
-
# We build a list of "parts" to send to the model, not just a string.
|
| 60 |
prompt_parts = [question]
|
| 61 |
|
| 62 |
# 1. Automatically find and add any URLs from the question text
|
|
@@ -64,17 +61,17 @@ class NativeGeminiAgent:
|
|
| 64 |
if urls_in_question:
|
| 65 |
for url in urls_in_question:
|
| 66 |
print(f"Found URL in question: {url}")
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
# 2. Check for and add any associated files from the GAIA server
|
| 70 |
file_url = f"{self.api_url}/files/{task_id}"
|
| 71 |
if self._check_if_file_exists(file_url):
|
| 72 |
print(f"Found associated file, adding URL: {file_url}")
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
mime_type = "
|
| 76 |
-
if '.
|
| 77 |
-
if '.txt' in file_url: mime_type = "text/plain"
|
| 78 |
prompt_parts.append(genai.Part.from_uri(uri=file_url, mime_type=mime_type))
|
| 79 |
else:
|
| 80 |
print("No associated file found for this task.")
|
|
@@ -82,10 +79,9 @@ class NativeGeminiAgent:
|
|
| 82 |
print(f"Sending {len(prompt_parts)} parts to the model.")
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
response = self.model.generate_content(prompt_parts)
|
| 87 |
|
| 88 |
-
# The grounding feature may add citations. We
|
| 89 |
final_answer = re.sub(r'\[\d+\]', '', response.text).strip()
|
| 90 |
print(f"Model generated answer: {final_answer}")
|
| 91 |
return final_answer
|
|
@@ -94,18 +90,17 @@ class NativeGeminiAgent:
|
|
| 94 |
print(f"An error occurred while calling the Gemini API: {e}")
|
| 95 |
return f"AGENT_ERROR: Could not get a response from the model. Details: {e}"
|
| 96 |
|
| 97 |
-
# --- Main run_and_submit_all function
|
| 98 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 99 |
space_id = os.getenv("SPACE_ID")
|
| 100 |
if not profile: return "Please Login to Hugging Face with the button.", None
|
| 101 |
username = f"{profile.username}"
|
| 102 |
|
| 103 |
-
|
| 104 |
if not gemini_key: return "CRITICAL ERROR: GEMINI_API_KEY not found in Space secrets.", None
|
| 105 |
|
| 106 |
api_url = DEFAULT_API_URL
|
| 107 |
try:
|
| 108 |
-
# We no longer need the Perplexity key for the agent
|
| 109 |
agent = NativeGeminiAgent(gemini_api_key=gemini_key, api_url=api_url)
|
| 110 |
questions_data = requests.get(f"{api_url}/questions", timeout=15).json()
|
| 111 |
except Exception as e: return f"Error during setup: {e}", None
|
|
@@ -123,7 +118,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 123 |
print(error_message)
|
| 124 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_message})
|
| 125 |
|
| 126 |
-
# API calls are now fewer but more complex. A delay is still wise.
|
| 127 |
print(f"--- Waiting for 10 seconds before next question... ---")
|
| 128 |
time.sleep(10)
|
| 129 |
|
|
|
|
| 22 |
|
| 23 |
self.api_url = api_url
|
| 24 |
|
| 25 |
+
# --- CORRECTED INITIALIZATION ---
|
| 26 |
+
# Enable native grounding with Google Search. No parameters are needed.
|
| 27 |
google_search_retrieval = genai.protos.Tool(
|
| 28 |
google_search_retrieval=genai.protos.GoogleSearchRetrieval()
|
| 29 |
)
|
|
|
|
| 33 |
self.model = genai.GenerativeModel(
|
| 34 |
model_name=self.model_name,
|
| 35 |
tools=[google_search_retrieval],
|
|
|
|
| 36 |
system_instruction="You are a world-class problem solver. Your goal is to answer the user's question accurately. Use your tools and reasoning abilities to provide a definitive answer.",
|
|
|
|
| 37 |
safety_settings={
|
| 38 |
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
| 39 |
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
|
|
|
| 54 |
def __call__(self, question: str, task_id: str) -> str:
|
| 55 |
print(f"\n{'='*20}\nProcessing Task ID: {task_id}")
|
| 56 |
|
|
|
|
|
|
|
| 57 |
prompt_parts = [question]
|
| 58 |
|
| 59 |
# 1. Automatically find and add any URLs from the question text
|
|
|
|
| 61 |
if urls_in_question:
|
| 62 |
for url in urls_in_question:
|
| 63 |
print(f"Found URL in question: {url}")
|
| 64 |
+
# Use a general MIME type that Gemini can often auto-detect for sources like YouTube
|
| 65 |
+
prompt_parts.append(genai.Part.from_uri(uri=url, mime_type="video/mp4"))
|
| 66 |
|
| 67 |
# 2. Check for and add any associated files from the GAIA server
|
| 68 |
file_url = f"{self.api_url}/files/{task_id}"
|
| 69 |
if self._check_if_file_exists(file_url):
|
| 70 |
print(f"Found associated file, adding URL: {file_url}")
|
| 71 |
+
# Simple MIME type guessing for common GAIA file types
|
| 72 |
+
mime_type = "image/jpeg" # Default
|
| 73 |
+
if file_url.endswith('.pdf'): mime_type = "application/pdf"
|
| 74 |
+
if file_url.endswith('.txt'): mime_type = "text/plain"
|
|
|
|
| 75 |
prompt_parts.append(genai.Part.from_uri(uri=file_url, mime_type=mime_type))
|
| 76 |
else:
|
| 77 |
print("No associated file found for this task.")
|
|
|
|
| 79 |
print(f"Sending {len(prompt_parts)} parts to the model.")
|
| 80 |
|
| 81 |
try:
|
| 82 |
+
response = self.model.generate_content(prompt_parts, request_options={'timeout': 120})
|
|
|
|
| 83 |
|
| 84 |
+
# The grounding feature may add citations. We remove them for the final answer.
|
| 85 |
final_answer = re.sub(r'\[\d+\]', '', response.text).strip()
|
| 86 |
print(f"Model generated answer: {final_answer}")
|
| 87 |
return final_answer
|
|
|
|
| 90 |
print(f"An error occurred while calling the Gemini API: {e}")
|
| 91 |
return f"AGENT_ERROR: Could not get a response from the model. Details: {e}"
|
| 92 |
|
| 93 |
+
# --- Main run_and_submit_all function ---
|
| 94 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 95 |
space_id = os.getenv("SPACE_ID")
|
| 96 |
if not profile: return "Please Login to Hugging Face with the button.", None
|
| 97 |
username = f"{profile.username}"
|
| 98 |
|
| 99 |
+
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 100 |
if not gemini_key: return "CRITICAL ERROR: GEMINI_API_KEY not found in Space secrets.", None
|
| 101 |
|
| 102 |
api_url = DEFAULT_API_URL
|
| 103 |
try:
|
|
|
|
| 104 |
agent = NativeGeminiAgent(gemini_api_key=gemini_key, api_url=api_url)
|
| 105 |
questions_data = requests.get(f"{api_url}/questions", timeout=15).json()
|
| 106 |
except Exception as e: return f"Error during setup: {e}", None
|
|
|
|
| 118 |
print(error_message)
|
| 119 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_message})
|
| 120 |
|
|
|
|
| 121 |
print(f"--- Waiting for 10 seconds before next question... ---")
|
| 122 |
time.sleep(10)
|
| 123 |
|