santimber commited on
Commit
b28638c
·
1 Parent(s): acb85c6

update file handling

Browse files
Files changed (1) hide show
  1. app.py +23 -39
app.py CHANGED
@@ -66,52 +66,24 @@ class MyAgent(TypedDict):
66
 
67
 
68
  # =========================
69
- # File Handling Functions
70
  # =========================
71
  def process_question_with_files(question_data: dict) -> str:
 
 
 
 
72
  question_text = question_data.get('question', '')
73
  file_name = question_data.get('file_name', '')
74
 
75
  if not file_name:
76
  return question_text
77
 
78
- print(f"📎 Processing question with attached file: {file_name}")
79
- try:
80
- # Download the file
81
- file_url = f"{DEFAULT_API_URL}/files/{file_name}"
82
- local_file_path = f"/tmp/{file_name}"
83
- download_result = download_file(file_url, local_file_path)
84
- if "Failed to download" in download_result:
85
- return f"{question_text}\n\n[Note: Could not download attached file {file_name}]"
86
-
87
- # Route based on file type
88
- ext = file_name.lower().split('.')[-1]
89
- if ext in ['mp3', 'wav', 'm4a', 'flac', 'ogg']:
90
- result = audio_processing_tool.invoke(local_file_path)
91
- tag = "Audio Transcription"
92
- elif ext in ['png', 'jpg', 'jpeg', 'gif', 'bmp']:
93
- result = image_recognition_tool.invoke(local_file_path)
94
- tag = "Image Analysis"
95
- elif ext in ['csv', 'xls', 'xlsx']:
96
- result = read_file_tool.invoke(local_file_path)
97
- tag = "Spreadsheet Content"
98
- elif ext in ['txt', 'md', 'py', 'json']:
99
- result = read_file_tool.invoke(local_file_path)
100
- tag = "File Content"
101
- else:
102
- result = read_file_tool.invoke(local_file_path)
103
- tag = "File Content"
104
-
105
- # Clean up
106
- try:
107
- os.remove(local_file_path)
108
- except Exception:
109
- pass
110
-
111
- return f"{question_text}\n\n[{tag}: {result}]"
112
 
113
- except Exception as e:
114
- return f"{question_text}\n\n[Note: Error processing attached file {file_name}: {str(e)}]"
115
 
116
 
117
  def extract_final_answer(text: str) -> str:
@@ -166,10 +138,22 @@ def assistant(state: MyAgent):
166
  # Add system message to instruct the agent to use the tool
167
  system_message = SystemMessage(content="""
168
  You are a helpful assistant tasked with answering questions using a set of tools.
169
- Now, I will ask you a question. Think step by step and report your answer with the following template:
 
 
 
 
 
 
 
 
 
 
170
  FINAL ANSWER: [YOUR FINAL ANSWER].
 
171
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. 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. 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. If you are asked for a comma separated list, Apply the rules above for each element (number or string), ensure there is exactly one space after each comma.
172
- Your answer should only have the answer.
 
173
  """)
174
 
175
  # Combine system message with user messages
 
66
 
67
 
68
  # =========================
69
+ # Simplified File Handling
70
  # =========================
71
  def process_question_with_files(question_data: dict) -> str:
72
+ """
73
+ Simple file handling - just pass the file info to the agent
74
+ and let it use its tools to handle the file.
75
+ """
76
  question_text = question_data.get('question', '')
77
  file_name = question_data.get('file_name', '')
78
 
79
  if not file_name:
80
  return question_text
81
 
82
+ # Simple approach: just tell the agent about the file
83
+ # Let the agent use its tools to download and process the file
84
+ file_url = f"{DEFAULT_API_URL}/files/{file_name}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ return f"{question_text}\n\n[There is an attached file: {file_name}. You can download it from: {file_url}]"
 
87
 
88
 
89
  def extract_final_answer(text: str) -> str:
 
138
  # Add system message to instruct the agent to use the tool
139
  system_message = SystemMessage(content="""
140
  You are a helpful assistant tasked with answering questions using a set of tools.
141
+
142
+ IMPORTANT: When a question mentions an attached file, you should:
143
+ 1. Use the download_file_from_url_tool to download the file
144
+ 2. Use the appropriate tool to analyze the file content:
145
+ - For images: use image_recognition_tool or extract_text_from_image_tool
146
+ - For audio: use audio_processing_tool
147
+ - For spreadsheets: use analyze_csv_file_tool or analyze_excel_file_tool
148
+ - For text files: use read_file_tool
149
+ - For code files: use python_execution_tool or code_execution_tool
150
+
151
+ Think step by step and report your answer with the following template:
152
  FINAL ANSWER: [YOUR FINAL ANSWER].
153
+
154
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. 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. 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. If you are asked for a comma separated list, Apply the rules above for each element (number or string), ensure there is exactly one space after each comma.
155
+
156
+ Your answer should only have the answer.
157
  """)
158
 
159
  # Combine system message with user messages