Spaces:
Sleeping
Sleeping
feat: add transcribe audio tool
Browse files- requirements.txt +2 -1
- tools.py +70 -0
requirements.txt
CHANGED
|
@@ -5,4 +5,5 @@ smolagents
|
|
| 5 |
duckduckgo-search
|
| 6 |
markdownify
|
| 7 |
gradio[oauth]
|
| 8 |
-
huggingface_hub
|
|
|
|
|
|
| 5 |
duckduckgo-search
|
| 6 |
markdownify
|
| 7 |
gradio[oauth]
|
| 8 |
+
huggingface_hub
|
| 9 |
+
openai
|
tools.py
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
import re
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
from markdownify import markdownify
|
| 4 |
from requests.exceptions import RequestException
|
| 5 |
from smolagents import tool
|
| 6 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
@tool
|
|
@@ -34,6 +39,7 @@ def visit_webpage(url: str) -> str:
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"An unexpected error occurred: {str(e)}"
|
| 36 |
|
|
|
|
| 37 |
@tool
|
| 38 |
def analyze_image(url: str, prompt: str) -> str:
|
| 39 |
"""Uses a vision model to identify features in an describe an image.
|
|
@@ -70,3 +76,67 @@ def analyze_image(url: str, prompt: str) -> str:
|
|
| 70 |
temperature=0.7
|
| 71 |
)
|
| 72 |
description = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
from markdownify import markdownify
|
| 6 |
from requests.exceptions import RequestException
|
| 7 |
from smolagents import tool
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
+
from openai import OpenAI
|
| 10 |
+
from urllib.parse import urlparse
|
| 11 |
+
from pathlib import Path
|
| 12 |
|
| 13 |
|
| 14 |
@tool
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"An unexpected error occurred: {str(e)}"
|
| 41 |
|
| 42 |
+
|
| 43 |
@tool
|
| 44 |
def analyze_image(url: str, prompt: str) -> str:
|
| 45 |
"""Uses a vision model to identify features in an describe an image.
|
|
|
|
| 76 |
temperature=0.7
|
| 77 |
)
|
| 78 |
description = response.choices[0].message.content
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def download_file(url, save_path):
|
| 82 |
+
"""Download a file from a URL and save it locally."""
|
| 83 |
+
try:
|
| 84 |
+
response = requests.get(url, stream=True)
|
| 85 |
+
response.raise_for_status()
|
| 86 |
+
with open(save_path, "wb") as f:
|
| 87 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 88 |
+
f.write(chunk)
|
| 89 |
+
return save_path
|
| 90 |
+
except requests.RequestException as e:
|
| 91 |
+
raise Exception(f"Failed to download file from {url}: {e}")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@tool
|
| 95 |
+
def transcribe_audio(file_path_or_url):
|
| 96 |
+
"""
|
| 97 |
+
Transcribe an MP3 file using OpenAI Whisper API.
|
| 98 |
+
Accepts either a local file path or a URL to an MP3 file.
|
| 99 |
+
"""
|
| 100 |
+
client = OpenAI(
|
| 101 |
+
api_key=os.environ['OPENAI_API_KEY'],
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
# Check if input is a URL
|
| 106 |
+
if file_path_or_url.startswith(("http://", "https://")):
|
| 107 |
+
# Extract filename from URL
|
| 108 |
+
parsed_url = urlparse(file_path_or_url)
|
| 109 |
+
filename = os.path.basename(parsed_url.path) or "downloaded_audio.mp3"
|
| 110 |
+
temp_file_path = os.path.join(os.getcwd(), filename)
|
| 111 |
+
# Download the file
|
| 112 |
+
print(f"Downloading file from {file_path_or_url}...")
|
| 113 |
+
file_path = download_file(file_path_or_url, temp_file_path)
|
| 114 |
+
else:
|
| 115 |
+
# Use local file path
|
| 116 |
+
file_path = file_path_or_url
|
| 117 |
+
if not os.path.exists(file_path):
|
| 118 |
+
raise FileNotFoundError(f"Local file {file_path} does not exist.")
|
| 119 |
+
|
| 120 |
+
# Check file size (Whisper API limit: 25 MB)
|
| 121 |
+
file_size = os.path.getsize(file_path) / (1024 * 1024) # Size in MB
|
| 122 |
+
if file_size > 25:
|
| 123 |
+
raise ValueError(f"File size {file_size:.2f} MB exceeds Whisper API limit of 25 MB.")
|
| 124 |
+
|
| 125 |
+
# Open and send the file to Whisper API
|
| 126 |
+
print(f"Transcribing {file_path}...")
|
| 127 |
+
with open(file_path, "rb") as audio_file:
|
| 128 |
+
transcription = client.audio.transcriptions.create(
|
| 129 |
+
model="whisper-1",
|
| 130 |
+
file=audio_file,
|
| 131 |
+
response_format="text"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# If file was downloaded, clean up
|
| 135 |
+
if file_path_or_url.startswith(("http://", "https://")):
|
| 136 |
+
os.remove(file_path)
|
| 137 |
+
print(f"Cleaned up temporary file: {file_path}")
|
| 138 |
+
|
| 139 |
+
return transcription
|
| 140 |
+
|
| 141 |
+
except Exception as e:
|
| 142 |
+
raise Exception(f"Error during transcription: {e}")
|