Speech-to-Text / app.py
hellos's picture
Update app.py
9a20907 verified
raw
history blame
2.93 kB
import whisper
select_model ="base" # ['tiny', 'base']
model = whisper.load_model(select_model)
def store_path_in_json(path, json_file_path="stored_paths.json"):
# Create a dictionary with the path and timestamp
entry = {
"path": path,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
# If the JSON file doesn't exist, create it with an empty list
if not os.path.exists(json_file_path):
with open(json_file_path, 'w') as json_file:
json.dump([], json_file)
try:
# Read existing entries from the JSON file
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
except json.decoder.JSONDecodeError as e:
print(f"Error decoding JSON file: {e}")
print(f"Content of JSON file: {json_file.read()}")
raise # Reraise the exception after printing for further analysis
# Append the new entry to the list
data.append(entry)
# Write the updated list back to the JSON file
with open(json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=2)
# print(f"Path '{path}' stored in '{json_file_path}' with timestamp '{entry['timestamp']}'.")
import os
import json
from datetime import datetime, timedelta
def delete_old_files(json_filename, max_age_hours):
# Load JSON data
if os.path.exists(json_filename):
with open(json_filename, 'r') as json_file:
data = json.load(json_file)
else:
# No data in the JSON file, nothing to delete
return
# Get the current date and time
now = datetime.now()
# Loop through the entries in the JSON file
updated_data = []
for entry in data:
path = entry["path"]
timestamp_str = entry["timestamp"]
creation_date = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')
# Check if the file is older than the specified max age in hours
if (now - creation_date).total_seconds() / 3600 > max_age_hours:
# Delete the file if it exists
if os.path.exists(path):
os.remove(path)
# Skip this entry in the updated data
continue
# Keep the entry in the updated data
updated_data.append(entry)
# Save the updated JSON data
with open(json_filename, 'w') as json_file:
json.dump(updated_data, json_file, indent=2)
def convert_to_text(audio_path):
delete_old_files("stored_paths.json", 1)
store_path_in_json(audio_path)
result = model.transcribe(audio_path,fp16=False)
return result["text"]
import gradio as gr
import os
def transcribe_audio(audio_file):
result=convert_to_text(audio_file)
return result
demo = gr.Interface(transcribe_audio,
[gr.File(label="Upload Audio")],
[gr.Textbox(label="Transcribe")],
)
demo.launch()