File size: 10,545 Bytes
ccb0bb0
 
11c81b2
 
ccb0bb0
 
 
 
11c81b2
 
ccb0bb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import streamlit as st
import requests
import base64   
import os 
import llama_index
from audio_recorder_streamlit import audio_recorder
from openai import OpenAI
from llama_index import VectorStoreIndex, SimpleDirectoryReader
os.environ['OPENAI_API_KEY'] = os.getenv("apikey")
API_KEY = os.getenv("apikey")
def RAG(text):
    documents = SimpleDirectoryReader("db3").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query(text)
    
    # Extract the text from the response
    response_text = response.response if hasattr(response, 'response') else str(response)

    return response_text
def linkRAGhindi(text):
    new_prompt="निम्नलिखित प्रश्न के लिए सबसे उपयुक्त वेबसाइट लिंक दें"+text
    documents = SimpleDirectoryReader("db1").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query(new_prompt)
    
    # Extract the text from the response
    response_text = response.response if hasattr(response, 'response') else str(response)

    return response_text
def rechindi(text):
    new_prompt="निम्नलिखित प्रश्न के लिए सबसे उपयुक्त वेबसाइट लिंक दें"+text
    documents = SimpleDirectoryReader("db2").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query(new_prompt)
    
    # Extract the text from the response
    response_text = response.response if hasattr(response, 'response') else str(response)
    return response_text
def linkRAGenglish(text):
    new_prompt="Give the most appropiate website link for the following question "+text
    documents = SimpleDirectoryReader("db1").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query(new_prompt)
    # Extract the text from the response
    response_text = response.response if hasattr(response, 'response') else str(response)
    return response_text
def recenglish(text):
    new_prompt="Give the most intresting other website link for the following question "+text
    documents = SimpleDirectoryReader("db2").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query(new_prompt)
    # Extract the text from the response
    response_text = response.response if hasattr(response, 'response') else str(response)
    return response_text
def transcribe_text_to_voice_english(audio_location):
    client = OpenAI(api_key=API_KEY)
    audio_file = open(audio_location, "rb")
    transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
    return transcript.text

def transcribe_text_to_voice_hindi(audio_location):
    url = "https://api.runpod.ai/v2/faster-whisper/runsync"
    
    with open(audio_location, "rb") as audio_file:
        audio_base64 = base64.b64encode(audio_file.read()).decode('utf-8')

    payload = {
        "input": {
            "audio_base64": audio_base64,
            "model": "small",
            "transcription": "plain_text",
            "translate": True,
            "language": "hi",
            "temperature": 0,
            "best_of": 5,
            "beam_size": 5,
            "patience": 1,
            "suppress_tokens": "-1",
            "condition_on_previous_text": False,
            "temperature_increment_on_fallback": 0.2,
            "compression_ratio_threshold": 2.4,
            "logprob_threshold": -1,
            "no_speech_threshold": 0.6,
            "word_timestamps": False
        },
        "enable_vad": False
    }

    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "authorization": "X01PG949AHTOVRYHLQZKSRIWN82UHBUU5JYLNAHM"
    }

    response = requests.post(url, json=payload, headers=headers)
    response_json = response.json()
    transcription = response_json["output"]["transcription"]
    translation = response_json["output"]["translation"].strip().split('\n')[-1].strip()
    return transcription, translation


def recommendation(text):
    client = OpenAI(api_key=API_KEY)
    messages = [{"role": "user", "content": text}]
    response = client.chat.completions.create(model="gpt-3.5-turbo-1106", messages=messages)
    return response.choices[0].message.content
def text_to_speech_ai(speech_file_path, api_response):
    client = OpenAI(api_key=API_KEY)
    response = client.audio.speech.create(model="tts-1",voice="nova",input=api_response)
    response.stream_to_file(speech_file_path)





st.title("🚀 SHRESHTH 💬 Bhuvan Assistant")

# Radio wheel for language selection
language = st.radio("Language/भाषा",["English", "हिंदी"])
# Displaying description based on selected language
if language == "English":
    mode = st.radio("Select Mode Of Input", ["Voice","Text"])
    st.write("Smart - Helpful - Robust - Effortless - System for Text-to-speech and Human-like Assistance")
    if mode == "Voice" or mode == "आवाज":
        st.write("Click on the voice recorder and let me know how I can help you today with your Queries Regarding Bhuvan!")
        audio_bytes = audio_recorder(
            text="",
            recording_color="#e8b62c",
            neutral_color="#6aa36f",
            icon_name="microphone",
            icon_size="2x",
        )

        if audio_bytes:
            # Save the Recorded File
            audio_location = "audio_file.wav"
            with open(audio_location, "wb") as f:
                f.write(audio_bytes)
    
            if language == "English":
               text=transcribe_text_to_voice_english(audio_location)
               st.write(text)
            else:
               text,trans=transcribe_text_to_voice_hindi(audio_location)
               st.write(text)


            link_response = linkRAGenglish(text)
            st.write("SHRESHTH:", link_response)
            api_response = RAG(text)
            st.write("SHRESHTH:", api_response)
            speech_file_path = 'audio_response.mp3'
            text_to_speech_ai(speech_file_path, api_response)
            st.audio(speech_file_path)
            recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
            recc=linkRAGenglish(recctext)  
            st.write("SHRESHTH:", recc)
    else: 
        # Text input option
        text_input = st.text_area("Enter your text here and press Enter", "")
        if st.button("Submit"):
            # Process the entered text
            link_response = linkRAGenglish(text_input)
            st.write("SHRESHTH:", link_response)
            api_response = RAG(text_input)
            st.write("SHRESHTH:", api_response)
            # Read out the text response using tts
            speech_file_path = 'audio_response.mp3'
            text_to_speech_ai(speech_file_path, api_response)
            st.audio(speech_file_path)
            recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
            recc=linkRAGenglish(recctext)  
            st.write("SHRESHTH:", recc)
else:
    mode = st.radio("इनपुट मोड का चयन करें", ["आवाज", "टेक्स्ट"])
    st.write("स्मार्ट - सहायक - मजबूत - प्रयासहीन - पाठ-से-बोल के लिए एक सिस्टम और मानव जैसी सहायता")

    if mode == "Voice" or mode == "आवाज" or mode == "ভয়েস":
        st.write("आवाज रेकॉर्डर पर क्लिक करें और मुझसे यह बताएं कि आज आपकी भुवन से संबंधित सवालों में मैं आपकी कैसे मदद कर सकता हूँ!")
        audio_bytes = audio_recorder(
            text="",
            recording_color="#e8b62c",
            neutral_color="#6aa36f",
            icon_name="microphone",
            icon_size="2x",
        )

        if audio_bytes:
            # Save the Recorded File
            audio_location = "audio_file.wav"
            with open(audio_location, "wb") as f:
                f.write(audio_bytes)

            if language == "English":
               text=transcribe_text_to_voice_english(audio_location)
               st.write(text)
            else:
               text,trans=transcribe_text_to_voice_hindi(audio_location)
               st.write(text)
    
            link_response = linkRAGhindi(text)
            st.write("श्रेष्ठ:", link_response)
            api_response = RAG(text)
            st.write("श्रेष्ठ:", api_response)      
            # Read out the text response using tts
            speech_file_path = 'audio_response.mp3'
            text_to_speech_ai(speech_file_path, api_response)
            st.audio(speech_file_path)
            recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
            recc=rechindi(recctext)  
            st.write("श्रेष्ठ:", recc)
            
    else: 
        # Text input option
        text_input = st.text_area("आप यहाँ अपना टेक्स्ट दर्ज करें और एंटर दबाएं", "")
        if st.button("एंटर"):
            # Process the entered text
            link_response = linkRAGhindi(text_input)
            st.write("श्रेष्ठ:", link_response)
            api_response = RAG(text_input)
            st.write("श्रेष्ठ:", api_response)
    
            # Read out the text response using tts
            speech_file_path = 'audio_response.mp3'
            text_to_speech_ai(speech_file_path, api_response)
            st.audio(speech_file_path)
            recctext="recommend top three other websites that could interest the user depending on this link and answer : " + link_response + api_response
            recc=rechindi(recctext)  
            st.write("श्रेष्ठ:", recc)