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+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import base64
|
4 |
+
import os
|
5 |
+
import llama_index
|
6 |
+
from audio_recorder_streamlit import audio_recorder
|
7 |
+
from openai import OpenAI
|
8 |
+
from llama_index import VectorStoreIndex, SimpleDirectoryReader
|
9 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
10 |
+
API_KEY = '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'
|
11 |
+
def RAG(text):
|
12 |
+
documents = SimpleDirectoryReader("db3").load_data()
|
13 |
+
index = VectorStoreIndex.from_documents(documents)
|
14 |
+
query_engine = index.as_query_engine()
|
15 |
+
response = query_engine.query(text)
|
16 |
+
|
17 |
+
# Extract the text from the response
|
18 |
+
response_text = response.response if hasattr(response, 'response') else str(response)
|
19 |
+
|
20 |
+
return response_text
|
21 |
+
def linkRAGhindi(text):
|
22 |
+
new_prompt="निम्नलिखित प्रश्न के लिए सबसे उपयुक्त वेबसाइट लिंक दें"+text
|
23 |
+
documents = SimpleDirectoryReader("db1").load_data()
|
24 |
+
index = VectorStoreIndex.from_documents(documents)
|
25 |
+
query_engine = index.as_query_engine()
|
26 |
+
response = query_engine.query(new_prompt)
|
27 |
+
|
28 |
+
# Extract the text from the response
|
29 |
+
response_text = response.response if hasattr(response, 'response') else str(response)
|
30 |
+
|
31 |
+
return response_text
|
32 |
+
def rechindi(text):
|
33 |
+
new_prompt="निम्नलिखित प्रश्न के लिए सबसे उपयुक्त वेबसाइट लिंक दें"+text
|
34 |
+
documents = SimpleDirectoryReader("db2").load_data()
|
35 |
+
index = VectorStoreIndex.from_documents(documents)
|
36 |
+
query_engine = index.as_query_engine()
|
37 |
+
response = query_engine.query(new_prompt)
|
38 |
+
|
39 |
+
# Extract the text from the response
|
40 |
+
response_text = response.response if hasattr(response, 'response') else str(response)
|
41 |
+
return response_text
|
42 |
+
def linkRAGenglish(text):
|
43 |
+
new_prompt="Give the most appropiate website link for the following question "+text
|
44 |
+
documents = SimpleDirectoryReader("db1").load_data()
|
45 |
+
index = VectorStoreIndex.from_documents(documents)
|
46 |
+
query_engine = index.as_query_engine()
|
47 |
+
response = query_engine.query(new_prompt)
|
48 |
+
# Extract the text from the response
|
49 |
+
response_text = response.response if hasattr(response, 'response') else str(response)
|
50 |
+
return response_text
|
51 |
+
def recenglish(text):
|
52 |
+
new_prompt="Give the most intresting other website link for the following question "+text
|
53 |
+
documents = SimpleDirectoryReader("db2").load_data()
|
54 |
+
index = VectorStoreIndex.from_documents(documents)
|
55 |
+
query_engine = index.as_query_engine()
|
56 |
+
response = query_engine.query(new_prompt)
|
57 |
+
# Extract the text from the response
|
58 |
+
response_text = response.response if hasattr(response, 'response') else str(response)
|
59 |
+
return response_text
|
60 |
+
def transcribe_text_to_voice_english(audio_location):
|
61 |
+
client = OpenAI(api_key=API_KEY)
|
62 |
+
audio_file = open(audio_location, "rb")
|
63 |
+
transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
|
64 |
+
return transcript.text
|
65 |
+
|
66 |
+
def transcribe_text_to_voice_hindi(audio_location):
|
67 |
+
url = "https://api.runpod.ai/v2/faster-whisper/runsync"
|
68 |
+
|
69 |
+
with open(audio_location, "rb") as audio_file:
|
70 |
+
audio_base64 = base64.b64encode(audio_file.read()).decode('utf-8')
|
71 |
+
|
72 |
+
payload = {
|
73 |
+
"input": {
|
74 |
+
"audio_base64": audio_base64,
|
75 |
+
"model": "small",
|
76 |
+
"transcription": "plain_text",
|
77 |
+
"translate": True,
|
78 |
+
"language": "hi",
|
79 |
+
"temperature": 0,
|
80 |
+
"best_of": 5,
|
81 |
+
"beam_size": 5,
|
82 |
+
"patience": 1,
|
83 |
+
"suppress_tokens": "-1",
|
84 |
+
"condition_on_previous_text": False,
|
85 |
+
"temperature_increment_on_fallback": 0.2,
|
86 |
+
"compression_ratio_threshold": 2.4,
|
87 |
+
"logprob_threshold": -1,
|
88 |
+
"no_speech_threshold": 0.6,
|
89 |
+
"word_timestamps": False
|
90 |
+
},
|
91 |
+
"enable_vad": False
|
92 |
+
}
|
93 |
+
|
94 |
+
headers = {
|
95 |
+
"accept": "application/json",
|
96 |
+
"content-type": "application/json",
|
97 |
+
"authorization": "X01PG949AHTOVRYHLQZKSRIWN82UHBUU5JYLNAHM"
|
98 |
+
}
|
99 |
+
|
100 |
+
response = requests.post(url, json=payload, headers=headers)
|
101 |
+
response_json = response.json()
|
102 |
+
transcription = response_json["output"]["transcription"]
|
103 |
+
translation = response_json["output"]["translation"].strip().split('\n')[-1].strip()
|
104 |
+
return transcription, translation
|
105 |
+
|
106 |
+
|
107 |
+
def recommendation(text):
|
108 |
+
client = OpenAI(api_key=API_KEY)
|
109 |
+
messages = [{"role": "user", "content": text}]
|
110 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo-1106", messages=messages)
|
111 |
+
return response.choices[0].message.content
|
112 |
+
def text_to_speech_ai(speech_file_path, api_response):
|
113 |
+
client = OpenAI(api_key=API_KEY)
|
114 |
+
response = client.audio.speech.create(model="tts-1",voice="nova",input=api_response)
|
115 |
+
response.stream_to_file(speech_file_path)
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
st.title("🚀 SHRESHTH 💬 Bhuvan Assistant")
|
122 |
+
|
123 |
+
# Radio wheel for language selection
|
124 |
+
language = st.radio("Language/भाषा",["English", "हिंदी"])
|
125 |
+
# Displaying description based on selected language
|
126 |
+
if language == "English":
|
127 |
+
mode = st.radio("Select Mode Of Input", ["Voice","Text"])
|
128 |
+
st.write("Smart - Helpful - Robust - Effortless - System for Text-to-speech and Human-like Assistance")
|
129 |
+
if mode == "Voice" or mode == "आवाज":
|
130 |
+
st.write("Click on the voice recorder and let me know how I can help you today with your Queries Regarding Bhuvan!")
|
131 |
+
audio_bytes = audio_recorder(
|
132 |
+
text="",
|
133 |
+
recording_color="#e8b62c",
|
134 |
+
neutral_color="#6aa36f",
|
135 |
+
icon_name="microphone",
|
136 |
+
icon_size="2x",
|
137 |
+
)
|
138 |
+
|
139 |
+
if audio_bytes:
|
140 |
+
# Save the Recorded File
|
141 |
+
audio_location = "audio_file.wav"
|
142 |
+
with open(audio_location, "wb") as f:
|
143 |
+
f.write(audio_bytes)
|
144 |
+
|
145 |
+
if language == "English":
|
146 |
+
text=transcribe_text_to_voice_english(audio_location)
|
147 |
+
st.write(text)
|
148 |
+
else:
|
149 |
+
text,trans=transcribe_text_to_voice_hindi(audio_location)
|
150 |
+
st.write(text)
|
151 |
+
|
152 |
+
|
153 |
+
link_response = linkRAGenglish(text)
|
154 |
+
st.write("SHRESHTH:", link_response)
|
155 |
+
api_response = RAG(text)
|
156 |
+
st.write("SHRESHTH:", api_response)
|
157 |
+
speech_file_path = 'audio_response.mp3'
|
158 |
+
text_to_speech_ai(speech_file_path, api_response)
|
159 |
+
st.audio(speech_file_path)
|
160 |
+
recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
|
161 |
+
recc=linkRAGenglish(recctext)
|
162 |
+
st.write("SHRESHTH:", recc)
|
163 |
+
else:
|
164 |
+
# Text input option
|
165 |
+
text_input = st.text_area("Enter your text here and press Enter", "")
|
166 |
+
if st.button("Submit"):
|
167 |
+
# Process the entered text
|
168 |
+
link_response = linkRAGenglish(text_input)
|
169 |
+
st.write("SHRESHTH:", link_response)
|
170 |
+
api_response = RAG(text_input)
|
171 |
+
st.write("SHRESHTH:", api_response)
|
172 |
+
# Read out the text response using tts
|
173 |
+
speech_file_path = 'audio_response.mp3'
|
174 |
+
text_to_speech_ai(speech_file_path, api_response)
|
175 |
+
st.audio(speech_file_path)
|
176 |
+
recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
|
177 |
+
recc=linkRAGenglish(recctext)
|
178 |
+
st.write("SHRESHTH:", recc)
|
179 |
+
else:
|
180 |
+
mode = st.radio("इनपुट मोड का चयन करें", ["आवाज", "टेक्स्ट"])
|
181 |
+
st.write("स्मार्ट - सहायक - मजबूत - प्रयासहीन - पाठ-से-बोल के लिए एक सिस्टम और मानव जैसी सहायता")
|
182 |
+
|
183 |
+
if mode == "Voice" or mode == "आवाज" or mode == "ভয়েস":
|
184 |
+
st.write("आवाज रेकॉर्डर पर क्लिक करें और मुझसे यह बताएं कि आज आपकी भुवन से संबंधित सवालों में मैं आपकी कैसे मदद कर सकता हूँ!")
|
185 |
+
audio_bytes = audio_recorder(
|
186 |
+
text="",
|
187 |
+
recording_color="#e8b62c",
|
188 |
+
neutral_color="#6aa36f",
|
189 |
+
icon_name="microphone",
|
190 |
+
icon_size="2x",
|
191 |
+
)
|
192 |
+
|
193 |
+
if audio_bytes:
|
194 |
+
# Save the Recorded File
|
195 |
+
audio_location = "audio_file.wav"
|
196 |
+
with open(audio_location, "wb") as f:
|
197 |
+
f.write(audio_bytes)
|
198 |
+
|
199 |
+
if language == "English":
|
200 |
+
text=transcribe_text_to_voice_english(audio_location)
|
201 |
+
st.write(text)
|
202 |
+
else:
|
203 |
+
text,trans=transcribe_text_to_voice_hindi(audio_location)
|
204 |
+
st.write(text)
|
205 |
+
|
206 |
+
link_response = linkRAGhindi(text)
|
207 |
+
st.write("श्रेष्ठ:", link_response)
|
208 |
+
api_response = RAG(text)
|
209 |
+
st.write("श्रेष्ठ:", api_response)
|
210 |
+
# Read out the text response using tts
|
211 |
+
speech_file_path = 'audio_response.mp3'
|
212 |
+
text_to_speech_ai(speech_file_path, api_response)
|
213 |
+
st.audio(speech_file_path)
|
214 |
+
recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
|
215 |
+
recc=rechindi(recctext)
|
216 |
+
st.write("श्रेष्ठ:", recc)
|
217 |
+
|
218 |
+
else:
|
219 |
+
# Text input option
|
220 |
+
text_input = st.text_area("आप यहाँ अपना टेक्स्ट दर्ज करें और एंटर दबाएं", "")
|
221 |
+
if st.button("एंटर"):
|
222 |
+
# Process the entered text
|
223 |
+
link_response = linkRAGhindi(text_input)
|
224 |
+
st.write("श्रेष्ठ:", link_response)
|
225 |
+
api_response = RAG(text_input)
|
226 |
+
st.write("श्रेष्ठ:", api_response)
|
227 |
+
|
228 |
+
# Read out the text response using tts
|
229 |
+
speech_file_path = 'audio_response.mp3'
|
230 |
+
text_to_speech_ai(speech_file_path, api_response)
|
231 |
+
st.audio(speech_file_path)
|
232 |
+
recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
|
233 |
+
recc=rechindi(recctext)
|
234 |
+
st.write("श्रेष्ठ:", recc)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
llama_index
|
2 |
+
audio_recorder_streamlit
|
3 |
+
openai
|