HoangHa commited on
Commit
b489094
1 Parent(s): c9b7b8b

Upload 2 files

Browse files
Files changed (2) hide show
  1. Step4_voice.py +213 -0
  2. requirement.txt +4 -0
Step4_voice.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Library
2
+ import openai
3
+ import streamlit as st
4
+ import pandas as pd
5
+ from datetime import datetime
6
+ from TTS.api import TTS
7
+ import whisper
8
+ from audio_recorder import record
9
+
10
+ # Custom Streamlit app title and icon
11
+ st.set_page_config(
12
+ page_title="VietAI Bot",
13
+ page_icon=":robot_face:",
14
+ )
15
+
16
+ # Set the title
17
+ st.title("[VietAI-NTI] ChatGPT")
18
+
19
+ # Sidebar Configuration
20
+ st.sidebar.title(":gear: Model Configuration")
21
+
22
+ # Set OPENAI API
23
+ openai.api_key = st.sidebar.text_input('Your OpenAI API key here:')
24
+
25
+ # User Input and AI Response
26
+ user_input_type = st.sidebar.selectbox("Choose input type:", ["Chat", "Record Audio"])
27
+
28
+ # Model Name Selector
29
+ model_name = st.sidebar.selectbox(
30
+ "Select a Model",
31
+ ["gpt-3.5-turbo", "gpt-4"], # Add more model names as needed
32
+ key="model_name",
33
+ )
34
+
35
+ # Temperature Slider
36
+ temperature = st.sidebar.slider(
37
+ ":thermometer: Temperature",
38
+ min_value=0.2,
39
+ max_value=2.0,
40
+ value=1.0,
41
+ step=0.1,
42
+ key="temperature",
43
+ )
44
+
45
+ # Max tokens Slider
46
+ max_tokens = st.sidebar.slider(
47
+ ":straight_ruler: Max Tokens",
48
+ min_value=1,
49
+ max_value=4095,
50
+ value=256,
51
+ step=1,
52
+ key="max_tokens",
53
+ )
54
+
55
+ # Top p Slider
56
+ # top_p = st.sidebar.slider(
57
+ # "🎯 Top P",
58
+ # min_value=0.00,
59
+ # max_value=1.00,
60
+ # value=1.00,
61
+ # step=0.01,
62
+ # key="top_p",
63
+ # )
64
+
65
+ # Presence penalty Slider
66
+ # presence_penalty = st.sidebar.slider(
67
+ # "🚫 Presence penalty",
68
+ # min_value=0.00,
69
+ # max_value=2.00,
70
+ # value=0.00,
71
+ # step=0.01,
72
+ # key="presence_penalty",
73
+ # )
74
+
75
+ # Frequency penalty Slider
76
+ # frequency_penalty = st.sidebar.slider(
77
+ # "🤐 Frequency penalty",
78
+ # min_value=0.00,
79
+ # max_value=2.00,
80
+ # value=0.00,
81
+ # step=0.01,
82
+ # key="frequency_penalty",
83
+ # )
84
+
85
+ # TEXT2SPEECH MODEL
86
+ # Instantiate the TTS class
87
+ tts = TTS(TTS().list_models()[13])
88
+ def convert_2_speech(given_text):
89
+ tts.tts_to_file(text=given_text, file_path="response.wav")
90
+ return("response.wav")
91
+
92
+ # SPEECH2TEXT MODEL
93
+ model_whisper = whisper.load_model("tiny.en")
94
+ def convert_2_text(speech):
95
+ user_message = model_whisper.transcribe(speech)["text"]
96
+ return user_message
97
+
98
+ # CHAT MODEL
99
+ # Initialize DataFrame to store chat history
100
+ chat_history_df = pd.DataFrame(columns=["Timestamp", "Chat"])
101
+
102
+ # Reset Button
103
+ if st.sidebar.button(":arrows_counterclockwise: Reset Chat"):
104
+ # Save the chat history to the DataFrame before clearing it
105
+ if st.session_state.messages:
106
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
107
+ chat_history = "\n".join([f"{m['role']}: {m['content']}" for m in st.session_state.messages])
108
+ new_entry = pd.DataFrame({"Timestamp": [timestamp], "Chat": [chat_history]})
109
+ chat_history_df = pd.concat([chat_history_df, new_entry], ignore_index=True)
110
+
111
+ # Save the DataFrame to a CSV file
112
+ chat_history_df.to_csv("chat_history.csv", index=False)
113
+
114
+ # Clear the chat messages and reset the full response
115
+ st.session_state.messages = []
116
+ full_response = ""
117
+
118
+ # Initialize Chat Messages
119
+ if "messages" not in st.session_state:
120
+ st.session_state.messages = []
121
+
122
+ # Initialize full_response outside the user input check
123
+ full_response = ""
124
+
125
+ # Display Chat History
126
+ for message in st.session_state.messages:
127
+ with st.chat_message(message["role"]):
128
+ st.markdown(message["content"])
129
+
130
+ # User Input and AI Response
131
+ # if st.sidebar.button(":microphone: Record Audio"):
132
+ # st.audio(None, format="audio/wav", start_time=0)
133
+ # recorded_audio = st.audio_recorder(key="audio_recorder")
134
+
135
+ # if recorded_audio:
136
+ # st.success("Recording complete. Click 'Generate Response' to transcribe and generate AI response.")
137
+
138
+
139
+
140
+ # User Input and AI Response
141
+ # For "Chat mode"
142
+ if user_input_type == "Chat":
143
+ if prompt := st.chat_input("What is up?"):
144
+ # System
145
+ st.session_state.messages.append({"role": "system", "content": "You are a helpful assistant named Jarvis"})
146
+
147
+ # User
148
+ st.session_state.messages.append({"role": "user", "content": prompt})
149
+ with st.chat_message("user"):
150
+ st.markdown(prompt)
151
+
152
+ # Assistant
153
+ with st.chat_message("assistant"):
154
+ with st.status("Generating response..."):
155
+ message_placeholder = st.empty()
156
+ for response in openai.ChatCompletion.create(
157
+ model=model_name, # Use the selected model name
158
+ messages=[
159
+ {"role": m["role"], "content": m["content"]}
160
+ for m in st.session_state.messages
161
+ ],
162
+ temperature=temperature, # Set temperature
163
+ max_tokens=max_tokens, # Set max tokens
164
+ top_p=top_p, # Set top p
165
+ frequency_penalty=frequency_penalty, # Set frequency penalty
166
+ presence_penalty=presence_penalty, # Set presence penalty
167
+ stream=True,
168
+ ):
169
+ full_response += response.choices[0].delta.get("content", "")
170
+ message_placeholder.markdown(full_response + "▌")
171
+ message_placeholder.markdown(full_response)
172
+
173
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
174
+ st.audio(convert_2_speech(full_response))
175
+
176
+ elif user_input_type == "Record Audio":
177
+ # Record audio when the "Record Audio" button is clicked
178
+ if st.button("Record Audio"):
179
+ st.write("Recording... Please speak for 10 seconds.")
180
+ output = record(seconds=10, filename='my_recording.wav')
181
+ st.write("Recording complete!")
182
+
183
+ # Convert the recorded audio to text using the Whisper model
184
+ user_message = convert_2_text(output)
185
+
186
+ # Display the transcribed text as user input
187
+ st.session_state.messages.append({"role": "user", "content": user_message})
188
+ with st.chat_message("user"):
189
+ st.markdown(user_message)
190
+
191
+ # Assistant
192
+ with st.chat_message("assistant"):
193
+ with st.status("Generating response..."):
194
+ message_placeholder = st.empty()
195
+ for response in openai.ChatCompletion.create(
196
+ model=model_name, # Use the selected model name
197
+ messages=[
198
+ {"role": m["role"], "content": m["content"]}
199
+ for m in st.session_state.messages
200
+ ],
201
+ temperature=temperature, # Set temperature
202
+ max_tokens=max_tokens, # Set max tokens
203
+ top_p=top_p, # Set top p
204
+ frequency_penalty=frequency_penalty, # Set frequency penalty
205
+ presence_penalty=presence_penalty, # Set presence penalty
206
+ stream=True,
207
+ ):
208
+ full_response += response.choices[0].delta.get("content", "")
209
+ message_placeholder.markdown(full_response + "▌")
210
+ message_placeholder.markdown(full_response)
211
+
212
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
213
+ st.audio(convert_2_speech(full_response))
requirement.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ openai
2
+ openai-whisper
3
+ streamlit
4
+ TTS == 0.17.4