-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreamlit_app_vc.py
More file actions
72 lines (62 loc) · 2.73 KB
/
Copy pathstreamlit_app_vc.py
File metadata and controls
72 lines (62 loc) · 2.73 KB
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
import streamlit as st
import azure.cognitiveservices.speech as speechsdk
from googletrans import Translator
from indictrans import Transliterator
import openai
from gtts import gTTS
from io import BytesIO
openai.api_key = st.secrets["openai_api_key"]
azure_speech_key = st.secrets["azure_key"]
azure_speech_region = st.secrets["azure_region"]
def chatbot_response(prompt):
completions = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.8,
)
message = completions.choices[0].text
return message
def text_to_speech(text):
audio_bytes = BytesIO()
tts = gTTS(text=text, lang="hi", slow=False)
tts.write_to_fp(audio_bytes)
audio_bytes.seek(0)
return audio_bytes.read()
def transcribe_audio(audio_file):
speech_config = speechsdk.SpeechConfig(subscription=azure_speech_key, region=azure_speech_region)
audio_config = speechsdk.AudioConfig(filename=audio_file)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
result = speech_recognizer.recognize_once_async().get()
return result.text if result.reason == speechsdk.ResultReason.RecognizedSpeech else ""
def run_chatbot():
default_prompt = "Answer in details in Hinglish language. Aap ek Microentreprenuer ke Mentor hai. Microentreprenuer ka sawaal:"
user_input = st.text_input("Enter your query in Hinglish:")
user_audio = st.file_uploader("Or upload an audio file:", type=["wav", "mp3"])
if user_input:
try:
hindi_text = Transliterator(source='eng', target='hin').transform(user_input)
english_text = Translator().translate(hindi_text, dest='en').text
prompt = default_prompt + "\nYou: " + english_text
response = chatbot_response(prompt)
st.success(f"Chatbot: {response}")
st.audio(text_to_speech(response), format="audio/wav")
except Exception as e:
st.error("Error: " + str(e))
if user_audio:
try:
hindi_text = Transliterator(source='eng', target='hin').transform(user_input)
english_text = Translator().translate(hindi_text, dest='en').text
audio_transcript = transcribe_audio(english_text)
prompt = default_prompt + "\nYou: " + audio_transcript
response = chatbot_response(prompt)
st.success(f"Chatbot: {response}")
st.audio(text_to_speech(response), format="audio/wav")
except Exception as e:
st.error("Error: " + str(e))
if __name__ == "__main__":
st.set_page_config(page_title="Hinglish Chatbot")
st.title("Hinglish Chatbot")
run_chatbot()