Refactor Agent initialization and enhance respond function to support web search; add check_format utility function.
Browse files
app.py
CHANGED
@@ -47,6 +47,15 @@ def load_file(path: str) -> list | dict:
|
|
47 |
else:
|
48 |
return {"raw document text": text, "file path": path}
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
## tools definition
|
52 |
@tool
|
@@ -134,13 +143,15 @@ def generate_image(prompt: str, neg_prompt: str) -> Image.Image:
|
|
134 |
## agent definition
|
135 |
class Agent:
|
136 |
def __init__(self, ):
|
137 |
-
client = HfApiModel("
|
138 |
self.agent = CodeAgent(
|
139 |
model=client,
|
140 |
tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(max_output_length=20000), generate_image, download_images, transcribe_audio],
|
141 |
additional_authorized_imports=["pandas", "PIL", "io"],
|
142 |
planning_interval=1,
|
143 |
-
max_steps=
|
|
|
|
|
144 |
)
|
145 |
with open("system_prompt.txt", "r") as f:
|
146 |
system_prompt = f.read()
|
@@ -156,14 +167,17 @@ class Agent:
|
|
156 |
return answer
|
157 |
|
158 |
## gradio functions
|
159 |
-
def respond(message: str, history : dict):
|
160 |
|
161 |
# input
|
162 |
print("history:", history)
|
163 |
text = message.get("text", "")
|
164 |
-
if not message.get("files"): # no files uploaded
|
165 |
print("No files received.")
|
166 |
-
message = agent(text, conversation_history=history) # conversation_history is a dict with the history of the conversation
|
|
|
|
|
|
|
167 |
else:
|
168 |
files = message.get("files", [])
|
169 |
print(f"files received: {files}")
|
@@ -173,12 +187,10 @@ def respond(message: str, history : dict):
|
|
173 |
else:
|
174 |
file = load_file(files[0])
|
175 |
message = agent(text, files=file, conversation_history=history)
|
176 |
-
|
177 |
# output
|
178 |
print("Agent response:", message)
|
179 |
|
180 |
return message
|
181 |
-
|
182 |
|
183 |
def initialize_agent():
|
184 |
agent = Agent()
|
@@ -196,9 +208,13 @@ with gr.Blocks() as demo:
|
|
196 |
title='MultiAgent System for Screenplay Creation and Editing',
|
197 |
show_progress='full',
|
198 |
fill_height=True,
|
199 |
-
fill_width=
|
200 |
save_history=True,
|
201 |
-
|
|
|
|
|
|
|
|
|
202 |
|
203 |
|
204 |
if __name__ == "__main__":
|
|
|
47 |
else:
|
48 |
return {"raw document text": text, "file path": path}
|
49 |
|
50 |
+
def check_format(answer: str | list, *args, **kwargs ) -> list:
|
51 |
+
""" Check if the answer is a list. """
|
52 |
+
|
53 |
+
print("Checking format of the answer:", answer)
|
54 |
+
if isinstance(answer, list):
|
55 |
+
return answer
|
56 |
+
else:
|
57 |
+
return [answer]
|
58 |
+
|
59 |
|
60 |
## tools definition
|
61 |
@tool
|
|
|
143 |
## agent definition
|
144 |
class Agent:
|
145 |
def __init__(self, ):
|
146 |
+
client = HfApiModel("google/gemma-3-27b-it", provider="nebius", api_key=os.getenv("NEBIUS_API_KEY"))
|
147 |
self.agent = CodeAgent(
|
148 |
model=client,
|
149 |
tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(max_output_length=20000), generate_image, download_images, transcribe_audio],
|
150 |
additional_authorized_imports=["pandas", "PIL", "io"],
|
151 |
planning_interval=1,
|
152 |
+
max_steps=5,
|
153 |
+
stream_outputs=False,
|
154 |
+
final_answer_checks=[check_format]
|
155 |
)
|
156 |
with open("system_prompt.txt", "r") as f:
|
157 |
system_prompt = f.read()
|
|
|
167 |
return answer
|
168 |
|
169 |
## gradio functions
|
170 |
+
def respond(message: str, history : dict, web_search: bool = False):
|
171 |
|
172 |
# input
|
173 |
print("history:", history)
|
174 |
text = message.get("text", "")
|
175 |
+
if not message.get("files") and not web_search: # no files uploaded
|
176 |
print("No files received.")
|
177 |
+
message = agent(text + "\nADDITIONAL CONTRAINT: Don't use web search", conversation_history=history) # conversation_history is a dict with the history of the conversation
|
178 |
+
elif not message.get("files") and web_search==True: # no files uploaded
|
179 |
+
print("No files received + web search enabled.")
|
180 |
+
message = agent(text, conversation_history=history)
|
181 |
else:
|
182 |
files = message.get("files", [])
|
183 |
print(f"files received: {files}")
|
|
|
187 |
else:
|
188 |
file = load_file(files[0])
|
189 |
message = agent(text, files=file, conversation_history=history)
|
|
|
190 |
# output
|
191 |
print("Agent response:", message)
|
192 |
|
193 |
return message
|
|
|
194 |
|
195 |
def initialize_agent():
|
196 |
agent = Agent()
|
|
|
208 |
title='MultiAgent System for Screenplay Creation and Editing',
|
209 |
show_progress='full',
|
210 |
fill_height=True,
|
211 |
+
fill_width=False,
|
212 |
save_history=True,
|
213 |
+
additional_inputs=[
|
214 |
+
gr.Checkbox(value=False, label="Web Search",
|
215 |
+
info="Enable web search to find information online. If disabled, the agent will only use the provided files and images."
|
216 |
+
),
|
217 |
+
])
|
218 |
|
219 |
|
220 |
if __name__ == "__main__":
|