Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import whisper
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
medium_en = whisper.load_model("medium.en")
|
| 6 |
+
|
| 7 |
+
def send_results_to_api(data, result_url):
|
| 8 |
+
headers = {"Content-Type": "application/json"}
|
| 9 |
+
response = requests.post(result_url, json= data, headers= headers)
|
| 10 |
+
|
| 11 |
+
if response.status_code == 200:
|
| 12 |
+
return response.json()
|
| 13 |
+
else:
|
| 14 |
+
return {"error": f"Failed to send results to API: {response.status_code}"}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def process_audio(params):
|
| 18 |
+
try:
|
| 19 |
+
params = json.loads(params)
|
| 20 |
+
except json.JSONDecodeError as e:
|
| 21 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
| 22 |
+
|
| 23 |
+
audio_files = params.get("audio_files",[])
|
| 24 |
+
api = params.get("api","")
|
| 25 |
+
job_id = params.get("job_id","")
|
| 26 |
+
|
| 27 |
+
solutions=[]
|
| 28 |
+
|
| 29 |
+
for audio in audio_files:
|
| 30 |
+
result_medium = medium_en.transcribe(audio)
|
| 31 |
+
text = result_medium['text']
|
| 32 |
+
|
| 33 |
+
answer_dict = {}
|
| 34 |
+
answer_dict.update({'file_name':audio, 'text':text})
|
| 35 |
+
solutions.append(answer_dict)
|
| 36 |
+
result_url = f"{api}/{job_id}"
|
| 37 |
+
# send_results_to_api(solutions, result_url)
|
| 38 |
+
|
| 39 |
+
return json.dumps({"solutions":solutions}, indent=4)
|
| 40 |
+
|
| 41 |
+
import gradio as gr
|
| 42 |
+
inputt = gr.Textbox(label="Parameters in json format Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'1001'}")
|
| 43 |
+
outputt = gr.JSON()
|
| 44 |
+
|
| 45 |
+
application = gr.Interface(fn=process_audio, inputs= inputt, outputs = outputt, title = "Audio Transcription with API Integration")
|
| 46 |
+
application.launch()
|