Test / TalktoChatGPT1
unclesamjo's picture
Create TalktoChatGPT1
04173be
import gradio as gr
import openai
import pyttsx3
openai.api_key = "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"
# Global variable to hold the chat history, initialise with system role
conversation = [
{"role": "system", "content": "You are an intelligent professor."}
]
# Add your construct_index function here
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 2048
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index.save_to_disk('index.json')
return index
# Construct the index here
directory_path = "path_to_your_directory" # replace with your directory path
index = construct_index(directory_path)
# transcribe function to record the audio input
def transcribe(audio):
print(audio)
# Whisper API
audio_file = open(audio, "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file)
print(transcript)
# append user's input to conversation
conversation.append({"role": "user", "content": transcript["text"]})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation
)
print(response)
# system_message is the response from ChatGPT API
system_message = response["choices"][0]["message"]["content"]
# append ChatGPT response (assistant role) back to conversation
conversation.append({"role": "assistant", "content": system_message})
# Query the index
index_answer = index.query(transcript["text"])
# Text to speech
engine = pyttsx3.init()
engine.setProperty("rate", 150)
engine.setProperty("voice", "english-us")
engine.save_to_file(system_message, "response.mp3")
engine.runAndWait()
# return response as text and audio
return transcript["text"], system_message, index_answer, "response.mp3"
# Gradio output
bot = gr.Interface(
fn=transcribe,
inputs=gr.Audio(source="microphone", type="filepath"),
outputs=[gr.outputs.Textbox(label="Transcribed Text"), gr.outputs.Textbox(label="API Answer"), gr.outputs.Textbox(label="Index Answer"), "audio"],
)
bot.launch()