Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- src/agent.py +19 -12
- src/tools.py +20 -1
src/agent.py
CHANGED
|
@@ -2,6 +2,7 @@ import base64
|
|
| 2 |
import json
|
| 3 |
from os.path import join
|
| 4 |
|
|
|
|
| 5 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 6 |
from langchain_core.rate_limiters import InMemoryRateLimiter
|
| 7 |
from langchain_openai.chat_models import ChatOpenAI
|
|
@@ -130,7 +131,7 @@ class Agent:
|
|
| 130 |
:param question_file: The file that comes with the question.
|
| 131 |
:return: Formatted HumanMessage.
|
| 132 |
"""
|
| 133 |
-
if question_file is None:
|
| 134 |
human_message = HumanMessage(content=question)
|
| 135 |
else:
|
| 136 |
if '.png' in question_file:
|
|
@@ -152,21 +153,15 @@ class Agent:
|
|
| 152 |
]
|
| 153 |
)
|
| 154 |
elif '.mp3' in question_file:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
decode("utf-8")
|
| 158 |
human_message = HumanMessage(
|
| 159 |
content=[
|
| 160 |
{
|
| 161 |
'type': 'text',
|
| 162 |
-
'text': question
|
|
|
|
| 163 |
},
|
| 164 |
-
{
|
| 165 |
-
'type': 'audio',
|
| 166 |
-
'source_type': 'base64',
|
| 167 |
-
'data': file_content,
|
| 168 |
-
'mime_type': 'audio/mp3'
|
| 169 |
-
}
|
| 170 |
]
|
| 171 |
)
|
| 172 |
elif '.py' in question_file:
|
|
@@ -182,7 +177,19 @@ class Agent:
|
|
| 182 |
]
|
| 183 |
)
|
| 184 |
elif '.xlsx' in question_file:
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
return human_message
|
| 187 |
|
| 188 |
|
|
|
|
| 2 |
import json
|
| 3 |
from os.path import join
|
| 4 |
|
| 5 |
+
import pandas as pd
|
| 6 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 7 |
from langchain_core.rate_limiters import InMemoryRateLimiter
|
| 8 |
from langchain_openai.chat_models import ChatOpenAI
|
|
|
|
| 131 |
:param question_file: The file that comes with the question.
|
| 132 |
:return: Formatted HumanMessage.
|
| 133 |
"""
|
| 134 |
+
if question_file is None or question_file == '':
|
| 135 |
human_message = HumanMessage(content=question)
|
| 136 |
else:
|
| 137 |
if '.png' in question_file:
|
|
|
|
| 153 |
]
|
| 154 |
)
|
| 155 |
elif '.mp3' in question_file:
|
| 156 |
+
# There is no support for audio fileswhen using gpt-4o
|
| 157 |
+
# So, I will use a tools to record the .mp3 file in text
|
|
|
|
| 158 |
human_message = HumanMessage(
|
| 159 |
content=[
|
| 160 |
{
|
| 161 |
'type': 'text',
|
| 162 |
+
'text': f'''{question}\n\nHere is the audio file:
|
| 163 |
+
```audio\n{question_file}\n```'''
|
| 164 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
]
|
| 166 |
)
|
| 167 |
elif '.py' in question_file:
|
|
|
|
| 177 |
]
|
| 178 |
)
|
| 179 |
elif '.xlsx' in question_file:
|
| 180 |
+
data = pd.read_excel(
|
| 181 |
+
join(self.data_path, question_file),
|
| 182 |
+
)
|
| 183 |
+
data = data.to_string()
|
| 184 |
+
human_message = HumanMessage(
|
| 185 |
+
content=[
|
| 186 |
+
{
|
| 187 |
+
'type': 'text',
|
| 188 |
+
'text': f'''{question}\n\nHere is the data:
|
| 189 |
+
```\n{data}\n```'''
|
| 190 |
+
},
|
| 191 |
+
]
|
| 192 |
+
)
|
| 193 |
return human_message
|
| 194 |
|
| 195 |
|
src/tools.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
from langchain_core.tools import tool
|
| 5 |
|
| 6 |
|
|
@@ -86,3 +88,20 @@ def wiki_search(query: str) -> str:
|
|
| 86 |
for doc in search_docs
|
| 87 |
]
|
| 88 |
return json.dumps({"wiki_results": formatted_search_docs})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
+
|
| 4 |
+
from langchain_community.document_loaders import \
|
| 5 |
+
AssemblyAIAudioTranscriptLoader, WikipediaLoader
|
| 6 |
from langchain_core.tools import tool
|
| 7 |
|
| 8 |
|
|
|
|
| 88 |
for doc in search_docs
|
| 89 |
]
|
| 90 |
return json.dumps({"wiki_results": formatted_search_docs})
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@tool
|
| 94 |
+
def audio_transcript(
|
| 95 |
+
audiofile_path: str
|
| 96 |
+
) -> str:
|
| 97 |
+
"""
|
| 98 |
+
Transcribe an audio file to text.
|
| 99 |
+
|
| 100 |
+
Args:
|
| 101 |
+
audiofile_path: The path to the audio file to transcribe.
|
| 102 |
+
"""
|
| 103 |
+
loader = AssemblyAIAudioTranscriptLoader(
|
| 104 |
+
file_path=audiofile_path,
|
| 105 |
+
)
|
| 106 |
+
docs = loader.load(file_path=audiofile_path)
|
| 107 |
+
return docs[0].page_content if docs else "No transcription available."
|