Commit
·
43493ee
1
Parent(s):
ffe5aac
........
Browse files
app.py
CHANGED
@@ -19,12 +19,10 @@ import tempfile
|
|
19 |
|
20 |
try:
|
21 |
import google.genai as genai
|
22 |
-
from google.genai import types
|
23 |
-
# Specific types are referenced via types.GenerationConfig etc. later
|
24 |
except ImportError:
|
25 |
genai = None
|
26 |
-
types = None
|
27 |
-
# Individual type hints will be set to None if types is None
|
28 |
GenerationConfig = None
|
29 |
HarmCategory = None
|
30 |
HarmBlockThreshold = None
|
@@ -32,14 +30,13 @@ except ImportError:
|
|
32 |
HarmProbability = None
|
33 |
print("WARNING: google-genai library not found. Install with: pip install google-genai")
|
34 |
|
35 |
-
# Assign specific types if 'types' was imported successfully, else None
|
36 |
if types:
|
37 |
GenerationConfig = types.GenerationConfig
|
38 |
HarmCategory = types.HarmCategory
|
39 |
HarmBlockThreshold = types.HarmBlockThreshold
|
40 |
FinishReason = types.FinishReason
|
41 |
HarmProbability = types.HarmProbability
|
42 |
-
else:
|
43 |
GenerationConfig = None
|
44 |
HarmCategory = None
|
45 |
HarmBlockThreshold = None
|
@@ -120,9 +117,9 @@ gaia_logger = logging.getLogger('gaia_agent_llm')
|
|
120 |
|
121 |
DEFAULT_API_URL = os.getenv("SCORING_API_URL", "https://agents-course-unit4-scoring.hf.space")
|
122 |
|
123 |
-
GOOGLE_CUSTOM_SEARCH_API_KEY = os.getenv("GOOGLE_API_KEY")
|
124 |
GOOGLE_CUSTOM_SEARCH_CSE_ID = os.getenv("GOOGLE_CSE_ID")
|
125 |
-
GOOGLE_GEMINI_API_KEY = os.getenv("GOOGLE_GEMINI_API_KEY")
|
126 |
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
127 |
|
128 |
AGENT_DEFAULT_TIMEOUT = 15
|
@@ -132,7 +129,6 @@ MAX_FILE_SIZE = 5 * 1024 * 1024
|
|
132 |
CSV_SAMPLE_ROWS = 10
|
133 |
MAX_FILE_CONTEXT_LENGTH = 10000
|
134 |
|
135 |
-
# Global instances for video analysis pipelines
|
136 |
video_object_detector_pipeline: Optional[Any] = None
|
137 |
video_vqa_pipeline: Optional[Any] = None
|
138 |
|
@@ -180,7 +176,6 @@ DEFAULT_RAG_CONFIG = {
|
|
180 |
'results': {'total_limit': 3, 'enrich_count': 2 }
|
181 |
}
|
182 |
|
183 |
-
# --- Helper functions for initializing video pipelines ---
|
184 |
def _get_video_object_detector():
|
185 |
global video_object_detector_pipeline, VIDEO_ANALYSIS_DEVICE
|
186 |
if video_object_detector_pipeline is None and hf_transformers_pipeline and torch:
|
@@ -780,13 +775,12 @@ class GaiaLevel1Agent:
|
|
780 |
def __init__(self, api_url: str = DEFAULT_API_URL):
|
781 |
self.api_url = api_url
|
782 |
self.genai_client: Optional[Any] = None
|
783 |
-
self.llm_model_name: str = 'gemini-2.5-flash-preview-05-20'
|
784 |
self.rag_pipeline = GeneralRAGPipeline(DEFAULT_RAG_CONFIG)
|
785 |
|
786 |
if genai and GOOGLE_GEMINI_API_KEY:
|
787 |
try:
|
788 |
# Corrected: Initialize client explicitly with the GOOGLE_GEMINI_API_KEY
|
789 |
-
# Remove genai.configure() as it's from the old SDK.
|
790 |
self.genai_client = genai.Client(api_key=GOOGLE_GEMINI_API_KEY)
|
791 |
gaia_logger.info(f"Google GenAI Client initialized successfully with GOOGLE_GEMINI_API_KEY. Will use model '{self.llm_model_name}'.")
|
792 |
except Exception as e:
|
@@ -898,7 +892,7 @@ class GaiaLevel1Agent:
|
|
898 |
'format': 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=480][ext=webm]+bestaudio[ext=webm]/best[height<=480][ext=mp4]/best[height<=480][ext=webm]/best[height<=480]',
|
899 |
'outtmpl': os.path.join(temp_dir, '%(id)s.%(ext)s'),
|
900 |
'quiet': True,
|
901 |
-
'max_filesize': 75 * 1024 * 1024,
|
902 |
'overwrites': True, 'noprogress': True, 'noplaylist': True, 'socket_timeout': 20,
|
903 |
'merge_output_format': 'mp4',
|
904 |
}
|
@@ -1081,7 +1075,6 @@ class GaiaLevel1Agent:
|
|
1081 |
default_model_answer = "Information not available in provided context"
|
1082 |
default_reasoning = "LLM processing failed or context insufficient."
|
1083 |
|
1084 |
-
# Check if genai client and necessary types are available
|
1085 |
if not self.genai_client or not types or not GenerationConfig or not FinishReason or not HarmCategory or not HarmBlockThreshold:
|
1086 |
gaia_logger.warning("Google GenAI Client or necessary types (GenerationConfig, HarmCategory, etc.) not available for answer formulation.")
|
1087 |
reasoning = "Google GenAI Client or its configuration components (types) not available for answer formulation."
|
@@ -1157,32 +1150,26 @@ class GaiaLevel1Agent:
|
|
1157 |
|
1158 |
|
1159 |
try:
|
1160 |
-
|
1161 |
-
current_gen_config_obj = GenerationConfig(temperature=0.1, top_p=0.95, max_output_tokens=1024)
|
1162 |
|
1163 |
-
# Prepare SafetySettings (list of types.SafetySetting objects)
|
1164 |
current_safety_settings_list_of_dicts = [
|
1165 |
-
{"category": HarmCategory.HARM_CATEGORY_HARASSMENT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1166 |
-
{"category": HarmCategory.HARM_CATEGORY_HATE_SPEECH, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1167 |
-
{"category": HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1168 |
-
{"category": HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1169 |
]
|
1170 |
-
sdk_safety_settings = []
|
1171 |
-
if types and HarmCategory and HarmBlockThreshold: # Ensure types are available
|
1172 |
-
for setting_dict in current_safety_settings_list_of_dicts:
|
1173 |
-
sdk_safety_settings.append(types.SafetySetting(category=setting_dict["category"], threshold=setting_dict["threshold"]))
|
1174 |
|
1175 |
-
#
|
1176 |
-
|
1177 |
-
tool_config_for_api = types.GenerateContentConfig(
|
1178 |
generation_config=current_gen_config_obj,
|
1179 |
safety_settings=sdk_safety_settings
|
1180 |
)
|
1181 |
|
1182 |
response = self.genai_client.models.generate_content(
|
1183 |
-
model=self.llm_model_name,
|
1184 |
-
contents=[final_prompt],
|
1185 |
-
config=
|
1186 |
)
|
1187 |
|
1188 |
if hasattr(response, 'prompt_feedback') and response.prompt_feedback.block_reason:
|
@@ -1196,11 +1183,10 @@ class GaiaLevel1Agent:
|
|
1196 |
return {"model_answer": "LLM Error: No response", "reasoning_trace": "LLM did not provide any response candidates."}
|
1197 |
|
1198 |
candidate = response.candidates[0]
|
1199 |
-
# Ensure FinishReason enum is available for comparison
|
1200 |
if types and FinishReason and candidate.finish_reason != FinishReason.STOP:
|
1201 |
reason_name = candidate.finish_reason.name if hasattr(candidate.finish_reason, 'name') else str(candidate.finish_reason)
|
1202 |
safety_ratings_str = ""
|
1203 |
-
if candidate.safety_ratings and types and HarmProbability:
|
1204 |
relevant_ratings = [
|
1205 |
f"{sr.category.name.split('_')[-1] if hasattr(sr.category, 'name') else 'CAT?'}: {(sr.probability.name if hasattr(sr.probability, 'name') else 'PROB?')}"
|
1206 |
for sr in candidate.safety_ratings if (hasattr(sr,'blocked') and sr.blocked) or (hasattr(sr,'probability') and sr.probability.value >= HarmProbability.MEDIUM.value)
|
@@ -1217,7 +1203,7 @@ class GaiaLevel1Agent:
|
|
1217 |
"model_answer": user_message,
|
1218 |
"reasoning_trace": f"LLM generation stopped. Reason: {reason_name}. " + (f"Details: {safety_ratings_str}" if safety_ratings_str else "")
|
1219 |
}
|
1220 |
-
elif not (types and FinishReason):
|
1221 |
gaia_logger.warning("FinishReason type not available, cannot check candidate finish reason properly.")
|
1222 |
|
1223 |
|
@@ -1482,4 +1468,6 @@ if __name__ == "__main__":
|
|
1482 |
|
1483 |
|
1484 |
print("-"*(60 + len(" GAIA Level 1 Agent - RAG, FileProc, Video Analysis ")) + "\n")
|
1485 |
-
|
|
|
|
|
|
19 |
|
20 |
try:
|
21 |
import google.genai as genai
|
22 |
+
from google.genai import types
|
|
|
23 |
except ImportError:
|
24 |
genai = None
|
25 |
+
types = None
|
|
|
26 |
GenerationConfig = None
|
27 |
HarmCategory = None
|
28 |
HarmBlockThreshold = None
|
|
|
30 |
HarmProbability = None
|
31 |
print("WARNING: google-genai library not found. Install with: pip install google-genai")
|
32 |
|
|
|
33 |
if types:
|
34 |
GenerationConfig = types.GenerationConfig
|
35 |
HarmCategory = types.HarmCategory
|
36 |
HarmBlockThreshold = types.HarmBlockThreshold
|
37 |
FinishReason = types.FinishReason
|
38 |
HarmProbability = types.HarmProbability
|
39 |
+
else:
|
40 |
GenerationConfig = None
|
41 |
HarmCategory = None
|
42 |
HarmBlockThreshold = None
|
|
|
117 |
|
118 |
DEFAULT_API_URL = os.getenv("SCORING_API_URL", "https://agents-course-unit4-scoring.hf.space")
|
119 |
|
120 |
+
GOOGLE_CUSTOM_SEARCH_API_KEY = os.getenv("GOOGLE_API_KEY")
|
121 |
GOOGLE_CUSTOM_SEARCH_CSE_ID = os.getenv("GOOGLE_CSE_ID")
|
122 |
+
GOOGLE_GEMINI_API_KEY = os.getenv("GOOGLE_GEMINI_API_KEY")
|
123 |
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
124 |
|
125 |
AGENT_DEFAULT_TIMEOUT = 15
|
|
|
129 |
CSV_SAMPLE_ROWS = 10
|
130 |
MAX_FILE_CONTEXT_LENGTH = 10000
|
131 |
|
|
|
132 |
video_object_detector_pipeline: Optional[Any] = None
|
133 |
video_vqa_pipeline: Optional[Any] = None
|
134 |
|
|
|
176 |
'results': {'total_limit': 3, 'enrich_count': 2 }
|
177 |
}
|
178 |
|
|
|
179 |
def _get_video_object_detector():
|
180 |
global video_object_detector_pipeline, VIDEO_ANALYSIS_DEVICE
|
181 |
if video_object_detector_pipeline is None and hf_transformers_pipeline and torch:
|
|
|
775 |
def __init__(self, api_url: str = DEFAULT_API_URL):
|
776 |
self.api_url = api_url
|
777 |
self.genai_client: Optional[Any] = None
|
778 |
+
self.llm_model_name: str = 'gemini-2.5-flash-preview-05-20'
|
779 |
self.rag_pipeline = GeneralRAGPipeline(DEFAULT_RAG_CONFIG)
|
780 |
|
781 |
if genai and GOOGLE_GEMINI_API_KEY:
|
782 |
try:
|
783 |
# Corrected: Initialize client explicitly with the GOOGLE_GEMINI_API_KEY
|
|
|
784 |
self.genai_client = genai.Client(api_key=GOOGLE_GEMINI_API_KEY)
|
785 |
gaia_logger.info(f"Google GenAI Client initialized successfully with GOOGLE_GEMINI_API_KEY. Will use model '{self.llm_model_name}'.")
|
786 |
except Exception as e:
|
|
|
892 |
'format': 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=480][ext=webm]+bestaudio[ext=webm]/best[height<=480][ext=mp4]/best[height<=480][ext=webm]/best[height<=480]',
|
893 |
'outtmpl': os.path.join(temp_dir, '%(id)s.%(ext)s'),
|
894 |
'quiet': True,
|
895 |
+
'max_filesize': 75 * 1024 * 1024,
|
896 |
'overwrites': True, 'noprogress': True, 'noplaylist': True, 'socket_timeout': 20,
|
897 |
'merge_output_format': 'mp4',
|
898 |
}
|
|
|
1075 |
default_model_answer = "Information not available in provided context"
|
1076 |
default_reasoning = "LLM processing failed or context insufficient."
|
1077 |
|
|
|
1078 |
if not self.genai_client or not types or not GenerationConfig or not FinishReason or not HarmCategory or not HarmBlockThreshold:
|
1079 |
gaia_logger.warning("Google GenAI Client or necessary types (GenerationConfig, HarmCategory, etc.) not available for answer formulation.")
|
1080 |
reasoning = "Google GenAI Client or its configuration components (types) not available for answer formulation."
|
|
|
1150 |
|
1151 |
|
1152 |
try:
|
1153 |
+
current_gen_config_obj = types.GenerationConfig(temperature=0.1, top_p=0.95, max_output_tokens=1024)
|
|
|
1154 |
|
|
|
1155 |
current_safety_settings_list_of_dicts = [
|
1156 |
+
{"category": types.HarmCategory.HARM_CATEGORY_HARASSMENT, "threshold": types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1157 |
+
{"category": types.HarmCategory.HARM_CATEGORY_HATE_SPEECH, "threshold": types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1158 |
+
{"category": types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, "threshold": types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1159 |
+
{"category": types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, "threshold": types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
1160 |
]
|
1161 |
+
sdk_safety_settings = [types.SafetySetting(category=s["category"], threshold=s["threshold"]) for s in current_safety_settings_list_of_dicts]
|
|
|
|
|
|
|
1162 |
|
1163 |
+
# Main config object for generate_content, now as types.GenerateContentConfig
|
1164 |
+
api_call_config = types.GenerateContentConfig(
|
|
|
1165 |
generation_config=current_gen_config_obj,
|
1166 |
safety_settings=sdk_safety_settings
|
1167 |
)
|
1168 |
|
1169 |
response = self.genai_client.models.generate_content(
|
1170 |
+
model=self.llm_model_name,
|
1171 |
+
contents=[final_prompt],
|
1172 |
+
config=api_call_config
|
1173 |
)
|
1174 |
|
1175 |
if hasattr(response, 'prompt_feedback') and response.prompt_feedback.block_reason:
|
|
|
1183 |
return {"model_answer": "LLM Error: No response", "reasoning_trace": "LLM did not provide any response candidates."}
|
1184 |
|
1185 |
candidate = response.candidates[0]
|
|
|
1186 |
if types and FinishReason and candidate.finish_reason != FinishReason.STOP:
|
1187 |
reason_name = candidate.finish_reason.name if hasattr(candidate.finish_reason, 'name') else str(candidate.finish_reason)
|
1188 |
safety_ratings_str = ""
|
1189 |
+
if candidate.safety_ratings and types and HarmProbability:
|
1190 |
relevant_ratings = [
|
1191 |
f"{sr.category.name.split('_')[-1] if hasattr(sr.category, 'name') else 'CAT?'}: {(sr.probability.name if hasattr(sr.probability, 'name') else 'PROB?')}"
|
1192 |
for sr in candidate.safety_ratings if (hasattr(sr,'blocked') and sr.blocked) or (hasattr(sr,'probability') and sr.probability.value >= HarmProbability.MEDIUM.value)
|
|
|
1203 |
"model_answer": user_message,
|
1204 |
"reasoning_trace": f"LLM generation stopped. Reason: {reason_name}. " + (f"Details: {safety_ratings_str}" if safety_ratings_str else "")
|
1205 |
}
|
1206 |
+
elif not (types and FinishReason):
|
1207 |
gaia_logger.warning("FinishReason type not available, cannot check candidate finish reason properly.")
|
1208 |
|
1209 |
|
|
|
1468 |
|
1469 |
|
1470 |
print("-"*(60 + len(" GAIA Level 1 Agent - RAG, FileProc, Video Analysis ")) + "\n")
|
1471 |
+
# Removed ssr=False as it was causing a TypeError with the Gradio version in the environment
|
1472 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, debug=False, share=False)
|
1473 |
+
-
|