Spaces:
Build error
Build error
Daniel commited on
Commit ·
c7aa5fb
1
Parent(s): f8ea276
Test flet
Browse files- Dockerfile +16 -0
- main.py +93 -0
- requirements.txt +1 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . /app
|
| 13 |
+
|
| 14 |
+
# CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 15 |
+
EXPOSE 8000
|
| 16 |
+
CMD ["python", "main.py"]
|
main.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import flet as ft
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
|
| 6 |
+
async def main(page: ft.Page):
|
| 7 |
+
page.title = "Simple Audio Recorder"
|
| 8 |
+
|
| 9 |
+
audio_recorder = ft.AudioRecorder(
|
| 10 |
+
audio_encoder=ft.AudioEncoder.WAV,
|
| 11 |
+
on_state_changed=lambda e: update_recording_status(e.data)
|
| 12 |
+
)
|
| 13 |
+
recording_status = ft.Text()
|
| 14 |
+
audio_player = None # Initialize audio_player as None
|
| 15 |
+
|
| 16 |
+
page.overlay.append(audio_recorder) # Only add audio_recorder to overlay initially
|
| 17 |
+
|
| 18 |
+
page.update()
|
| 19 |
+
|
| 20 |
+
temp_recording_file = None
|
| 21 |
+
|
| 22 |
+
async def start_recording(e):
|
| 23 |
+
record_button.disabled = True
|
| 24 |
+
stop_button.disabled = False
|
| 25 |
+
play_button.disabled = True
|
| 26 |
+
page.update()
|
| 27 |
+
|
| 28 |
+
nonlocal temp_recording_file
|
| 29 |
+
temp_recording_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 30 |
+
print(f"Starting recording to {temp_recording_file.name}")
|
| 31 |
+
await audio_recorder.start_recording_async(temp_recording_file.name)
|
| 32 |
+
page.update()
|
| 33 |
+
|
| 34 |
+
def stop_recording(e):
|
| 35 |
+
record_button.disabled = False
|
| 36 |
+
stop_button.disabled = True
|
| 37 |
+
play_button.disabled = False
|
| 38 |
+
|
| 39 |
+
page.update()
|
| 40 |
+
nonlocal audio_player
|
| 41 |
+
result_uri = audio_recorder.stop_recording()
|
| 42 |
+
print("result_uri")
|
| 43 |
+
print(result_uri)
|
| 44 |
+
|
| 45 |
+
# Create audio_player after recording is complete
|
| 46 |
+
if temp_recording_file:
|
| 47 |
+
with open(temp_recording_file.name, "rb") as audio_file:
|
| 48 |
+
audio_base64 = base64.b64encode(audio_file.read()).decode('utf-8')
|
| 49 |
+
audio_data_uri = f"data:audio/wav;base64,{audio_base64}"
|
| 50 |
+
audio_player = ft.Audio(src=result_uri)
|
| 51 |
+
page.overlay.append(audio_player)
|
| 52 |
+
|
| 53 |
+
page.update()
|
| 54 |
+
|
| 55 |
+
def play_recording(e):
|
| 56 |
+
if audio_player:
|
| 57 |
+
audio_player.play()
|
| 58 |
+
|
| 59 |
+
def update_recording_status(status):
|
| 60 |
+
recording_status.value = f"Recording status: {status}"
|
| 61 |
+
page.update()
|
| 62 |
+
|
| 63 |
+
record_button = ft.ElevatedButton("Start Recording", on_click=start_recording)
|
| 64 |
+
stop_button = ft.ElevatedButton("Stop Recording", on_click=stop_recording, disabled=True)
|
| 65 |
+
play_button = ft.ElevatedButton("Play Recording", on_click=play_recording, disabled=True)
|
| 66 |
+
|
| 67 |
+
async def handle_list_devices(e):
|
| 68 |
+
devices = await audio_recorder.get_input_devices_async()
|
| 69 |
+
print(devices)
|
| 70 |
+
|
| 71 |
+
async def handle_has_permission(e):
|
| 72 |
+
try:
|
| 73 |
+
print(f"HasPermission: {await audio_recorder.has_permission_async()}")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(e)
|
| 76 |
+
|
| 77 |
+
async def handle_audio_encoding_test(e):
|
| 78 |
+
for i in list(ft.AudioEncoder):
|
| 79 |
+
print(f"{i}: {await audio_recorder.is_supported_encoder_async(i)}")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
page.add(
|
| 83 |
+
ft.Text("Simple Audio Recorder", size=24),
|
| 84 |
+
ft.Row([record_button, stop_button, play_button]),
|
| 85 |
+
recording_status,
|
| 86 |
+
ft.ElevatedButton("List Devices", on_click=handle_list_devices),
|
| 87 |
+
ft.ElevatedButton("Test AudioEncodings", on_click=handle_audio_encoding_test),
|
| 88 |
+
ft.ElevatedButton("Has Permission", on_click=handle_has_permission),
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
page.update()
|
| 92 |
+
|
| 93 |
+
ft.app(target=main)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
flet==0.23.2
|