yosuke-i commited on
Commit
05708b6
1 Parent(s): cf39e54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from openai import OpenAI
3
  import json
4
 
5
  def extract_words(json_data):
@@ -7,15 +7,26 @@ def extract_words(json_data):
7
  return ''.join(words)
8
 
9
  def transcribe_audio(file):
10
- client = OpenAI(api_key="sk-proj-6J4EfDKmIduACsB38LXlT3BlbkFJSVTYDzfbVeAvbKHp1l1z")
11
- transcription = client.audio.transcriptions.create(
12
- model="whisper-1",
13
- language="ja",
14
- response_format="verbose_json",
15
- timestamp_granularities=["word"],
16
- file=file
17
- )
18
- return extract_words(transcription['words'])
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  iface = gr.Interface(
21
  fn=transcribe_audio,
 
1
  import gradio as gr
2
+ import requests
3
  import json
4
 
5
  def extract_words(json_data):
 
7
  return ''.join(words)
8
 
9
  def transcribe_audio(file):
10
+ api_key = "sk-proj-6J4EfDKmIduACsB38LXlT3BlbkFJSVTYDzfbVeAvbKHp1l1z"
11
+ url = "https://api.openai.com/v1/audio/transcriptions"
12
+ headers = {
13
+ "Authorization": f"Bearer {api_key}"
14
+ }
15
+ files = {
16
+ "file": file,
17
+ "model": (None, "whisper-1"),
18
+ "language": (None, "ja"),
19
+ "response_format": (None, "verbose_json"),
20
+ "timestamp_granularities": (None, "word")
21
+ }
22
+
23
+ response = requests.post(url, headers=headers, files=files)
24
+ response_json = response.json()
25
+
26
+ if "words" in response_json:
27
+ return extract_words(response_json['words'])
28
+ else:
29
+ return "Error in transcription: " + response_json.get("error", {}).get("message", "Unknown error")
30
 
31
  iface = gr.Interface(
32
  fn=transcribe_audio,