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