init!
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from audiorecorder import audiorecorder
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
open_ai = OpenAI(
|
8 |
+
api_key="sk-dW5stjNmgnwJb00p-Hk-oQCwGhHPNMoT9Nrx4yQvH4T3BlbkFJgoCDc7RdDufCuXRQNCpcq_m4oUswu217zkojwaxw4A")
|
9 |
+
|
10 |
+
st.set_page_config(
|
11 |
+
layout="centered",
|
12 |
+
page_icon="🤖",
|
13 |
+
page_title="Voice Chat"
|
14 |
+
)
|
15 |
+
|
16 |
+
st.title("Voice Chat")
|
17 |
+
|
18 |
+
if 'chat' not in st.session_state:
|
19 |
+
st.session_state.chat = []
|
20 |
+
|
21 |
+
|
22 |
+
def recordAudio():
|
23 |
+
audio = audiorecorder("Click to record", "Click to stop recording")
|
24 |
+
if len(audio) > 0:
|
25 |
+
audio.export("audio.wav", format="wav")
|
26 |
+
userPrompt = speechToText()
|
27 |
+
st.text(userPrompt)
|
28 |
+
st.session_state.chat.append({
|
29 |
+
'role': 'user',
|
30 |
+
'content': userPrompt
|
31 |
+
})
|
32 |
+
|
33 |
+
answer = getAnswer(userPrompt)
|
34 |
+
st.session_state.chat.append({
|
35 |
+
'role': 'assistant',
|
36 |
+
'content': answer
|
37 |
+
})
|
38 |
+
textToSpeech(answer)
|
39 |
+
st.audio("reply.wav", autoplay=True)
|
40 |
+
|
41 |
+
|
42 |
+
def speechToText():
|
43 |
+
audio_file = open("audio.wav", "rb")
|
44 |
+
transcription = open_ai.audio.transcriptions.create(
|
45 |
+
model="whisper-1",
|
46 |
+
file=audio_file
|
47 |
+
)
|
48 |
+
return transcription.text
|
49 |
+
|
50 |
+
|
51 |
+
def textToSpeech(text):
|
52 |
+
speech_file_path = Path(__file__).parent / "reply.wav"
|
53 |
+
response = open_ai.audio.speech.create(
|
54 |
+
model="tts-1",
|
55 |
+
voice="alloy",
|
56 |
+
input=text
|
57 |
+
)
|
58 |
+
|
59 |
+
response.stream_to_file(speech_file_path)
|
60 |
+
|
61 |
+
|
62 |
+
def getAnswer(question):
|
63 |
+
response = open_ai.chat.completions.create(
|
64 |
+
model="gpt-4o-mini",
|
65 |
+
messages=[
|
66 |
+
{"role": "system", "content": "You are a helpful assistant who can uderstand native urdu andwho provide very very short response in urdu"},
|
67 |
+
*st.session_state.chat,
|
68 |
+
{"role": "user", "content": question}
|
69 |
+
]
|
70 |
+
)
|
71 |
+
|
72 |
+
return response.choices[0].message.content
|
73 |
+
|
74 |
+
|
75 |
+
recordAudio()
|