albertCHY commited on
Commit
357fd3d
·
verified ·
1 Parent(s): d310ca1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -118
app.py CHANGED
@@ -1,20 +1,12 @@
1
  import os
2
- import spaces
3
  import gradio as gr
4
  import requests
5
  import inspect
6
  import pandas as pd
7
 
8
- import base64
9
- from langchain_community.tools import DuckDuckGoSearchRun
10
- from typing import TypedDict, Annotated, Optional, List
11
- from langgraph.graph.message import add_messages
12
- from langgraph.graph import START, StateGraph
13
- from langgraph.prebuilt import ToolNode, tools_condition
14
- from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
15
- from langchain_core.tools import Tool
16
- from IPython.display import Image, display
17
- from langchain_google_genai import ChatGoogleGenerativeAI
18
 
19
  # (Keep Constants as is)
20
  # --- Constants ---
@@ -28,117 +20,19 @@ def gpu_check():
28
 
29
  # --- Basic Agent Definition ---
30
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
31
  class BasicAgent:
32
  def __init__(self):
33
  print("BasicAgent initialized.")
34
-
35
- # --- Tools ---
36
- def _search_web(query:str) -> str:
37
- """Searches the web for the answer to a given question or topic."""
38
- return DuckDuckGoSearchRun().run(query)
39
-
40
- def _extract_text(img_path: str) -> str:
41
- """
42
- Extract text from an image file using a multimodal model.
43
- Get information from the image including objects, people and/or weather
44
- """
45
- all_text = ""
46
- try:
47
- # Read image and encode as base64
48
- with open(img_path, "rb") as image_file:
49
- image_bytes = image_file.read()
50
-
51
- image_base64 = base64.b64encode(image_bytes).decode("utf-8")
52
-
53
- # Prepare the prompt including the base64 image data
54
- message = [
55
- HumanMessage(
56
- content=[
57
- {
58
- "type": "text",
59
- "text": (
60
- "Extract all the text from this image. Also extract any objects, people and/or weather"
61
- "Return only the extracted text, no explanations."
62
- ),
63
- },
64
- {
65
- "type": "image_url",
66
- "image_url": {
67
- "url": f"data:image/png;base64,{image_base64}"
68
- },
69
- },
70
- ]
71
- )
72
- ]
73
-
74
- # Call the vision-capable model
75
- vision_llm = ChatGoogleGenerativeAI(model="gemini-3.7-flash")
76
- response = vision_llm.invoke(message)
77
-
78
- # Append extracted text
79
- all_text += response.content + "\n\n"
80
-
81
- return all_text.strip()
82
- except Exception as e:
83
- # A butler should handle errors gracefully
84
- error_msg = f"Error extracting text: {str(e)}"
85
- print(error_msg)
86
- return ""
87
-
88
-
89
- # --- Agent ---
90
- class AgentState(TypedDict):
91
- messages: Annotated[list[AnyMessage], add_messages]
92
-
93
- chat = ChatGoogleGenerativeAI(
94
- model="gemini-3.7-flash"
95
- )
96
- # Initialize the tools
97
- describe_image = Tool(
98
- name = "describe_image",
99
- func = _extract_text,
100
- description = "Extracts text from an image file."
101
- )
102
- search_web = Tool(
103
- name = "search_web",
104
- func = _search_web,
105
- description = "Searches the web for information."
106
- )
107
- tools = [search_web, describe_image]
108
- chat_with_tools = chat.bind_tools(tools)
109
-
110
-
111
- def assistant(state: AgentState):
112
- return {
113
- "messages": [chat_with_tools.invoke(state["messages"])],
114
- }
115
-
116
- ## The graph
117
- workflow = StateGraph(AgentState)
118
-
119
- # Define nodes: these do the work
120
- workflow.add_node("assistant", assistant)
121
- workflow.add_node("tools", ToolNode(tools))
122
-
123
- # Define edges: these determine how the control flow moves
124
- workflow.add_edge(START, "assistant")
125
- workflow.add_conditional_edges(
126
- "assistant",
127
- # If the latest message requires a tool, route to tools
128
- # Otherwise, provide a direct response
129
- tools_condition,
130
- )
131
- workflow.add_edge("tools", "assistant")
132
- self.alfred = workflow.compile()
133
-
134
-
135
-
136
  def __call__(self, question: str) -> str:
137
- print(f"Agent received question (first 50 chars): {question[:50]}...")
138
- response = self.alfred.invoke({"messages": question})
139
- answer = response['messages'][-1].text
140
- print(f"Agent returning answer: {answer}")
141
- return answer
 
 
142
 
143
  def run_and_submit_all( profile: gr.OAuthProfile | None):
144
  """
@@ -267,9 +161,11 @@ with gr.Blocks() as demo:
267
  gr.Markdown(
268
  """
269
  **Instructions:**
 
270
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
271
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
272
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
273
  ---
274
  **Disclaimers:**
275
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
 
1
  import os
 
2
  import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
 
7
+ import spaces
8
+ from agent import graph
9
+ from langchain_core.messages import HumanMessage
 
 
 
 
 
 
 
10
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
 
20
 
21
  # --- Basic Agent Definition ---
22
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
23
+
24
+
25
  class BasicAgent:
26
  def __init__(self):
27
  print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def __call__(self, question: str) -> str:
29
+ answer = graph.invoke({
30
+ "messages": [HumanMessage(content=question)],
31
+ "file_path": None,
32
+ "url": None,
33
+ "task_id": None
34
+ })
35
+ return answer["messages"][-1].content
36
 
37
  def run_and_submit_all( profile: gr.OAuthProfile | None):
38
  """
 
161
  gr.Markdown(
162
  """
163
  **Instructions:**
164
+
165
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
166
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
167
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
168
+
169
  ---
170
  **Disclaimers:**
171
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).