tsi-org commited on
Commit
ba40e2e
·
verified ·
1 Parent(s): 294dfa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -64
app.py CHANGED
@@ -5,13 +5,15 @@ from groq import Groq
5
  # Predefined password
6
  CORRECT_PASSWORD = "password123"
7
 
8
- def check_password(password):
9
- return password == CORRECT_PASSWORD
10
-
11
- def transcribe_audio(api_key, audio_file=None):
12
- client = Groq(api_key=api_key) # Initialize Groq client with user-provided key
13
-
14
- if audio_file is not None:
 
 
15
  with open(audio_file.name, "rb") as file:
16
  transcription = client.audio.transcriptions.create(
17
  file=(audio_file.name, file.read()),
@@ -19,63 +21,33 @@ def transcribe_audio(api_key, audio_file=None):
19
  temperature=1,
20
  response_format="verbose_json",
21
  )
22
- return transcription.text
23
- else:
24
- return "No audio file provided."
25
-
26
- # Create the main interface
27
- with gr.Blocks() as demo:
28
- # Password input
29
- password_input = gr.Textbox(type="password", label="Enter Password")
30
- submit_button = gr.Button("Submit")
31
- password_message = gr.Markdown(visible=False)
32
-
33
- # Main app interface
34
- api_key = gr.Textbox(label="Enter Your Groq API Key", type="password", visible=False)
35
- audio_file = gr.File(label="Upload Audio File", visible=False)
36
- transcribe_button = gr.Button("Transcribe", visible=False)
37
- output = gr.Textbox(label="Transcribed Text", visible=False)
38
-
39
- # Function to check password and update visibility
40
- def check_password_and_update(password):
41
- if check_password(password):
42
- return {
43
- password_input: gr.Textbox.update(visible=False),
44
- submit_button: gr.Button.update(visible=False),
45
- password_message: gr.Markdown.update(visible=False),
46
- api_key: gr.Textbox.update(visible=True),
47
- audio_file: gr.File.update(visible=True),
48
- transcribe_button: gr.Button.update(visible=True),
49
- output: gr.Textbox.update(visible=True)
50
- }
51
- else:
52
- return {
53
- password_message: gr.Markdown.update(visible=True, value="Incorrect password. Please try again.")
54
- }
55
-
56
- # Connect components
57
- submit_button.click(
58
- check_password_and_update,
59
- inputs=[password_input],
60
- outputs=[password_input, submit_button, password_message, api_key, audio_file, transcribe_button, output]
61
- )
62
- transcribe_button.click(
63
- transcribe_audio,
64
- inputs=[api_key, audio_file],
65
- outputs=output
66
- )
67
-
68
- # Instructions
69
- gr.Markdown(
70
- """
71
- ## How to use this app:
72
- 1. Enter the correct password to access the app.
73
- 2. Enter your [Groq API Key](https://console.groq.com/keys) in the provided field.
74
- 3. Click on the upload section and provide a supported audio file. Supported audio files include mp3, mp4, mpeg, mpga, m4a, wav, and webm file types.
75
- 4. Click the "Transcribe" button to transcribe your speech.
76
- 5. The transcription will appear in the text box below.
77
  """
78
- )
79
 
80
  if __name__ == "__main__":
81
- demo.launch()
 
5
  # Predefined password
6
  CORRECT_PASSWORD = "password123"
7
 
8
+ def process_input(password, api_key, audio_file):
9
+ if password != CORRECT_PASSWORD:
10
+ return "Incorrect password. Please try again.", "", ""
11
+
12
+ if not api_key or not audio_file:
13
+ return "Password correct. Please enter your API key and upload an audio file.", "", ""
14
+
15
+ try:
16
+ client = Groq(api_key=api_key)
17
  with open(audio_file.name, "rb") as file:
18
  transcription = client.audio.transcriptions.create(
19
  file=(audio_file.name, file.read()),
 
21
  temperature=1,
22
  response_format="verbose_json",
23
  )
24
+ return "Transcription successful", transcription.text, ""
25
+ except Exception as e:
26
+ return f"An error occurred: {str(e)}", "", ""
27
+
28
+ # Create the interface
29
+ iface = gr.Interface(
30
+ fn=process_input,
31
+ inputs=[
32
+ gr.Textbox(type="password", label="Password"),
33
+ gr.Textbox(label="Groq API Key", type="password"),
34
+ gr.File(label="Upload Audio File")
35
+ ],
36
+ outputs=[
37
+ gr.Textbox(label="Status"),
38
+ gr.Textbox(label="Transcribed Text"),
39
+ gr.Audio(label="Audio File") # This will display the uploaded audio file
40
+ ],
41
+ title="Audio Transcription App",
42
+ description="""
43
+ How to use this app:
44
+ 1. Enter the correct password.
45
+ 2. Enter your Groq API Key.
46
+ 3. Upload a supported audio file (mp3, mp4, mpeg, mpga, m4a, wav, or webm).
47
+ 4. Click "Submit" to transcribe your speech.
48
+ 5. The transcription will appear in the "Transcribed Text" box.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  """
50
+ )
51
 
52
  if __name__ == "__main__":
53
+ iface.launch(share=True)