peerawatchomp commited on
Commit
ade5302
·
verified ·
1 Parent(s): d4d427d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -2
app.py CHANGED
@@ -4,6 +4,12 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
 
 
 
7
  from typing import TypedDict, Annotated
8
  from langgraph.graph.message import add_messages
9
  from langchain_core.messages import AnyMessage, HumanMessage, AIMessage, SystemMessage
@@ -55,9 +61,10 @@ class BasicAgent:
55
  # Compile the agent
56
  self.agent = builder.compile()
57
 
58
- def __call__(self, question: str) -> str:
59
  print(f"Agent received question (first 50 chars): {question[:50]}...")
60
 
 
61
  system_prompt = """
62
  You are a general AI assistant. I will ask you a question. Report your thoughts, and finish
63
  your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
@@ -70,6 +77,12 @@ class BasicAgent:
70
  If you are asked for a comma separated list, apply the above rules depending of whether the element to be put
71
  in the list is a number or a string.
72
  """
 
 
 
 
 
 
73
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=question)]
74
  response = self.agent.invoke({"messages": messages})
75
 
@@ -82,6 +95,79 @@ class BasicAgent:
82
  print(f"Agent returning fixed answer: {answer}")
83
  return answer
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  def run_and_submit_all( profile: gr.OAuthProfile | None):
86
  """
87
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -265,7 +351,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
265
  print(f"Skipping item with missing task_id or question: {item}")
266
  continue
267
  try:
268
- submitted_answer = agent(question_text)
269
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
270
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
271
  except Exception as e:
 
4
  import inspect
5
  import pandas as pd
6
 
7
+ import speech_recognition as sr
8
+ import magic
9
+ from io import BytesIO
10
+ from pydub import AudioSegment
11
+ from PIL import Image
12
+
13
  from typing import TypedDict, Annotated
14
  from langgraph.graph.message import add_messages
15
  from langchain_core.messages import AnyMessage, HumanMessage, AIMessage, SystemMessage
 
61
  # Compile the agent
62
  self.agent = builder.compile()
63
 
64
+ def __call__(self, question: str, task_id: str) -> str:
65
  print(f"Agent received question (first 50 chars): {question[:50]}...")
66
 
67
+
68
  system_prompt = """
69
  You are a general AI assistant. I will ask you a question. Report your thoughts, and finish
70
  your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
 
77
  If you are asked for a comma separated list, apply the above rules depending of whether the element to be put
78
  in the list is a number or a string.
79
  """
80
+ text_content = download_and_detect_file(
81
+ f"{DEFAULT_API_URL}/files/{task_id}",
82
+ )
83
+ if text_content:
84
+ question += f' attached data: {text_content} '
85
+
86
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=question)]
87
  response = self.agent.invoke({"messages": messages})
88
 
 
95
  print(f"Agent returning fixed answer: {answer}")
96
  return answer
97
 
98
+ def download_and_detect_file(url: str):
99
+ try:
100
+ # Download the file
101
+ response = requests.get(url)
102
+ response.raise_for_status() # Raise error if download failed
103
+
104
+ # Detect MIME type using python-magic (it will try to guess the type)
105
+ mime_type = magic.from_buffer(response.content, mime=True)
106
+ print(f"Detected MIME type: {mime_type}")
107
+
108
+ # Handle the file based on MIME type
109
+ if mime_type == mime_type_xlsx:
110
+ # Handle Excel file
111
+ # Handle Excel file
112
+ excel_data = BytesIO(response.content)
113
+ excel_df = pd.read_excel(excel_data)
114
+
115
+ # Convert DataFrame to a plain text string (could be tabular or a simple concatenation)
116
+ text_content = ""
117
+
118
+ # Iterate through the DataFrame and convert it to text
119
+ for index, row in excel_df.iterrows():
120
+ row_text = ' | '.join([str(cell) for cell in row]) # Join cell values in each row with "|"
121
+ text_content += row_text + "\n" # Add a newline after each row
122
+
123
+ return text_content
124
+
125
+ elif mime_type == mime_type_txt:
126
+ # Handle Text file
127
+ return response.text # Treat it as a text file
128
+
129
+ elif mime_type == mime_type_mp3:
130
+ # Handle MP3 file
131
+ audio_data = BytesIO(response.content)
132
+
133
+ # Convert MP3 to WAV using pydub (speech_recognition only works with WAV, AIFF, or FLAC)
134
+ audio_segment = AudioSegment.from_mp3(audio_data)
135
+ wav_audio = BytesIO()
136
+ audio_segment.export(wav_audio, format="wav")
137
+ wav_audio.seek(0) # Go to the start of the BytesIO object
138
+
139
+ # Perform speech recognition using speech_recognition library
140
+ recognizer = sr.Recognizer()
141
+ with sr.AudioFile(wav_audio) as source:
142
+ audio = recognizer.record(source) # Read the audio file
143
+ try:
144
+ # Use Google Web Speech API to convert speech to text
145
+ text = recognizer.recognize_google(audio)
146
+ return text
147
+ except sr.UnknownValueError:
148
+ return "Google Speech Recognition could not understand the audio"
149
+ except sr.RequestError as e:
150
+ return f"Could not request results from Google Speech Recognition service; {e}"
151
+
152
+ elif mime_type == mime_type_jpg:
153
+ # Handle JPEG Image file
154
+ return url
155
+
156
+ elif mime_type == mime_type_png:
157
+ # Handle PNG Image file
158
+ return url
159
+
160
+ else:
161
+ return f"Unsupported MIME type: {mime_type}"
162
+
163
+ except requests.exceptions.RequestException as e:
164
+ print(f"Error downloading the file: {e}")
165
+ return None
166
+ except Exception as e:
167
+ print(f"An unexpected error occurred: {e}")
168
+ return None
169
+
170
+
171
  def run_and_submit_all( profile: gr.OAuthProfile | None):
172
  """
173
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
351
  print(f"Skipping item with missing task_id or question: {item}")
352
  continue
353
  try:
354
+ submitted_answer = agent(question_text, task_id)
355
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
356
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
357
  except Exception as e: