Jack
commited on
Commit
·
07ced72
1
Parent(s):
f1db1a2
removed comments from app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,11 @@ import os
|
|
5 |
|
6 |
os.environ["HF_HOME"] = "/app/.cache"
|
7 |
|
8 |
-
# Model path for the fine-tuned Whisper model
|
9 |
model_path = "jacktol/whisper-medium.en-fine-tuned-for-ATC-faster-whisper"
|
10 |
|
11 |
-
# Initialize the Whisper model and OpenAI client
|
12 |
whisper_model = WhisperModel(model_path, device="cpu", compute_type="float32")
|
13 |
client = AsyncOpenAI()
|
14 |
|
15 |
-
# System prompt for converting transcript to standard ATC syntax
|
16 |
system_prompt = """Convert the provided transcript into standard pilot-ATC syntax without altering the content.
|
17 |
Ensure that all runway and heading numbers are formatted correctly (e.g., '11L' for 'one one left'). Use standard
|
18 |
aviation phraseology wherever applicable. Maintain the segmentation of the transcript as provided, but exclude the timestamps.
|
@@ -20,26 +17,21 @@ Based on the context and segmentation of each transmission, label it as either '
|
|
20 |
response place a horizontal div with "---" and then line-break, and then add a H2 which says "Transcription", and then
|
21 |
proceed with the transcription."""
|
22 |
|
23 |
-
# Function to transcribe audio and return the concatenated transcript with segment info
|
24 |
def transcribe_audio(file_path):
|
25 |
segments, info = whisper_model.transcribe(file_path, beam_size=5)
|
26 |
transcript = []
|
27 |
|
28 |
-
# Combine all segments with timestamps
|
29 |
for segment in segments:
|
30 |
transcript.append(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}")
|
31 |
|
32 |
return '\n'.join(transcript).strip()
|
33 |
|
34 |
-
# Start chat session
|
35 |
@cl.on_chat_start
|
36 |
async def on_chat_start():
|
37 |
try:
|
38 |
-
# Initialize the session data
|
39 |
if cl.user_session.get("transcription_counter") is None:
|
40 |
cl.user_session.set("transcription_counter", 0)
|
41 |
|
42 |
-
# Full welcome message
|
43 |
welcome_message = """
|
44 |
## Welcome to the **ATC Transcription Assistant**
|
45 |
|
@@ -73,7 +65,6 @@ To get started, upload the audio below.
|
|
73 |
"""
|
74 |
await cl.Message(content=welcome_message).send()
|
75 |
|
76 |
-
# Prompt the user to upload an audio file
|
77 |
files = await cl.AskFileMessage(
|
78 |
content="",
|
79 |
accept={
|
@@ -87,38 +78,30 @@ To get started, upload the audio below.
|
|
87 |
if files:
|
88 |
audio_file = files[0]
|
89 |
|
90 |
-
# Get the full segmented transcription with timestamps
|
91 |
transcription = transcribe_audio(audio_file.path)
|
92 |
|
93 |
-
# Send the entire transcription to the LLM for ATC syntax processing
|
94 |
msg = cl.Message(content="")
|
95 |
await msg.send()
|
96 |
|
97 |
-
# Process the transcription via the LLM
|
98 |
stream = await client.chat.completions.create(
|
99 |
messages=[
|
100 |
{"role": "system", "content": system_prompt},
|
101 |
{"role": "user", "content": transcription},
|
102 |
],
|
103 |
stream=True,
|
104 |
-
model="gpt-4o",
|
105 |
temperature=0,
|
106 |
)
|
107 |
|
108 |
-
# Stream the ATC-processed output
|
109 |
async for part in stream:
|
110 |
token = part.choices[0].delta.content or ""
|
111 |
await msg.stream_token(token)
|
112 |
|
113 |
-
|
114 |
-
await msg.send() # This will mark the end of the streaming process
|
115 |
|
116 |
except Exception as e:
|
117 |
-
# Log any errors that occur during session initialization
|
118 |
print(f"Error during on_chat_start: {str(e)}")
|
119 |
|
120 |
-
# Stop chat session cleanup
|
121 |
@cl.on_stop
|
122 |
async def on_chat_stop():
|
123 |
-
# Clean up any session data or resources here, if needed
|
124 |
print("Session ended, resources cleaned up.")
|
|
|
5 |
|
6 |
os.environ["HF_HOME"] = "/app/.cache"
|
7 |
|
|
|
8 |
model_path = "jacktol/whisper-medium.en-fine-tuned-for-ATC-faster-whisper"
|
9 |
|
|
|
10 |
whisper_model = WhisperModel(model_path, device="cpu", compute_type="float32")
|
11 |
client = AsyncOpenAI()
|
12 |
|
|
|
13 |
system_prompt = """Convert the provided transcript into standard pilot-ATC syntax without altering the content.
|
14 |
Ensure that all runway and heading numbers are formatted correctly (e.g., '11L' for 'one one left'). Use standard
|
15 |
aviation phraseology wherever applicable. Maintain the segmentation of the transcript as provided, but exclude the timestamps.
|
|
|
17 |
response place a horizontal div with "---" and then line-break, and then add a H2 which says "Transcription", and then
|
18 |
proceed with the transcription."""
|
19 |
|
|
|
20 |
def transcribe_audio(file_path):
|
21 |
segments, info = whisper_model.transcribe(file_path, beam_size=5)
|
22 |
transcript = []
|
23 |
|
|
|
24 |
for segment in segments:
|
25 |
transcript.append(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}")
|
26 |
|
27 |
return '\n'.join(transcript).strip()
|
28 |
|
|
|
29 |
@cl.on_chat_start
|
30 |
async def on_chat_start():
|
31 |
try:
|
|
|
32 |
if cl.user_session.get("transcription_counter") is None:
|
33 |
cl.user_session.set("transcription_counter", 0)
|
34 |
|
|
|
35 |
welcome_message = """
|
36 |
## Welcome to the **ATC Transcription Assistant**
|
37 |
|
|
|
65 |
"""
|
66 |
await cl.Message(content=welcome_message).send()
|
67 |
|
|
|
68 |
files = await cl.AskFileMessage(
|
69 |
content="",
|
70 |
accept={
|
|
|
78 |
if files:
|
79 |
audio_file = files[0]
|
80 |
|
|
|
81 |
transcription = transcribe_audio(audio_file.path)
|
82 |
|
|
|
83 |
msg = cl.Message(content="")
|
84 |
await msg.send()
|
85 |
|
|
|
86 |
stream = await client.chat.completions.create(
|
87 |
messages=[
|
88 |
{"role": "system", "content": system_prompt},
|
89 |
{"role": "user", "content": transcription},
|
90 |
],
|
91 |
stream=True,
|
92 |
+
model="gpt-4o",
|
93 |
temperature=0,
|
94 |
)
|
95 |
|
|
|
96 |
async for part in stream:
|
97 |
token = part.choices[0].delta.content or ""
|
98 |
await msg.stream_token(token)
|
99 |
|
100 |
+
await msg.send()
|
|
|
101 |
|
102 |
except Exception as e:
|
|
|
103 |
print(f"Error during on_chat_start: {str(e)}")
|
104 |
|
|
|
105 |
@cl.on_stop
|
106 |
async def on_chat_stop():
|
|
|
107 |
print("Session ended, resources cleaned up.")
|