Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,8 +38,9 @@ except ImportError: PIL_TESSERACT_AVAILABLE = False; print("WARNING: Pillow or P
|
|
| 38 |
try: import whisper; WHISPER_AVAILABLE = True
|
| 39 |
except ImportError: WHISPER_AVAILABLE = False; print("WARNING: OpenAI Whisper not found, Audio Transcription tool will be disabled.")
|
| 40 |
|
| 41 |
-
# Google GenAI (
|
| 42 |
from google.genai.types import HarmCategory, HarmBlockThreshold
|
|
|
|
| 43 |
|
| 44 |
# LangChain
|
| 45 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, ToolMessage
|
|
@@ -337,10 +338,13 @@ def direct_multimodal_gemini_tool(action_input_json_str: str) -> str:
|
|
| 337 |
try:
|
| 338 |
pil_image = Image.open(local_image_path)
|
| 339 |
except Exception as e_img_open: return f"Error opening image {local_image_path}: {str(e_img_open)}"
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
| 342 |
)
|
| 343 |
-
logger.info(f"Direct Multimodal Tool: Response from {
|
| 344 |
return response.text[:40000]
|
| 345 |
except json.JSONDecodeError as e_json_mm: return f"Error parsing JSON for Direct MM Tool: {str(e_json_mm)}. Input: {action_input_json_str}"
|
| 346 |
except Exception as e_tool_mm:
|
|
@@ -386,9 +390,8 @@ def initialize_agent_and_tools(force_reinit=False):
|
|
| 386 |
logger.info("Initializing agent and tools...")
|
| 387 |
if not GOOGLE_API_KEY: raise ValueError("GOOGLE_API_KEY not set for LangChain LLM.")
|
| 388 |
|
| 389 |
-
#
|
| 390 |
-
|
| 391 |
-
llm_safety_settings_corrected_again = {
|
| 392 |
HarmCategory.HARM_CATEGORY_HARASSMENT.value: HarmBlockThreshold.BLOCK_NONE.value,
|
| 393 |
HarmCategory.HARM_CATEGORY_HATE_SPEECH.value: HarmBlockThreshold.BLOCK_NONE.value,
|
| 394 |
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT.value: HarmBlockThreshold.BLOCK_NONE.value,
|
|
@@ -400,14 +403,14 @@ def initialize_agent_and_tools(force_reinit=False):
|
|
| 400 |
model=GEMINI_MODEL_NAME,
|
| 401 |
google_api_key=GOOGLE_API_KEY,
|
| 402 |
temperature=0.0,
|
| 403 |
-
safety_settings=
|
| 404 |
-
timeout=120,
|
| 405 |
convert_system_message_to_human=True
|
| 406 |
)
|
| 407 |
logger.info(f"LangChain LLM (Planner) initialized: {GEMINI_MODEL_NAME}")
|
| 408 |
except Exception as e:
|
| 409 |
logger.error(f"LangChain LLM init failed: {e}", exc_info=True)
|
| 410 |
-
logger.error(f"Type of safety_settings attempted: {type(
|
| 411 |
raise
|
| 412 |
|
| 413 |
TOOLS = []
|
|
@@ -631,7 +634,7 @@ with gr.Blocks(css=".gradio-container {max-width:1280px !important;margin:auto !
|
|
| 631 |
demo.load(update_ui_on_load_fn_within_context, [], [agent_status_display, missing_secrets_display])
|
| 632 |
|
| 633 |
if __name__ == "__main__":
|
| 634 |
-
logger.info(f"Application starting up (v7 - SafetySettings INT Values)...")
|
| 635 |
if not PYPDF2_AVAILABLE: logger.warning("PyPDF2 (PDF tool) NOT AVAILABLE.")
|
| 636 |
if not PIL_TESSERACT_AVAILABLE: logger.warning("Pillow/Pytesseract (OCR tool) NOT AVAILABLE.")
|
| 637 |
if not WHISPER_AVAILABLE: logger.warning("Whisper (Audio tool) NOT AVAILABLE.")
|
|
@@ -659,5 +662,4 @@ if __name__ == "__main__":
|
|
| 659 |
|
| 660 |
logger.info(f"Space ID: {os.getenv('SPACE_ID', 'Not Set')}")
|
| 661 |
logger.info("Gradio Interface launching...")
|
| 662 |
-
demo.queue().launch(debug=os.getenv("GRADIO_DEBUG","false").lower()=="true", share=False, max_threads=20)
|
| 663 |
-
|
|
|
|
| 38 |
try: import whisper; WHISPER_AVAILABLE = True
|
| 39 |
except ImportError: WHISPER_AVAILABLE = False; print("WARNING: OpenAI Whisper not found, Audio Transcription tool will be disabled.")
|
| 40 |
|
| 41 |
+
# Google GenAI SDK types (used by langchain-google-genai and direct client)
|
| 42 |
from google.genai.types import HarmCategory, HarmBlockThreshold
|
| 43 |
+
from google.ai import generativelanguage as glm # For FileState enum
|
| 44 |
|
| 45 |
# LangChain
|
| 46 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, ToolMessage
|
|
|
|
| 338 |
try:
|
| 339 |
pil_image = Image.open(local_image_path)
|
| 340 |
except Exception as e_img_open: return f"Error opening image {local_image_path}: {str(e_img_open)}"
|
| 341 |
+
|
| 342 |
+
model_id_for_client = f"models/{GEMINI_FLASH_MULTIMODAL_MODEL_NAME}" if not GEMINI_FLASH_MULTIMODAL_MODEL_NAME.startswith("models/") else GEMINI_FLASH_MULTIMODAL_MODEL_NAME
|
| 343 |
+
response = google_genai_client.generate_content( # Corrected to use the alias
|
| 344 |
+
model=model_id_for_client,
|
| 345 |
+
contents=[pil_image, text_prompt]
|
| 346 |
)
|
| 347 |
+
logger.info(f"Direct Multimodal Tool: Response received from {model_id_for_client} received.")
|
| 348 |
return response.text[:40000]
|
| 349 |
except json.JSONDecodeError as e_json_mm: return f"Error parsing JSON for Direct MM Tool: {str(e_json_mm)}. Input: {action_input_json_str}"
|
| 350 |
except Exception as e_tool_mm:
|
|
|
|
| 390 |
logger.info("Initializing agent and tools...")
|
| 391 |
if not GOOGLE_API_KEY: raise ValueError("GOOGLE_API_KEY not set for LangChain LLM.")
|
| 392 |
|
| 393 |
+
# Using INTEGER VALUES for HarmCategory keys AND HarmBlockThreshold enum .value for values.
|
| 394 |
+
llm_safety_settings_final_corrected = {
|
|
|
|
| 395 |
HarmCategory.HARM_CATEGORY_HARASSMENT.value: HarmBlockThreshold.BLOCK_NONE.value,
|
| 396 |
HarmCategory.HARM_CATEGORY_HATE_SPEECH.value: HarmBlockThreshold.BLOCK_NONE.value,
|
| 397 |
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT.value: HarmBlockThreshold.BLOCK_NONE.value,
|
|
|
|
| 403 |
model=GEMINI_MODEL_NAME,
|
| 404 |
google_api_key=GOOGLE_API_KEY,
|
| 405 |
temperature=0.0,
|
| 406 |
+
safety_settings=llm_safety_settings_final_corrected, # USE THE DICTIONARY WITH INT VALUES FOR BOTH
|
| 407 |
+
timeout=120,
|
| 408 |
convert_system_message_to_human=True
|
| 409 |
)
|
| 410 |
logger.info(f"LangChain LLM (Planner) initialized: {GEMINI_MODEL_NAME}")
|
| 411 |
except Exception as e:
|
| 412 |
logger.error(f"LangChain LLM init failed: {e}", exc_info=True)
|
| 413 |
+
logger.error(f"Type of safety_settings attempted: {type(llm_safety_settings_final_corrected)}, content: {llm_safety_settings_final_corrected}")
|
| 414 |
raise
|
| 415 |
|
| 416 |
TOOLS = []
|
|
|
|
| 634 |
demo.load(update_ui_on_load_fn_within_context, [], [agent_status_display, missing_secrets_display])
|
| 635 |
|
| 636 |
if __name__ == "__main__":
|
| 637 |
+
logger.info(f"Application starting up (v7 - SafetySettings INT Values FINAL fix)...")
|
| 638 |
if not PYPDF2_AVAILABLE: logger.warning("PyPDF2 (PDF tool) NOT AVAILABLE.")
|
| 639 |
if not PIL_TESSERACT_AVAILABLE: logger.warning("Pillow/Pytesseract (OCR tool) NOT AVAILABLE.")
|
| 640 |
if not WHISPER_AVAILABLE: logger.warning("Whisper (Audio tool) NOT AVAILABLE.")
|
|
|
|
| 662 |
|
| 663 |
logger.info(f"Space ID: {os.getenv('SPACE_ID', 'Not Set')}")
|
| 664 |
logger.info("Gradio Interface launching...")
|
| 665 |
+
demo.queue().launch(debug=os.getenv("GRADIO_DEBUG","false").lower()=="true", share=False, max_threads=20)
|
|
|