|
import streamlit as st |
|
import requests |
|
from pydub import AudioSegment |
|
from io import BytesIO |
|
|
|
|
|
CTP_DATASCIENCE = st.secrets.get("CTP_DATASCIENCE") |
|
|
|
|
|
headers = {"Authorization": f"Bearer {CTP_DATASCIENCE}"} |
|
|
|
|
|
API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo" |
|
|
|
|
|
def query(file_url): |
|
|
|
response = requests.get(file_url) |
|
if response.status_code == 200: |
|
|
|
audio_file = BytesIO(response.content) |
|
|
|
|
|
audio = AudioSegment.from_file(audio_file) |
|
audio = audio.set_channels(1) |
|
audio = audio.set_frame_rate(16000) |
|
|
|
|
|
audio_buffer = BytesIO() |
|
audio.export(audio_buffer, format="wav") |
|
audio_buffer.seek(0) |
|
|
|
|
|
response_api = requests.post(API_URL, headers=headers, files={"file": audio_buffer}) |
|
|
|
if response_api.status_code == 200: |
|
return response_api.json() |
|
else: |
|
return {"error": "Failed to process the audio with Hugging Face API."} |
|
else: |
|
return {"error": "Failed to download the file from the provided URL."} |
|
|
|
|
|
file_url = "https://huggingface.co/spaces/AndrewLam489/PillID_Transcribe/resolve/main/Greenpill2.mp3" |
|
|
|
|
|
output = query(file_url) |
|
|
|
|
|
st.write(output) |
|
|