i make image analyze script that receive image from front end via fast api then proceed by phidata backend, every time i procees the image it show
Failed to fetch
in my docker log i see erorr :
2025-01-13 11:43:11 DEBUG ============== model ==============
2025-01-13 11:43:11 DEBUG Okay, I'll analyze the provided image and give you a breakdown of the
2025-01-13 11:43:11 ingredients as per your instructions. Please note that since I can't
2025-01-13 11:43:11 see the actual image, I'll be relying on the text you've provided which
2025-01-13 11:43:11 may or may not be the complete list.
2025-01-13 11:43:11
2025-01-13 11:43:11 Please provide the ingredient list from the image.
2025-01-13 11:43:11
2025-01-13 11:43:11 DEBUG ---------- Gemini Response End ----------
2025-01-13 11:43:11 DEBUG Added 3 Messages to AgentMemory
2025-01-13 11:43:11 DEBUG Added AgentChat to AgentMemory
2025-01-13 11:43:11 DEBUG Exception upserting into table: Object of type RepeatedComposite is not
2025-01-13 11:43:11 JSON serializable
2025-01-13 11:43:11 DEBUG Table does not exist: gambar_agent_sessions
2025-01-13 11:43:11 DEBUG Creating table for future transactions
2025-01-13 11:43:11 DEBUG Checking if table exists: gambar_agent_sessions
2025-01-13 11:43:11 DEBUG Exception upserting into table: Object of type RepeatedComposite is not
2025-01-13 11:43:11 JSON serializable
2025-01-13 11:43:11 DEBUG Table does not exist: gambar_agent_sessions
2025-01-13 11:43:11 DEBUG Creating table for future transactions
2025-01-13 11:43:11 DEBUG Checking if table exists: gambar_agent_sessions
2025-01-13 11:43:11 DEBUG --**-- Logging Agent Run
2025-01-13 11:43:11 DEBUG Could not create Agent run: Object of type RepeatedComposite is not
2025-01-13 11:43:11 JSON serializable
2025-01-13 11:43:11 DEBUG *********** Agent Run End: f809445e-03ac-48a6-bae0-612a3cf3c7a8
2025-01-13 11:43:11 ***********
2025-01-13 11:43:11 INFO: 172.18.0.1:36946 - "POST /v1/playground/agent/run HTTP/1.1" 500 Internal Server Error
2025-01-13 11:43:11 ERROR: Exception in ASGI application
2025-01-13 11:43:11 Traceback (most recent call last):
2025-01-13 11:43:11 File "/usr/local/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 401, in run_asgi
this is my actual code :
import os
from typing import Optional
from fastapi import UploadFile
from tempfile import NamedTemporaryFile
from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.tavily import TavilyTools
from phi.storage.agent.postgres import PgAgentStorage
from agents.settings import agent_settings
from db.session import db_url
from .constants import SYSTEM_PROMPT, INSTRUCTIONS
gambar_agent_storage = PgAgentStorage(table_name="gambar_agent_sessions", db_url=db_url)
def save_uploaded_file(uploaded_file: UploadFile) -> str:
"""Save uploaded file to temporary storage and return path
Args:
uploaded_file: FastAPI UploadFile object
Returns:
str: Path to saved temporary file
Raises:
IOError: If file cannot be saved
"""
try:
with NamedTemporaryFile(dir='.', suffix='.jpg', delete=False) as f:
f.write(uploaded_file.file.read())
return f.name
except Exception as e:
raise IOError(f"Failed to save uploaded file: {str(e)}")
def get_gambar_agent(
model_id: Optional[str] = None,
user_id: Optional[str] = None,
session_id: Optional[str] = None,
debug_mode: bool = False,
) -> Agent:
"""Returns a gambar agent for analyzing product ingredients from images
Args:
model_id: Model ID to use for the agent
user_id: User ID for the agent session
session_id: Session ID for the agent
debug_mode: Whether to run in debug mode
Returns:
Agent: Configured gambar agent instance
"""
return Agent(
name="Gambar Agent",
agent_id="gambar-agent",
session_id=session_id,
user_id=user_id,
model=Gemini(
id=model_id or "gemini-2.0-flash-exp",
max_tokens=agent_settings.default_max_completion_tokens,
temperature=agent_settings.default_temperature,
),
description="Anda adalah asisten AI yang menganalisis bahan-bahan produk dari gambar.",
instructions=INSTRUCTIONS,
tools=[TavilyTools(api_key=os.getenv("TAVILY_API_KEY"))],
markdown=True,
add_datetime_to_instructions=True,
storage=gambar_agent_storage,
read_chat_history=True,
monitoring=True,
debug_mode=debug_mode,
)
async def analyze_image_api(uploaded_file: UploadFile) -> str:
"""Analyze uploaded image using gambar agent
Args:
uploaded_file: FastAPI UploadFile object
Returns:
str: Analysis result from agent
Raises:
IOError: If image cannot be processed
"""
try:
# Save uploaded file
temp_path = save_uploaded_file(uploaded_file)
# Get agent instance
agent = get_gambar_agent()
# Analyze image
response = agent.run(
"Analyze the given image",
images=[temp_path],
)
# Clean up temporary file
os.unlink(temp_path)
# Extract and format response content
if hasattr(response, 'content'):
# If response has content attribute, try to extract text
try:
if hasattr(response.content, 'parts'):
# Handle RepeatedComposite type by joining text parts
return ''.join(part.text for part in response.content.parts if hasattr(part, 'text'))
elif hasattr(response.content, 'text'):
return response.content.text
return str(response.content)
except Exception as e:
return str(response.content)
elif hasattr(response, 'text'):
# If response has text attribute, use it
return response.text
else:
# Fallback to string representation
return str(response)
except Exception as e:
if 'temp_path' in locals():
try:
os.unlink(temp_path)
except:
pass
raise IOError(f"Failed to analyze image: {str(e)}")