mobrown commited on
Commit
ca2ff17
·
verified ·
1 Parent(s): 4a8229e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import time
4
+ import os
5
+ from dotenv import load_dotenv
6
+
7
+ # Load AssemblyAI API key from .env
8
+ load_dotenv()
9
+ ASSEMBLYAI_API_KEY = os.getenv("ASSEMBLYAI_API_KEY")
10
+ if not ASSEMBLYAI_API_KEY:
11
+ raise ValueError("Missing ASSEMBLYAI_API_KEY in environment variables.")
12
+
13
+
14
+ def transcribe_with_assemblyai_file(file_path, api_key):
15
+ headers = {"authorization": api_key}
16
+ upload_url = "https://api.assemblyai.com/v2/upload"
17
+
18
+ def read_file(filename, chunk_size=5242880):
19
+ with open(filename, 'rb') as f:
20
+ while True:
21
+ data = f.read(chunk_size)
22
+ if not data:
23
+ break
24
+ yield data
25
+
26
+ print("Uploading file to AssemblyAI...")
27
+ upload_response = requests.post(upload_url, headers=headers, data=read_file(file_path))
28
+ audio_url = upload_response.json().get("upload_url")
29
+ if not audio_url:
30
+ return "Error during file upload."
31
+
32
+ transcript_endpoint = "https://api.assemblyai.com/v2/transcript"
33
+ transcript_request = {"audio_url": audio_url}
34
+ transcript_response = requests.post(transcript_endpoint, json=transcript_request, headers=headers)
35
+ transcript_id = transcript_response.json().get("id")
36
+ if not transcript_id:
37
+ return "Error initiating transcription."
38
+
39
+ polling_url = f"{transcript_endpoint}/{transcript_id}"
40
+ print("Transcription in progress...")
41
+ while True:
42
+ polling_response = requests.get(polling_url, headers=headers).json()
43
+ status = polling_response.get("status")
44
+ if status == "completed":
45
+ return polling_response.get("text", "No transcription text available.")
46
+ elif status == "error":
47
+ return f"Error transcribing audio: {polling_response.get('error')}"
48
+ time.sleep(5)
49
+
50
+
51
+ def transcribe(audio_file_path):
52
+ # If user provides no audio, fallback to CommodityReport.mp3
53
+ file_path = audio_file_path if audio_file_path else "CommodityReport.mp3"
54
+ return transcribe_with_assemblyai_file(file_path, ASSEMBLYAI_API_KEY)
55
+
56
+
57
+ # Create a simplified UI without the upload button
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("# Transcription")
60
+
61
+ # Instructions with HTML for better styling
62
+ gr.HTML("""
63
+ <div class="instructions-container">
64
+
65
+ <h4 class="section-header">USING AN AUDIO FILE:</h4>
66
+ <ul>
67
+ <li>Upload an audio file by dropping it or uploading it in the panel below. (If you don't have an audio file, just click 'Transcribe' and it will transcribe a default file.)</li>
68
+ <li>Click Transcribe</li>
69
+ </ul>
70
+
71
+ <h4 class="section-header">RECORD YOUR OWN AUDIO AND TRANSCRIBE IT:</h4>
72
+ <ul>
73
+ <li>Click the grey microphone icon in the middle above the 'Transcribe' button.</li>
74
+ <li>Then click the 'Record' button and click the 'Stop' button when finished.</li>
75
+ <li>Click Transcribe</li>
76
+ </ul>
77
+ </div>
78
+ """)
79
+
80
+ # Use the original Audio component but make it smaller
81
+ audio_input = gr.Audio(
82
+ label="", # Remove the label to reduce spacing
83
+ type="filepath",
84
+ interactive=True
85
+ )
86
+
87
+ # Add styling to make the audio box smaller and improve visuals
88
+ gr.HTML("""
89
+ <style>
90
+ /* Style the instructions container */
91
+ .instructions-container {
92
+ background-color: #f8f9fa;
93
+ padding: 5px;
94
+ border-radius: 5px;
95
+ margin-bottom: 5px;
96
+ border-left: 4px solid #FF6B00;
97
+ }
98
+
99
+ /* Make the section headers larger */
100
+ .section-header {
101
+ font-size: 14px;
102
+ font-weight: bold;
103
+ margin-top: 5px;
104
+ margin-bottom: 5px;
105
+ color: #333;
106
+ }
107
+
108
+ /* Style list items */
109
+ .instructions-container ul {
110
+ margin-bottom: 5px;
111
+ }
112
+
113
+ .instructions-container li {
114
+ margin-bottom: 5px;
115
+ }
116
+
117
+ /* Highlight the microphone icon */
118
+ .audio-player-footer button:last-child svg {
119
+ color: #888 !important;
120
+ transform: scale(1.2);
121
+ }
122
+
123
+ /* Make the audio box smaller */
124
+ .gradio-audio {
125
+ max-height: 50px;
126
+ overflow: hidden;
127
+ }
128
+
129
+ /* Ensure the audio controls remain visible */
130
+ .audio-player-footer {
131
+ position: relative;
132
+ z-index: 10;
133
+ }
134
+
135
+ /* Make the Transcribe button more prominent */
136
+ .gradio-button.primary {
137
+ font-size: 16px;
138
+ padding: 10px 20px;
139
+ margin: 5px 0;
140
+ background-color: #FF6B00;
141
+ }
142
+ </style>
143
+ """)
144
+
145
+ # Transcription controls - make button more prominent
146
+ transcribe_button = gr.Button("Transcribe", variant="primary", size="lg")
147
+ transcript_output = gr.Textbox(label="Transcript", lines=10)
148
+
149
+ # Connect the transcribe button
150
+ transcribe_button.click(fn=transcribe, inputs=audio_input, outputs=transcript_output)
151
+
152
+ demo.launch()