KrishiGPT / app.py
Soma Dhavala
Krishi GPT with ASR
03d4021
raw
history blame
2.6 kB
import gradio as gr
import openai
import os
api_key = os.getenv('OPEN_API_KEY')
openai.api_key = api_key
global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in English. I can answer your questions about farming in India. Ask me anything!"}]
from pydub import AudioSegment
def get_asr_output(audio_path,lang):
audio = AudioSegment.from_file(audio_path)
audio.export("temp.mp3", format="mp3")
file = open("temp.mp3","rb")
transcription = openai.Audio.transcribe("whisper-1", file, language=lang)
return transcription.text
def add_text(history, audio_path,lang):
global global_history
text = get_asr_output(audio_path,lang_dict[lang])
history = history + [(text, None)]
global_history = global_history+[{"role": "user", "content": text}]
print(global_history)
return history, ""
def get_chatgpt_response(history):
output = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=history)
history.append({"role": "assistant", "content": output.choices[0].message.content})
return output.choices[0].message.content, history
def bot(history):
global global_history
response, global_history = get_chatgpt_response(global_history)
history[-1][1] = response
return history
def clear_history(lang = "English"):
global global_history
global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in {}. I can answer your questions about farming in India. Ask me anything!".format(lang)}]
return None
lang_dict = {
"English": "en",
"Hindi": "hi",
"Bengali": "bn",
"Gujarati": "gu",
"Kannada": "kn",
"Marathi": "mr",
"Tamil": "ta",
"Telugu": "te"
}
with gr.Blocks(title="Krishi GPT Demo") as demo:
lang = gr.Radio(list(lang_dict.keys()), label="Select a Language")
with gr.Row():
with gr.Column():
user_audio = gr.Audio(source="microphone",type="filepath",label = "Speak your query")
txt = gr.Textbox(
show_label=True,
placeholder="Enter text and press enter, or Record your Audio",
visible=False,
).style(container=False)
submit = gr.Button("Submit")
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=500)
clear = gr.Button("Clear")
submit.click(add_text, [chatbot, user_audio,lang], [chatbot, txt]).then(bot, chatbot, chatbot)
clear.click(clear_history, [lang], chatbot, queue=False)
lang.change(clear_history, [lang], chatbot, queue=False)
demo.launch(share=False)