main.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. import asyncio
  3. import functools
  4. import io
  5. from typing import Callable
  6. import replicate # very nice API to run AI models; see https://replicate.com/
  7. from nicegui import ui
  8. from nicegui.events import UploadEventArguments
  9. async def io_bound(callback: Callable, *args: any, **kwargs: any):
  10. '''Makes a blocking function awaitable; pass function as first parameter and its arguments as the rest'''
  11. return await asyncio.get_event_loop().run_in_executor(None, functools.partial(callback, *args, **kwargs))
  12. async def transcribe(e: UploadEventArguments):
  13. transcription.text = 'Transcribing...'
  14. model = replicate.models.get('openai/whisper')
  15. prediction = await io_bound(model.predict, audio=io.BytesIO(e.content))
  16. text = prediction.get('transcription', 'no transcription')
  17. transcription.set_text(f'result: "{text}"')
  18. async def generate_image():
  19. image.source = 'https://dummyimage.com/600x400/ccc/000000.png&text=building+image...'
  20. model = replicate.models.get('stability-ai/stable-diffusion')
  21. prediction = await io_bound(model.predict, prompt=prompt.value)
  22. image.source = prediction[0]
  23. # User Interface
  24. with ui.row().style('gap:10em'):
  25. with ui.column():
  26. ui.label('OpenAI Whisper (voice transcription)').classes('text-2xl')
  27. ui.upload(on_upload=transcribe).style('width: 20em')
  28. transcription = ui.label().classes('text-xl')
  29. with ui.column():
  30. ui.label('Stable Diffusion (image generator)').classes('text-2xl')
  31. prompt = ui.input('prompt').style('width: 20em')
  32. ui.button('Generate', on_click=generate_image).style('width: 15em')
  33. image = ui.image().style('width: 60em')
  34. ui.run()