Spaces:
Sleeping
Sleeping
ajout de quelques agents / fonctions
Browse files- agent.py +7 -26
- app.py +1 -1
- apt.txt +1 -0
- fonctions.py +49 -0
- requirements.txt +4 -1
agent.py
CHANGED
|
@@ -13,6 +13,7 @@ from pydantic import BaseModel
|
|
| 13 |
from typing import List, Dict
|
| 14 |
import pandas as pd
|
| 15 |
|
|
|
|
| 16 |
class ToolInput(BaseModel):
|
| 17 |
data: List[Dict]
|
| 18 |
question: str
|
|
@@ -132,11 +133,7 @@ def divide(a: float, b: float):
|
|
| 132 |
return a / b
|
| 133 |
|
| 134 |
|
| 135 |
-
|
| 136 |
-
def open_xlsx_doc(path: str) -> pd.DataFrame:
|
| 137 |
-
""" Open a pandas DataFrame from a xlsx file path"""
|
| 138 |
-
df = pd.read_excel(path)
|
| 139 |
-
return df.to_dict(orient="records")
|
| 140 |
@tool
|
| 141 |
def create_agent_and_answer(input: ToolInput) -> str:
|
| 142 |
""" From a dataframe, can anwser any question
|
|
@@ -146,8 +143,8 @@ def create_agent_and_answer(input: ToolInput) -> str:
|
|
| 146 |
"""
|
| 147 |
df = pd.DataFrame(input.data)
|
| 148 |
question = input.question
|
| 149 |
-
|
| 150 |
-
text =
|
| 151 |
|
| 152 |
return text
|
| 153 |
|
|
@@ -202,15 +199,15 @@ math_agent = create_react_agent(
|
|
| 202 |
|
| 203 |
name="math_agent")
|
| 204 |
|
| 205 |
-
agent_excel =
|
| 206 |
model=llm_4o,
|
| 207 |
|
| 208 |
-
tools=[
|
| 209 |
prompt=(
|
| 210 |
"You are an agent specialized with Excel files.\n\n"
|
| 211 |
"INSTRUCTIONS:\n"
|
| 212 |
"- Assist ONLY when an Excel file is mentionned \n"
|
| 213 |
-
"-
|
| 214 |
"- The output of the first tool is a part of the input of the second tool\n"
|
| 215 |
|
| 216 |
"- Once you have got a response from the 'create_agent_and_answer_tool', transmit it to your supervisor \n"
|
|
@@ -253,20 +250,4 @@ supervisor = create_supervisor(
|
|
| 253 |
add_handoff_back_messages=True,
|
| 254 |
output_mode="full_history",
|
| 255 |
).compile()
|
| 256 |
-
def clean_response(response):
|
| 257 |
-
match = re.search(r'FINAL ANSWER:\s*(.+)', response['supervisor']['messages'][-1].content)
|
| 258 |
-
answer = match.group(1).strip() if match else None
|
| 259 |
-
if answer is None :
|
| 260 |
-
answer = "pas de réponse"
|
| 261 |
-
return answer
|
| 262 |
-
def response_from_agent(question):
|
| 263 |
-
|
| 264 |
-
for chunk in supervisor.stream(
|
| 265 |
-
{"messages": [{"role": "user", "content": question}]}
|
| 266 |
-
):
|
| 267 |
-
|
| 268 |
-
response = chunk
|
| 269 |
-
|
| 270 |
|
| 271 |
-
response = clean_response(response)
|
| 272 |
-
return response
|
|
|
|
| 13 |
from typing import List, Dict
|
| 14 |
import pandas as pd
|
| 15 |
|
| 16 |
+
from fonctions import clean_response, response_from_agent
|
| 17 |
class ToolInput(BaseModel):
|
| 18 |
data: List[Dict]
|
| 19 |
question: str
|
|
|
|
| 133 |
return a / b
|
| 134 |
|
| 135 |
|
| 136 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
@tool
|
| 138 |
def create_agent_and_answer(input: ToolInput) -> str:
|
| 139 |
""" From a dataframe, can anwser any question
|
|
|
|
| 143 |
"""
|
| 144 |
df = pd.DataFrame(input.data)
|
| 145 |
question = input.question
|
| 146 |
+
agent_excel_inter = create_pandas_dataframe_agent(llm_4o, df, verbose=True, allow_dangerous_code=True)
|
| 147 |
+
text = agent_excel_inter.run(question)
|
| 148 |
|
| 149 |
return text
|
| 150 |
|
|
|
|
| 199 |
|
| 200 |
name="math_agent")
|
| 201 |
|
| 202 |
+
agent_excel = create_pandas_dataframe_agent(
|
| 203 |
model=llm_4o,
|
| 204 |
|
| 205 |
+
tools=[create_agent_and_answer],
|
| 206 |
prompt=(
|
| 207 |
"You are an agent specialized with Excel files.\n\n"
|
| 208 |
"INSTRUCTIONS:\n"
|
| 209 |
"- Assist ONLY when an Excel file is mentionned \n"
|
| 210 |
+
"- You will receive the path of a dataframe. '\n"
|
| 211 |
"- The output of the first tool is a part of the input of the second tool\n"
|
| 212 |
|
| 213 |
"- Once you have got a response from the 'create_agent_and_answer_tool', transmit it to your supervisor \n"
|
|
|
|
| 250 |
add_handoff_back_messages=True,
|
| 251 |
output_mode="full_history",
|
| 252 |
).compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
|
|
|
|
|
app.py
CHANGED
|
@@ -78,7 +78,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 78 |
print(f'la question est {question_text}')
|
| 79 |
if filename :
|
| 80 |
print('fichier attaché : ', filename)
|
| 81 |
-
submitted_answer = response_from_agent(question_text + 'The file_name (path) is : ' + filename)
|
| 82 |
print('submitted_answer:', submitted_answer)
|
| 83 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 84 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 78 |
print(f'la question est {question_text}')
|
| 79 |
if filename :
|
| 80 |
print('fichier attaché : ', filename)
|
| 81 |
+
submitted_answer = response_from_agent(supervisor,question_text + 'The file_name (path) is : ' + filename)
|
| 82 |
print('submitted_answer:', submitted_answer)
|
| 83 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 84 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
apt.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
fonctions.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
+
import requests
|
| 4 |
+
import re
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
def clean_response(response):
|
| 8 |
+
match = re.search(r'FINAL ANSWER:\s*(.+)', response['supervisor']['messages'][-1].content)
|
| 9 |
+
answer = match.group(1).strip() if match else None
|
| 10 |
+
if answer is None :
|
| 11 |
+
answer = "pas de réponse"
|
| 12 |
+
return answer
|
| 13 |
+
def response_from_agent(supervisor, question):
|
| 14 |
+
|
| 15 |
+
for chunk in supervisor.stream(
|
| 16 |
+
{"messages": [{"role": "user", "content": question}]}
|
| 17 |
+
):
|
| 18 |
+
|
| 19 |
+
response = chunk
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
response = clean_response(response)
|
| 23 |
+
return response
|
| 24 |
+
def load_data(question):
|
| 25 |
+
task_id = question['task_id']
|
| 26 |
+
file_name = question['file_name']
|
| 27 |
+
|
| 28 |
+
if file_name == "":
|
| 29 |
+
return 'There is no attached file'
|
| 30 |
+
|
| 31 |
+
files_response = requests.get(f"https://agents-course-unit4-scoring.hf.space/files/{task_id}")
|
| 32 |
+
if files_response.status_code == 404:
|
| 33 |
+
return 'Le lien ne fonctionne pas'
|
| 34 |
+
if file_name.endswith('.xlsx'):
|
| 35 |
+
excel_data = BytesIO(files_response.content)
|
| 36 |
+
df = pd.read_excel(excel_data, engine='openpyxl')
|
| 37 |
+
return df
|
| 38 |
+
|
| 39 |
+
elif file_name.endswith('.png'):
|
| 40 |
+
filename = f"fichier_{task_id}.png"
|
| 41 |
+
with open(filename, "wb") as f:
|
| 42 |
+
f.write(files_response.content)
|
| 43 |
+
return filename
|
| 44 |
+
|
| 45 |
+
elif file_name.endswith('.mp3'):
|
| 46 |
+
audio_bytes = BytesIO(files_response.content)
|
| 47 |
+
audio = AudioSegment.from_file(audio_bytes, format="mp3")
|
| 48 |
+
|
| 49 |
+
return audio
|
requirements.txt
CHANGED
|
@@ -10,4 +10,7 @@ arxiv
|
|
| 10 |
pymupdf
|
| 11 |
langchain-experimental
|
| 12 |
tabulate
|
| 13 |
-
pandas
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
pymupdf
|
| 11 |
langchain-experimental
|
| 12 |
tabulate
|
| 13 |
+
pandas
|
| 14 |
+
pydub
|
| 15 |
+
torch
|
| 16 |
+
numpy
|