JabrilJacobs commited on
Commit
d8ed545
·
verified ·
1 Parent(s): 74e9fb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -128
app.py CHANGED
@@ -61,12 +61,14 @@ class NewAgent:
61
  sys_msg = SystemMessage(
62
  content=f"""
63
  You are a general AI assistant. I will ask you a question.
 
64
  If you cannot find an answer, you may report your thoughts.
65
  If you find an answer, your response should only contain your final answer. Report nothing before or after this answer.
66
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
67
  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.
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"])],
@@ -91,128 +93,6 @@ class NewAgent:
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,8 +117,8 @@ 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
@@ -274,16 +154,17 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
274
  for item in questions_data:
275
  task_id = item.get("task_id")
276
  question_text = item.get("question")
 
277
  if not task_id or question_text is None:
278
  print(f"Skipping item with missing task_id or question: {item}")
279
  continue
280
  try:
281
- submitted_answer = agent(question_text)
282
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
283
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
284
  except Exception as e:
285
  print(f"Error running agent on task {task_id}: {e}")
286
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
287
 
288
  if not answers_payload:
289
  print("Agent did not produce any answers to submit.")
 
61
  sys_msg = SystemMessage(
62
  content=f"""
63
  You are a general AI assistant. I will ask you a question.
64
+ If a file_name is provided, it indicates there's an associated file you may need to analyze:
65
  If you cannot find an answer, you may report your thoughts.
66
  If you find an answer, your response should only contain your final answer. Report nothing before or after this answer.
67
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
68
  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.
69
  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.
70
+ 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.
71
+ Current file (if any): {file_name}"""
72
  )
73
  return {
74
  "messages": [llm_with_tools.invoke([sys_msg] + state["messages"])],
 
93
  messages = [HumanMessage(content=question)]
94
  response = alfred.invoke({"messages": messages})
95
  return response['messages'][-1].content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
 
98
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
117
  # 1. Instantiate Agent ( modify this part to create your agent)
118
  try:
119
  # agent = BasicAgent()
120
+ agent = NewAgent()
121
+ # agent = NewAgent2()
122
  except Exception as e:
123
  print(f"Error instantiating agent: {e}")
124
  return f"Error initializing agent: {e}", None
 
154
  for item in questions_data:
155
  task_id = item.get("task_id")
156
  question_text = item.get("question")
157
+ file_name = item.get("file_name", "")
158
  if not task_id or question_text is None:
159
  print(f"Skipping item with missing task_id or question: {item}")
160
  continue
161
  try:
162
+ submitted_answer = agent(question_text, file_name)
163
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
164
+ results_log.append({"Task ID": task_id, "Question": question_text, "File": file_name, "Submitted Answer": submitted_answer})
165
  except Exception as e:
166
  print(f"Error running agent on task {task_id}: {e}")
167
+ results_log.append({"Task ID": task_id, "Question": question_text, "File": file_name, "Submitted Answer": f"AGENT ERROR: {e}"})
168
 
169
  if not answers_payload:
170
  print("Agent did not produce any answers to submit.")