save_audios / audio_save.py
tttarun's picture
Update audio_save.py
b22ed7b verified
raw
history blame
2.31 kB
from fastapi import FastAPI, File, Form, UploadFile
import shutil
import os
from huggingface_hub import HfApi,HfFileSystem
app = FastAPI()
if not os.path.exists('./tarun_1234'):
os.mkdir('./tarun_1234')
hf_token = os.environ['HF_TOKEN']
api = HfApi(token = hf_token)
@app.post("/save_audio")
async def save_audio(audio: UploadFile = File(...), path: str = Form(...), script_name: str = Form(...)):
# print('PATH ************************ >>>>>', path)
folder_path = path.split('/')[1]
filename = path.split('/')[-1]
# script_name = script_name
count = path.split('/')[-1].split('.')[0]
# print('FOLDER PATH ----------->>>>>>>>>>>>>>', folder_path)
if not os.path.exists(folder_path):
os.mkdir(folder_path)
print("Received Audio !!!!!")
# Save the audio to the specified path
with open(path, "wb") as buffer:
shutil.copyfileobj(audio.file, buffer)
# url = 'https://hellojarvis-asr-hindi.hf.space/transcribe'
# headers = {
# 'Authorization': f'Bearer {hf_token}',
# 'accept': 'application/json'
# }
# with open(path, 'rb') as file:
# files = {'file': file}
# print('*********** Reached Here *******************')
# response = requests.post(SERVER_URL, headers=headers, files=files)
# if response.status_code == 200:
# transcription = response.json()
# # return transcription
# else:
# # return "Failed to transcribe audio", None
# if os.path.exists(f'{folder_path}.csv'):
# df = pd.read_csv(f'{folder_path}.csv')
# else:
# df = pd.DataFrame(columns = ['script','response','transcription'])
# df1 = pd.DataFrame(l,columns = ['script','response','transcription'])
api.upload_file(
path_or_fileobj=path,
path_in_repo=f"{folder_path}/{script_name}/{filename}",
repo_id="hellojarvis/agent_data",
repo_type="dataset",)
# api.upload_file(
# path_or_fileobj=f'{folder_path}/transcription/state_wise_duplicates.csv',
# path_in_repo=f"{folder_path}.csv",
# repo_id="hellojarvis/agent_test",
# repo_type="dataset",)
return {"message": "Audio saved successfully"}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)