Update app.py
Browse files
app.py
CHANGED
|
@@ -68,15 +68,6 @@ class NewAgent:
|
|
| 68 |
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 69 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
| 70 |
)
|
| 71 |
-
# sys_msg = SystemMessage(
|
| 72 |
-
# content=f"""
|
| 73 |
-
# You are a general AI assistant. I will ask you a question.
|
| 74 |
-
# Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 75 |
-
# YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 76 |
-
# If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 77 |
-
# If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 78 |
-
# If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
| 79 |
-
# )
|
| 80 |
return {
|
| 81 |
"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])],
|
| 82 |
}
|
|
@@ -100,6 +91,128 @@ class NewAgent:
|
|
| 100 |
messages = [HumanMessage(content=question)]
|
| 101 |
response = alfred.invoke({"messages": messages})
|
| 102 |
return response['messages'][-1].content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -124,7 +237,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 124 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 125 |
try:
|
| 126 |
# agent = BasicAgent()
|
| 127 |
-
agent = NewAgent()
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
print(f"Error instantiating agent: {e}")
|
| 130 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 68 |
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 69 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
| 70 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return {
|
| 72 |
"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])],
|
| 73 |
}
|
|
|
|
| 91 |
messages = [HumanMessage(content=question)]
|
| 92 |
response = alfred.invoke({"messages": messages})
|
| 93 |
return response['messages'][-1].content
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class NewAgent2:
|
| 97 |
+
def __init__(self):
|
| 98 |
+
print("NewAgent initialized.")
|
| 99 |
+
|
| 100 |
+
def _process_question_input(self, question_input: Union[str, Dict[str, Any]]) -> tuple:
|
| 101 |
+
"""
|
| 102 |
+
Process the question input which could be:
|
| 103 |
+
- A simple string
|
| 104 |
+
- A dictionary with text and image data
|
| 105 |
+
"""
|
| 106 |
+
if isinstance(question_input, str):
|
| 107 |
+
return question_input, None
|
| 108 |
+
|
| 109 |
+
# If it's a dictionary, extract text and image
|
| 110 |
+
if isinstance(question_input, dict):
|
| 111 |
+
text = question_input.get('text', question_input.get('question', ''))
|
| 112 |
+
image_data = question_input.get('image', question_input.get('image_url', None))
|
| 113 |
+
return text, image_data
|
| 114 |
+
|
| 115 |
+
return str(question_input), None
|
| 116 |
+
|
| 117 |
+
def _create_message_content(self, text: str, image_data: str = None) -> Union[str, list]:
|
| 118 |
+
"""
|
| 119 |
+
Create message content that can handle both text and images
|
| 120 |
+
"""
|
| 121 |
+
if not image_data:
|
| 122 |
+
return text
|
| 123 |
+
|
| 124 |
+
# Handle different image formats
|
| 125 |
+
if image_data.startswith('http'):
|
| 126 |
+
# URL format
|
| 127 |
+
return [
|
| 128 |
+
{"type": "text", "text": text},
|
| 129 |
+
{"type": "image_url", "image_url": {"url": image_data}}
|
| 130 |
+
]
|
| 131 |
+
elif image_data.startswith('data:image'):
|
| 132 |
+
# Base64 data URL format
|
| 133 |
+
return [
|
| 134 |
+
{"type": "text", "text": text},
|
| 135 |
+
{"type": "image_url", "image_url": {"url": image_data}}
|
| 136 |
+
]
|
| 137 |
+
else:
|
| 138 |
+
# Assume it's base64 encoded image data
|
| 139 |
+
image_url = f"data:image/jpeg;base64,{image_data}"
|
| 140 |
+
return [
|
| 141 |
+
{"type": "text", "text": text},
|
| 142 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 143 |
+
]
|
| 144 |
+
|
| 145 |
+
def __call__(self, question: Union[str, Dict[str, Any]]) -> str:
|
| 146 |
+
print(f"Agent received question input: {str(question)[:100]}...")
|
| 147 |
+
|
| 148 |
+
# Process the input to extract text and image
|
| 149 |
+
question_text, image_data = self._process_question_input(question)
|
| 150 |
+
print(f"Extracted text: {question_text[:50]}...")
|
| 151 |
+
print(f"Image data present: {image_data is not None}")
|
| 152 |
+
|
| 153 |
+
# Initialize the web search tool
|
| 154 |
+
search_tool = DuckDuckGoSearchRun()
|
| 155 |
+
# Initialize the Hub stats tool
|
| 156 |
+
hub_stats_tool = Tool(
|
| 157 |
+
name="get_hub_stats",
|
| 158 |
+
func=get_hub_stats,
|
| 159 |
+
description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 160 |
+
)
|
| 161 |
+
# Generate the chat interface, including the tools
|
| 162 |
+
tools = [
|
| 163 |
+
search_tool,
|
| 164 |
+
hub_stats_tool,
|
| 165 |
+
]
|
| 166 |
+
|
| 167 |
+
# Use a vision-capable model
|
| 168 |
+
llm = ChatOpenAI(model="gpt-4o") # Vision-capable model
|
| 169 |
+
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
|
| 170 |
+
|
| 171 |
+
# Generate the AgentState and Agent graph
|
| 172 |
+
class AgentState(TypedDict):
|
| 173 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 174 |
+
|
| 175 |
+
def assistant(state: AgentState):
|
| 176 |
+
sys_msg = SystemMessage(
|
| 177 |
+
content=f"""
|
| 178 |
+
You are a general AI assistant. I will ask you a question that may include text and/or images.
|
| 179 |
+
If you cannot find an answer, you may report your thoughts.
|
| 180 |
+
If you find an answer, your response should only contain your final answer. Report nothing before or after this answer.
|
| 181 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 182 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 183 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 184 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 185 |
+
|
| 186 |
+
If an image is provided, analyze it carefully and answer based on what you see in the image.
|
| 187 |
+
"""
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
return {
|
| 191 |
+
"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])],
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
## The graph
|
| 195 |
+
builder = StateGraph(AgentState)
|
| 196 |
+
# Define nodes: these do the work
|
| 197 |
+
builder.add_node("assistant", assistant)
|
| 198 |
+
builder.add_node("tools", ToolNode(tools))
|
| 199 |
+
# Define edges: these determine how the control flow moves
|
| 200 |
+
builder.add_edge(START, "assistant")
|
| 201 |
+
builder.add_conditional_edges(
|
| 202 |
+
"assistant",
|
| 203 |
+
# If the latest message requires a tool, route to tools
|
| 204 |
+
# Otherwise, provide a direct response
|
| 205 |
+
tools_condition,
|
| 206 |
+
)
|
| 207 |
+
builder.add_edge("tools", "assistant")
|
| 208 |
+
alfred = builder.compile()
|
| 209 |
+
|
| 210 |
+
# Create the human message with proper content format
|
| 211 |
+
message_content = self._create_message_content(question_text, image_data)
|
| 212 |
+
messages = [HumanMessage(content=message_content)]
|
| 213 |
+
|
| 214 |
+
response = alfred.invoke({"messages": messages})
|
| 215 |
+
return response['messages'][-1].content
|
| 216 |
|
| 217 |
|
| 218 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 237 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 238 |
try:
|
| 239 |
# agent = BasicAgent()
|
| 240 |
+
# agent = NewAgent()
|
| 241 |
+
agent = NewAgent2()
|
| 242 |
except Exception as e:
|
| 243 |
print(f"Error instantiating agent: {e}")
|
| 244 |
return f"Error initializing agent: {e}", None
|