| """import gradio as gr |
| from huggingface_hub import InferenceClient |
| |
| |
| def respond( |
| message, |
| history: list[dict[str, str]], |
| system_message, |
| max_tokens, |
| temperature, |
| top_p, |
| hf_token: gr.OAuthToken, |
| ): |
| |
| #For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference |
| |
| client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b") |
| |
| messages = [{"role": "system", "content": system_message}] |
| |
| messages.extend(history) |
| |
| messages.append({"role": "user", "content": message}) |
| |
| response = "" |
| |
| for message in client.chat_completion( |
| messages, |
| max_tokens=max_tokens, |
| stream=True, |
| temperature=temperature, |
| top_p=top_p, |
| ): |
| choices = message.choices |
| token = "" |
| if len(choices) and choices[0].delta.content: |
| token = choices[0].delta.content |
| |
| response += token |
| yield response |
| |
| |
| |
| #For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface |
| |
| chatbot = gr.ChatInterface( |
| respond, |
| type="messages", |
| additional_inputs=[ |
| gr.Textbox(value="You are a friendly Chatbot.", label="System message"), |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
| gr.Slider( |
| minimum=0.1, |
| maximum=1.0, |
| value=0.95, |
| step=0.05, |
| label="Top-p (nucleus sampling)", |
| ), |
| ], |
| ) |
| |
| with gr.Blocks() as demo: |
| with gr.Sidebar(): |
| gr.LoginButton() |
| chatbot.render() |
| |
| |
| if __name__ == "__main__": |
| demo.launch() |
| """ |
| import gradio as gr |
| import requests |
| from huggingface_hub import InferenceClient |
|
|
| DEEPGRAM_API_KEY = "0c72698eb40f85fc25b56a76039e795be653afed" |
|
|
| def deepgram_stt(audio_file_path): |
| |
| |
| |
| url = "https://api.deepgram.com/v1/listen" |
| headers = { |
| "Authorization": f"Token {DEEPGRAM_API_KEY}", |
| "Content-Type": "audio/wav" |
| } |
|
|
| with open(audio_file_path, "rb") as f: |
| audio = f.read() |
|
|
| response = requests.post(url, headers=headers, data=audio).json() |
| return response["results"]["channels"][0]["alternatives"][0]["transcript"] |
|
|
|
|
| def deepgram_tts(text): |
| |
| |
| |
| url = "https://api.deepgram.com/v1/speak?model=aura-asteria-en" |
| headers = { |
| "Authorization": f"Token {DEEPGRAM_API_KEY}", |
| "Content-Type": "application/json" |
| } |
|
|
| payload = {"text": text} |
|
|
| audio_out = "response.wav" |
| r = requests.post(url, json=payload, headers=headers) |
|
|
| with open(audio_out, "wb") as f: |
| f.write(r.content) |
|
|
| return audio_out |
|
|
|
|
| def respond_audio( |
| audio_input, |
| history, |
| system_message, |
| max_tokens, |
| temperature, |
| top_p, |
| hf_token: gr.OAuthToken, |
| ): |
| |
| |
| |
| client = InferenceClient( |
| token=hf_token.token, |
| model="openai/gpt-oss-20b" |
| ) |
|
|
| |
| user_message = deepgram_stt(audio_input) |
|
|
| messages = [{"role": "system", "content": system_message}] |
| messages.extend(history) |
| messages.append({"role": "user", "content": user_message}) |
|
|
| |
| response_text = "" |
| for message in client.chat_completion( |
| messages, |
| max_tokens=max_tokens, |
| stream=True, |
| temperature=temperature, |
| top_p=top_p, |
| ): |
| if len(message.choices) and message.choices[0].delta.content: |
| response_text += message.choices[0].delta.content |
| yield response_text, None |
|
|
| |
| audio_file = deepgram_tts(response_text) |
|
|
| yield response_text, audio_file |
|
|
|
|
| with gr.Blocks() as demo: |
| with gr.Sidebar(): |
| gr.LoginButton() |
|
|
| gr.Markdown("## 🎤 Voice Chat Mode (Deepgram + GPT-OSS)") |
|
|
| |
| with gr.Accordion("Optional: Type Instead of Speaking", open=False): |
| typed_message = gr.Textbox(label="Manual Text Input") |
| |
| chatbot = gr.Chatbot(type="messages") |
| |
| audio_in = gr.Audio(label="Press to Speak", type="filepath") |
| audio_out = gr.Audio(label="TTS Output") |
|
|
| system_message = gr.Textbox( |
| value="You are a friendly Chatbot.", |
| label="System message" |
| ) |
| max_tokens = gr.Slider(1, 2048, value=512, label="Max new tokens") |
| temp = gr.Slider(0.1, 4.0, value=0.7, label="Temperature") |
| top_p = gr.Slider(0.1, 1.0, value=0.95, label="Top-p") |
|
|
| send_button = gr.Button("Send (Voice)") |
|
|
| send_button.click( |
| respond_audio, |
| inputs=[audio_in, chatbot, system_message, max_tokens, temp, top_p], |
| outputs=[chatbot, audio_out] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |