Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -15,6 +15,25 @@ from bs4 import BeautifulSoup
|
|
15 |
from io import BytesIO
|
16 |
from PyPDF2 import PdfReader
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
@tool
|
19 |
def add(a: float, b: float) -> float:
|
20 |
""" Adds two numbers.
|
@@ -109,13 +128,20 @@ def transcribe_audio(audio_file: str, file_extension: str) -> str:
|
|
109 |
with open(filename, 'wb') as file: # opens a new file for writing with a name like, e.g. tmp.mp3
|
110 |
file.write(response.content) # write(w) the binary(b) contents (audio file) to disk
|
111 |
|
112 |
-
#
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
#
|
|
|
116 |
with open(filename, "rb") as audio_content:
|
117 |
transcription = client.audio.transcriptions.create(
|
118 |
-
model="whisper-
|
119 |
file=audio_content
|
120 |
)
|
121 |
return transcription.text
|
@@ -158,15 +184,19 @@ def query_image(query: str, image_url: str) -> str:
|
|
158 |
image_url (str): the image's URL
|
159 |
"""
|
160 |
try:
|
161 |
-
|
|
|
|
|
|
|
|
|
162 |
response = client.responses.create(
|
163 |
-
model="
|
164 |
input=[
|
165 |
{
|
166 |
"role": "user",
|
167 |
"content": [
|
168 |
{"type": "input_text", "text": query},
|
169 |
-
{"type": "input_image","image_url": image_url},
|
170 |
],
|
171 |
}
|
172 |
],
|
@@ -222,4 +252,8 @@ def read_excel(file_url: str) -> str:
|
|
222 |
return df.to_csv(index=False) # convert dataframe to CSV string for easy processing
|
223 |
|
224 |
except Exception as e:
|
225 |
-
return f"read_excel failed: {str(e)}"
|
|
|
|
|
|
|
|
|
|
15 |
from io import BytesIO
|
16 |
from PyPDF2 import PdfReader
|
17 |
|
18 |
+
|
19 |
+
class GroqKeyManager:
|
20 |
+
def __init__(self):
|
21 |
+
self.api_keys = [
|
22 |
+
os.getenv("GROQ_API_KEY"),
|
23 |
+
os.getenv("GROQ_API_KEY_1"),
|
24 |
+
os.getenv("GROQ_API_KEY_2"),
|
25 |
+
]
|
26 |
+
self.key_index = 0
|
27 |
+
|
28 |
+
def get_next_key(self):
|
29 |
+
api_key = self.api_keys[self.key_index]
|
30 |
+
self.key_index = (self.key_index + 1) % len(self.api_keys)
|
31 |
+
return api_key
|
32 |
+
|
33 |
+
# Create a global instance of the key manager
|
34 |
+
groq_key_manager = GroqKeyManager()
|
35 |
+
|
36 |
+
|
37 |
@tool
|
38 |
def add(a: float, b: float) -> float:
|
39 |
""" Adds two numbers.
|
|
|
128 |
with open(filename, 'wb') as file: # opens a new file for writing with a name like, e.g. tmp.mp3
|
129 |
file.write(response.content) # write(w) the binary(b) contents (audio file) to disk
|
130 |
|
131 |
+
# Get the next Groq API key
|
132 |
+
api_key = groq_key_manager.get_next_key()
|
133 |
+
|
134 |
+
# Initialize the client with Groq's base URL and API key
|
135 |
+
client = OpenAI(
|
136 |
+
base_url="https://api.groq.com/openai/v1",
|
137 |
+
api_key=api_key
|
138 |
+
)
|
139 |
|
140 |
+
# Note: Check if Groq supports audio transcription
|
141 |
+
# If not, you might need to keep using OpenAI's Whisper for this specific function
|
142 |
with open(filename, "rb") as audio_content:
|
143 |
transcription = client.audio.transcriptions.create(
|
144 |
+
model="whisper-large-v3-turbo", # Check if Groq has an equivalent model
|
145 |
file=audio_content
|
146 |
)
|
147 |
return transcription.text
|
|
|
184 |
image_url (str): the image's URL
|
185 |
"""
|
186 |
try:
|
187 |
+
api_key = groq_key_manager.get_next_key()
|
188 |
+
client = OpenAI(
|
189 |
+
base_url="https://api.groq.com/openai/v1",
|
190 |
+
api_key=api_key
|
191 |
+
)
|
192 |
response = client.responses.create(
|
193 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
194 |
input=[
|
195 |
{
|
196 |
"role": "user",
|
197 |
"content": [
|
198 |
{"type": "input_text", "text": query},
|
199 |
+
{"type": "input_image", "image_url": image_url},
|
200 |
],
|
201 |
}
|
202 |
],
|
|
|
252 |
return df.to_csv(index=False) # convert dataframe to CSV string for easy processing
|
253 |
|
254 |
except Exception as e:
|
255 |
+
return f"read_excel failed: {str(e)}"
|
256 |
+
|
257 |
+
|
258 |
+
|
259 |
+
|