anupam_speech / app.py
gradioapp's picture
Update app.py
4a2eb2b
raw
history blame
No virus
3.05 kB
import openai
import gradio as gr
import os
from gtts import gTTS
from pdfminer.high_level import extract_text
openai.api_key = os.getenv("OPENAI_API_KEY")
from transformers import pipeline
p = pipeline("automatic-speech-recognition",model="openai/whisper-tiny")
def text_to_speech(text):
myobj = gTTS(text=text, lang='en', slow=False)
myobj.save("test.wav")
return 'test.wav'
def transcribe(audio):
text = p(audio)["text"]
return text
def sentiment(text):
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"calssify the text into below sentiment category\n\ntext : {text}\"\n\n['positive','negative','neutral']\n",
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0)
return response.choices[0].text.strip()
messages = [
{"role": "system",
"content": "you name is Rebecca and you are a Pepsico call center assistant and your job is to take the order from the customer and also analysis the sentiment of the customer"}
]
def chatbot(input1,input2):
if input1:
input =input1
input = transcribe(input)
messages.append({"role": "user", "content": input})
if sentiment(input) == 'negative':
reply = "Sorry for any inconvinience. We are tranfering your call."
else:
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0.2,
max_tokens=320,
top_p=1,
frequency_penalty=0,
presence_penalty=0)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
elif input2:
input =input2
messages.append({"role": "user", "content": input})
if sentiment(input) == 'negative':
reply = "Sorry for any inconvinience. We are tranfering your call."
else:
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0.2,
max_tokens=320,
top_p=1,
frequency_penalty=0,
presence_penalty=0)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return text_to_speech(reply)
##inputs = gr.inputs.Textbox(lines=7, label="Chat with PepsiCo AI assitant")
#inputs= gr.Audio(source="microphone", type="filepath")
inputs= [ gr.Audio(source="microphone", type="filepath"),
gr.inputs.Textbox(lines=7)]
#outputs = gr.outputs.Textbox(label="Reply")
gr.Interface(fn= chatbot,
inputs= inputs,
outputs= 'audio',
title="PepsiCo-chatbot",
description="Give your order",
theme="compact").launch()
# gr.Interface(
# fn=transcribe,
# inputs= inputs,
# outputs="text"
# ).launch()