refactor: remove unimported packages
Browse files- agent.py +13 -23
- app.py +2 -0
- tools/document_process.py +1 -1
- tools/image_tools.py +175 -1
agent.py
CHANGED
@@ -1,32 +1,15 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
-
from typing import List, Dict, Any, Optional
|
4 |
-
import tempfile
|
5 |
-
import re
|
6 |
-
import json
|
7 |
-
import requests
|
8 |
-
from urllib.parse import urlparse
|
9 |
-
import pytesseract
|
10 |
-
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter
|
11 |
-
import cmath
|
12 |
-
import pandas as pd
|
13 |
-
import uuid
|
14 |
-
import numpy as np
|
15 |
from tools.python_interpreter import CodeInterpreter
|
16 |
|
17 |
interpreter_instance = CodeInterpreter()
|
18 |
-
hf_token = os.environ["HUGGING_FACE_TOKEN"]
|
19 |
|
20 |
|
21 |
from tools.image import *
|
22 |
|
23 |
"""Langraph"""
|
24 |
from langgraph.graph import START, StateGraph, MessagesState
|
25 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
26 |
-
from langchain_community.document_loaders import WikipediaLoader
|
27 |
-
from langchain_community.document_loaders import ArxivLoader
|
28 |
from langgraph.prebuilt import ToolNode, tools_condition
|
29 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
30 |
from langchain_groq import ChatGroq
|
31 |
from langchain_huggingface import (
|
32 |
ChatHuggingFace,
|
@@ -35,13 +18,12 @@ from langchain_huggingface import (
|
|
35 |
)
|
36 |
from langchain_community.vectorstores import SupabaseVectorStore
|
37 |
from langchain_core.messages import SystemMessage, HumanMessage
|
38 |
-
from langchain_core.tools import tool
|
39 |
from langchain.tools.retriever import create_retriever_tool
|
40 |
from supabase.client import Client, create_client
|
41 |
# ------- Tools
|
42 |
from tools.browse import web_search, wiki_search, arxiv_search
|
43 |
from tools.document_process import save_and_read_file, analyze_csv_file, analyze_excel_file, extract_text_from_image, download_file_from_url
|
44 |
-
from tools.image_tools import analyze_image, generate_simple_image
|
45 |
from tools.simple_math import multiply, add, subtract, divide, modulus, power, square_root
|
46 |
from tools.python_interpreter import execute_code_lang
|
47 |
|
@@ -64,8 +46,8 @@ supabase: Client = create_client(
|
|
64 |
vector_store = SupabaseVectorStore(
|
65 |
client=supabase,
|
66 |
embedding=embeddings,
|
67 |
-
table_name="
|
68 |
-
query_name="
|
69 |
)
|
70 |
create_retriever_tool = create_retriever_tool(
|
71 |
retriever=vector_store.as_retriever(),
|
@@ -92,13 +74,17 @@ tools = [
|
|
92 |
analyze_excel_file,
|
93 |
execute_code_lang,
|
94 |
analyze_image,
|
|
|
|
|
95 |
generate_simple_image,
|
|
|
96 |
]
|
97 |
|
98 |
def build_graph(provider: str = "groq"):
|
99 |
if provider == "groq":
|
100 |
# Groq https://console.groq.com/docs/models
|
101 |
llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
|
|
|
102 |
elif provider == "huggingface":
|
103 |
llm = ChatHuggingFace(
|
104 |
llm=HuggingFaceEndpoint(
|
@@ -122,7 +108,9 @@ def build_graph(provider: str = "groq"):
|
|
122 |
|
123 |
def retriever(state: MessagesState):
|
124 |
"""Retriever Node"""
|
125 |
-
|
|
|
|
|
126 |
if similar_question:
|
127 |
example_msg = HumanMessage(
|
128 |
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
@@ -131,6 +119,7 @@ def build_graph(provider: str = "groq"):
|
|
131 |
else:
|
132 |
return {"messages": [sys_msg] + state["messages"]}
|
133 |
|
|
|
134 |
builder = StateGraph(MessagesState)
|
135 |
builder.add_node("retriever", retriever)
|
136 |
builder.add_node("assistant", assistant)
|
@@ -142,7 +131,8 @@ def build_graph(provider: str = "groq"):
|
|
142 |
return builder.compile()
|
143 |
|
144 |
if __name__ == "__main__":
|
145 |
-
question = "
|
|
|
146 |
graph = build_graph(provider="groq")
|
147 |
messages = [HumanMessage(content=question)]
|
148 |
messages = graph.invoke({"messages": messages})
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from tools.python_interpreter import CodeInterpreter
|
4 |
|
5 |
interpreter_instance = CodeInterpreter()
|
|
|
6 |
|
7 |
|
8 |
from tools.image import *
|
9 |
|
10 |
"""Langraph"""
|
11 |
from langgraph.graph import START, StateGraph, MessagesState
|
|
|
|
|
|
|
12 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
|
13 |
from langchain_groq import ChatGroq
|
14 |
from langchain_huggingface import (
|
15 |
ChatHuggingFace,
|
|
|
18 |
)
|
19 |
from langchain_community.vectorstores import SupabaseVectorStore
|
20 |
from langchain_core.messages import SystemMessage, HumanMessage
|
|
|
21 |
from langchain.tools.retriever import create_retriever_tool
|
22 |
from supabase.client import Client, create_client
|
23 |
# ------- Tools
|
24 |
from tools.browse import web_search, wiki_search, arxiv_search
|
25 |
from tools.document_process import save_and_read_file, analyze_csv_file, analyze_excel_file, extract_text_from_image, download_file_from_url
|
26 |
+
from tools.image_tools import analyze_image, generate_simple_image , transform_image, draw_on_image, combine_images
|
27 |
from tools.simple_math import multiply, add, subtract, divide, modulus, power, square_root
|
28 |
from tools.python_interpreter import execute_code_lang
|
29 |
|
|
|
46 |
vector_store = SupabaseVectorStore(
|
47 |
client=supabase,
|
48 |
embedding=embeddings,
|
49 |
+
table_name="documents",
|
50 |
+
query_name="match_documents_langchain",
|
51 |
)
|
52 |
create_retriever_tool = create_retriever_tool(
|
53 |
retriever=vector_store.as_retriever(),
|
|
|
74 |
analyze_excel_file,
|
75 |
execute_code_lang,
|
76 |
analyze_image,
|
77 |
+
transform_image,
|
78 |
+
draw_on_image,
|
79 |
generate_simple_image,
|
80 |
+
combine_images,
|
81 |
]
|
82 |
|
83 |
def build_graph(provider: str = "groq"):
|
84 |
if provider == "groq":
|
85 |
# Groq https://console.groq.com/docs/models
|
86 |
llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
|
87 |
+
# llm = ChatGroq(model="deepseek-r1-distill-llama-70b", temperature=0)
|
88 |
elif provider == "huggingface":
|
89 |
llm = ChatHuggingFace(
|
90 |
llm=HuggingFaceEndpoint(
|
|
|
108 |
|
109 |
def retriever(state: MessagesState):
|
110 |
"""Retriever Node"""
|
111 |
+
# Extract the latest message content
|
112 |
+
query = state['messages'][-1].content
|
113 |
+
similar_question = vector_store.similarity_search(query, k = 2)
|
114 |
if similar_question:
|
115 |
example_msg = HumanMessage(
|
116 |
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
|
|
119 |
else:
|
120 |
return {"messages": [sys_msg] + state["messages"]}
|
121 |
|
122 |
+
|
123 |
builder = StateGraph(MessagesState)
|
124 |
builder.add_node("retriever", retriever)
|
125 |
builder.add_node("assistant", assistant)
|
|
|
131 |
return builder.compile()
|
132 |
|
133 |
if __name__ == "__main__":
|
134 |
+
question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
|
135 |
+
# question = """Q is Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec. What does Teal'c say in response to the question "Isn't that hot?"""
|
136 |
graph = build_graph(provider="groq")
|
137 |
messages = [HumanMessage(content=question)]
|
138 |
messages = graph.invoke({"messages": messages})
|
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import inspect
|
@@ -84,6 +85,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
84 |
if not task_id or question_text is None:
|
85 |
print(f"Skipping item with missing task_id or question: {item}")
|
86 |
continue
|
|
|
87 |
try:
|
88 |
submitted_answer = agent(question_text)
|
89 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import inspect
|
|
|
85 |
if not task_id or question_text is None:
|
86 |
print(f"Skipping item with missing task_id or question: {item}")
|
87 |
continue
|
88 |
+
# time.sleep(10)
|
89 |
try:
|
90 |
submitted_answer = agent(question_text)
|
91 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
tools/document_process.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from langchain_core.tools import tool
|
2 |
import os
|
3 |
-
from typing import
|
4 |
import tempfile
|
5 |
import requests
|
6 |
from urllib.parse import urlparse
|
|
|
1 |
from langchain_core.tools import tool
|
2 |
import os
|
3 |
+
from typing import Optional
|
4 |
import tempfile
|
5 |
import requests
|
6 |
from urllib.parse import urlparse
|
tools/image_tools.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
from langchain_core.tools import tool
|
2 |
from tools.image import decode_image, encode_image, save_image
|
3 |
-
from typing import Dict, Any, Optional
|
4 |
import numpy as np
|
|
|
5 |
|
6 |
@tool
|
7 |
def analyze_image(image_base64: str) -> Dict[str, Any]:
|
@@ -44,6 +45,132 @@ def analyze_image(image_base64: str) -> Dict[str, Any]:
|
|
44 |
except Exception as e:
|
45 |
return {"error": str(e)}
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
@tool
|
48 |
def generate_simple_image(
|
49 |
image_type: str,
|
@@ -109,3 +236,50 @@ def generate_simple_image(
|
|
109 |
|
110 |
except Exception as e:
|
111 |
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from langchain_core.tools import tool
|
2 |
from tools.image import decode_image, encode_image, save_image
|
3 |
+
from typing import Dict, Any, List, Optional
|
4 |
import numpy as np
|
5 |
+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter
|
6 |
|
7 |
@tool
|
8 |
def analyze_image(image_base64: str) -> Dict[str, Any]:
|
|
|
45 |
except Exception as e:
|
46 |
return {"error": str(e)}
|
47 |
|
48 |
+
@tool
|
49 |
+
def transform_image(
|
50 |
+
image_base64: str, operation: str, params: Optional[Dict[str, Any]] = None
|
51 |
+
) -> Dict[str, Any]:
|
52 |
+
"""
|
53 |
+
Apply transformations: resize, rotate, crop, flip, brightness, contrast, blur, sharpen, grayscale.
|
54 |
+
Args:
|
55 |
+
image_base64 (str): Base64 encoded input image
|
56 |
+
operation (str): Transformation operation
|
57 |
+
params (Dict[str, Any], optional): Parameters for the operation
|
58 |
+
Returns:
|
59 |
+
Dictionary with transformed image (base64)
|
60 |
+
"""
|
61 |
+
try:
|
62 |
+
img = decode_image(image_base64)
|
63 |
+
params = params or {}
|
64 |
+
|
65 |
+
if operation == "resize":
|
66 |
+
img = img.resize(
|
67 |
+
(
|
68 |
+
params.get("width", img.width // 2),
|
69 |
+
params.get("height", img.height // 2),
|
70 |
+
)
|
71 |
+
)
|
72 |
+
elif operation == "rotate":
|
73 |
+
img = img.rotate(params.get("angle", 90), expand=True)
|
74 |
+
elif operation == "crop":
|
75 |
+
img = img.crop(
|
76 |
+
(
|
77 |
+
params.get("left", 0),
|
78 |
+
params.get("top", 0),
|
79 |
+
params.get("right", img.width),
|
80 |
+
params.get("bottom", img.height),
|
81 |
+
)
|
82 |
+
)
|
83 |
+
elif operation == "flip":
|
84 |
+
if params.get("direction", "horizontal") == "horizontal":
|
85 |
+
img = img.transpose(Image.FLIP_LEFT_RIGHT)
|
86 |
+
else:
|
87 |
+
img = img.transpose(Image.FLIP_TOP_BOTTOM)
|
88 |
+
elif operation == "adjust_brightness":
|
89 |
+
img = ImageEnhance.Brightness(img).enhance(params.get("factor", 1.5))
|
90 |
+
elif operation == "adjust_contrast":
|
91 |
+
img = ImageEnhance.Contrast(img).enhance(params.get("factor", 1.5))
|
92 |
+
elif operation == "blur":
|
93 |
+
img = img.filter(ImageFilter.GaussianBlur(params.get("radius", 2)))
|
94 |
+
elif operation == "sharpen":
|
95 |
+
img = img.filter(ImageFilter.SHARPEN)
|
96 |
+
elif operation == "grayscale":
|
97 |
+
img = img.convert("L")
|
98 |
+
else:
|
99 |
+
return {"error": f"Unknown operation: {operation}"}
|
100 |
+
|
101 |
+
result_path = save_image(img)
|
102 |
+
result_base64 = encode_image(result_path)
|
103 |
+
return {"transformed_image": result_base64}
|
104 |
+
|
105 |
+
except Exception as e:
|
106 |
+
return {"error": str(e)}
|
107 |
+
|
108 |
+
|
109 |
+
@tool
|
110 |
+
def draw_on_image(
|
111 |
+
image_base64: str, drawing_type: str, params: Dict[str, Any]
|
112 |
+
) -> Dict[str, Any]:
|
113 |
+
"""
|
114 |
+
Draw shapes (rectangle, circle, line) or text onto an image.
|
115 |
+
Args:
|
116 |
+
image_base64 (str): Base64 encoded input image
|
117 |
+
drawing_type (str): Drawing type
|
118 |
+
params (Dict[str, Any]): Drawing parameters
|
119 |
+
Returns:
|
120 |
+
Dictionary with result image (base64)
|
121 |
+
"""
|
122 |
+
try:
|
123 |
+
img = decode_image(image_base64)
|
124 |
+
draw = ImageDraw.Draw(img)
|
125 |
+
color = params.get("color", "red")
|
126 |
+
|
127 |
+
if drawing_type == "rectangle":
|
128 |
+
draw.rectangle(
|
129 |
+
[params["left"], params["top"], params["right"], params["bottom"]],
|
130 |
+
outline=color,
|
131 |
+
width=params.get("width", 2),
|
132 |
+
)
|
133 |
+
elif drawing_type == "circle":
|
134 |
+
x, y, r = params["x"], params["y"], params["radius"]
|
135 |
+
draw.ellipse(
|
136 |
+
(x - r, y - r, x + r, y + r),
|
137 |
+
outline=color,
|
138 |
+
width=params.get("width", 2),
|
139 |
+
)
|
140 |
+
elif drawing_type == "line":
|
141 |
+
draw.line(
|
142 |
+
(
|
143 |
+
params["start_x"],
|
144 |
+
params["start_y"],
|
145 |
+
params["end_x"],
|
146 |
+
params["end_y"],
|
147 |
+
),
|
148 |
+
fill=color,
|
149 |
+
width=params.get("width", 2),
|
150 |
+
)
|
151 |
+
elif drawing_type == "text":
|
152 |
+
font_size = params.get("font_size", 20)
|
153 |
+
try:
|
154 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
155 |
+
except IOError:
|
156 |
+
font = ImageFont.load_default()
|
157 |
+
draw.text(
|
158 |
+
(params["x"], params["y"]),
|
159 |
+
params.get("text", "Text"),
|
160 |
+
fill=color,
|
161 |
+
font=font,
|
162 |
+
)
|
163 |
+
else:
|
164 |
+
return {"error": f"Unknown drawing type: {drawing_type}"}
|
165 |
+
|
166 |
+
result_path = save_image(img)
|
167 |
+
result_base64 = encode_image(result_path)
|
168 |
+
return {"result_image": result_base64}
|
169 |
+
|
170 |
+
except Exception as e:
|
171 |
+
return {"error": str(e)}
|
172 |
+
|
173 |
+
|
174 |
@tool
|
175 |
def generate_simple_image(
|
176 |
image_type: str,
|
|
|
236 |
|
237 |
except Exception as e:
|
238 |
return {"error": str(e)}
|
239 |
+
|
240 |
+
|
241 |
+
@tool
|
242 |
+
def combine_images(
|
243 |
+
images_base64: List[str], operation: str, params: Optional[Dict[str, Any]] = None
|
244 |
+
) -> Dict[str, Any]:
|
245 |
+
"""
|
246 |
+
Combine multiple images (collage, stack, blend).
|
247 |
+
Args:
|
248 |
+
images_base64 (List[str]): List of base64 images
|
249 |
+
operation (str): Combination type
|
250 |
+
params (Dict[str, Any], optional)
|
251 |
+
Returns:
|
252 |
+
Dictionary with combined image (base64)
|
253 |
+
"""
|
254 |
+
try:
|
255 |
+
images = [decode_image(b64) for b64 in images_base64]
|
256 |
+
params = params or {}
|
257 |
+
|
258 |
+
if operation == "stack":
|
259 |
+
direction = params.get("direction", "horizontal")
|
260 |
+
if direction == "horizontal":
|
261 |
+
total_width = sum(img.width for img in images)
|
262 |
+
max_height = max(img.height for img in images)
|
263 |
+
new_img = Image.new("RGB", (total_width, max_height))
|
264 |
+
x = 0
|
265 |
+
for img in images:
|
266 |
+
new_img.paste(img, (x, 0))
|
267 |
+
x += img.width
|
268 |
+
else:
|
269 |
+
max_width = max(img.width for img in images)
|
270 |
+
total_height = sum(img.height for img in images)
|
271 |
+
new_img = Image.new("RGB", (max_width, total_height))
|
272 |
+
y = 0
|
273 |
+
for img in images:
|
274 |
+
new_img.paste(img, (0, y))
|
275 |
+
y += img.height
|
276 |
+
else:
|
277 |
+
return {"error": f"Unsupported combination operation {operation}"}
|
278 |
+
|
279 |
+
result_path = save_image(new_img)
|
280 |
+
result_base64 = encode_image(result_path)
|
281 |
+
return {"combined_image": result_base64}
|
282 |
+
|
283 |
+
except Exception as e:
|
284 |
+
return {"error": str(e)}
|
285 |
+
|