# How to record and transcribe text and send to ChatInterface

**URL:** <https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478>\
**Category:** Showcase\
**Tags:** panel\
**Created:** [July 22, 2024, 5:31pm UTC](https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478 "2024-07-22T17:31:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [July 22, 2024, 5:31pm UTC](https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478/1 "2024-07-22T17:31:46Z")

</div>

```python
import panel as pn
import speech_recognition as sr

from marvin.ai.audio import transcribe
from marvin.audio import record_phrase
from openai import AsyncOpenAI
pn.extension()

def recognize_speech(instance, event):
    with instance.active_widget.param.update(loading=True):
        audio = record_phrase()
        transcription = transcribe(audio)
        try:
            instance.active_widget.value = transcription
        except sr.RequestError as e:
            instance.stream("Could not request results; {0}".format(e), user="System")
        except sr.UnknownValueError:
            instance.stream("Unknown error occurred", user="System")

async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    messages = instance.serialize()
    response = await aclient.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        stream=True,
    )
    message = ""
    async for chunk in response:
        part = chunk.choices[0].delta.content
        if part is not None:
            message += part
            yield message

aclient = AsyncOpenAI()
chat = pn.chat.ChatInterface(
    callback=callback,
    button_properties={"speak": {"callback": recognize_speech, "icon": "microphone"}},
    show_rerun=False,
    show_undo=False,
    show_clear=False,
)
chat.servable()

```

---

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [July 22, 2024, 5:51pm UTC](https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478/2 "2024-07-22T17:51:56Z")

</div>

You can also make it speak back!

```python
import panel as pn
import speech_recognition as sr

from marvin.ai.audio import transcribe, speak_async
from marvin.audio import record_phrase
from openai import AsyncOpenAI
pn.extension()

def recognize_speech(instance, event):
    with instance.active_widget.param.update(loading=True), instance.param.update(loading=True):
        audio = record_phrase()
        transcription = transcribe(audio)
        instance.active_widget.value = transcription

async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    messages = instance.serialize()
    response = await aclient.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        stream=True,
    )
    message = ""
    async for chunk in response:
        part = chunk.choices[0].delta.content
        if part is not None:
            message += part
            yield message
    await (await speak_async(message, voice="shimmer")).play_async()

aclient = AsyncOpenAI()
chat = pn.chat.ChatInterface(
    callback=callback,
    button_properties={"speak": {"callback": recognize_speech, "icon": "microphone"}},
    show_rerun=False,
    show_undo=False,
    show_clear=False,
)
chat.servable()

```

---

<div class="post-metadata">

**Author:** ![awesomebytes](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/awesomebytes/32/6787_2.png) [@awesomebytes](https://discourse.holoviz.org/u/awesomebytes)\
**Post date:** [July 26, 2024, 8:28am UTC](https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478/3 "2024-07-26T08:28:02Z")

</div>

Thank you @ahuang11 so much for your example!

I have 2 little issues with your proposed approach:

1. I understand this is recording the audio of the machine running the code (note: I did some minor research on marvin and it’s dependencies speech\_recognition and pyaudio). I aim to deploy an app in a server, and people will access through the browser, so doing `marvin.audio.record_phrase` will not work, as it defaults to capture from the audio card of the computer where it is running. So I was aiming to use Panel’s [SpeechToText](https://panel.holoviz.org/reference/widgets/SpeechToText.html) as it does the JS magic to capture the audio (and send to the default’s browser implementations of STT). I also tried to find a ‘record audio’ via the browser in Panel but I had no luck.
2. This uses OpenAI in the background, which implies a necessary API key, which implies paying for it. Albeit I’m not against it (in the end I’ll be using some LLM that most probably will be paid) having the “free” option given by the browser for both STT and TTS (I know in the end it basically sends it to google and there’s a limited amount until getting blocked/charged for stt) is a great way to develop and test.

Overall, I would like to, again, thank you SO MUCH, for spending your time and energies helping me (this convo [started in github](https://github.com/holoviz/panel/pull/6959)). If you, or any other kind soul, knows how to make a widget to just capture audio via the browser in panel (so I can then redirect that audio to any STT service of my choosing), I’d love to learn about it. I’m not well versed in JS, and just getting into learning Panel (which seems to use Bokeh in the background?) so I may be hitting my head against a wall for a while if I try to implement it myself.

Have a great day/night!

---

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [July 26, 2024, 6:25pm UTC](https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478/4 "2024-07-26T18:25:45Z")

</div>

Thanks for the feedback. For SpeechToText can you submit an issue on GitHub to make it workable with ChatInterface?

For point #2 for speech to text, I think you can use speech\_recognition without marvin and use `recognize_whisper` which will run on your own machine. (I recommend just asking ChatGPT/Claude on how to migrate to vanilla speech\_recongition). You can also use elevenlabs for text to speech under the free tier.

---

<div class="post-metadata">

**Author:** ![awesomebytes](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/awesomebytes/32/6787_2.png) [@awesomebytes](https://discourse.holoviz.org/u/awesomebytes)\
**Post date:** [July 26, 2024, 6:58pm UTC](https://discourse.holoviz.org/t/how-to-record-and-transcribe-text-and-send-to-chatinterface/7478/5 "2024-07-26T18:58:12Z")

</div>

Done @ahuang11 [Integrate SpeechToText with ChatInterface · Issue #7021 · holoviz/panel · GitHub](https://github.com/holoviz/panel/issues/7021) 🙂
