Spaces:
Running
Running
Update genAI.py
Browse filesAdded the text-to-speech model feature for every response generated by the model.
genAI.py
CHANGED
@@ -149,6 +149,21 @@ def load_and_preprocess(uploaded_file):
|
|
149 |
for item in data if item["text"].strip()]
|
150 |
return data, passages
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
def load_model(model_name="BAAI/bge-m3"):
|
154 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
@@ -250,6 +265,7 @@ def main():
|
|
250 |
if st.session_state.faiss_index:
|
251 |
st.header("Ask a Question")
|
252 |
user_query = st.text_input("Type your question here:")
|
|
|
253 |
if user_query:
|
254 |
response = handle_userinput(user_query)
|
255 |
|
@@ -259,6 +275,12 @@ def main():
|
|
259 |
|
260 |
st.session_state.chat_history_ui.append({"role": "user", "content": user_query})
|
261 |
st.session_state.chat_history_ui.append({"role": "bot", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
|
264 |
if "chat_history_ui" in st.session_state:
|
@@ -267,6 +289,16 @@ def main():
|
|
267 |
message(chat["content"], is_user=True,key=f"user_{i}")
|
268 |
else:
|
269 |
message(chat["content"], is_user=False,key=f"bot_{i}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
|
272 |
if __name__ == "__main__":
|
|
|
149 |
for item in data if item["text"].strip()]
|
150 |
return data, passages
|
151 |
|
152 |
+
def generate_text_to_speech(text):
|
153 |
+
API_KEY = 'sk_926210280a2b0e013545e33350ae35c73a080b0f24f9542e'
|
154 |
+
VOICE_ID = 'TX3LPaxmHKxFdv7VOQHJ'
|
155 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
|
156 |
+
headers = {
|
157 |
+
"Content-Type": "application/json",
|
158 |
+
"xi-api-key": API_KEY
|
159 |
+
}
|
160 |
+
payload = {
|
161 |
+
"text": text,
|
162 |
+
"model_id": "eleven_monolingual_v1",
|
163 |
+
"voice_settings": {"stability": 0.5, "similarity_boost": 0.75}
|
164 |
+
}
|
165 |
+
response = requests.post(url, headers=headers, json=payload)
|
166 |
+
return response.content if response.status_code == 200 else None
|
167 |
|
168 |
def load_model(model_name="BAAI/bge-m3"):
|
169 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
265 |
if st.session_state.faiss_index:
|
266 |
st.header("Ask a Question")
|
267 |
user_query = st.text_input("Type your question here:")
|
268 |
+
play_audio = st.checkbox("π Generate audio for the response")
|
269 |
if user_query:
|
270 |
response = handle_userinput(user_query)
|
271 |
|
|
|
275 |
|
276 |
st.session_state.chat_history_ui.append({"role": "user", "content": user_query})
|
277 |
st.session_state.chat_history_ui.append({"role": "bot", "content": response})
|
278 |
+
if play_audio:
|
279 |
+
if "audio_cache" not in st.session_state:
|
280 |
+
st.session_state.audio_cache = {}
|
281 |
+
if response not in st.session_state.audio_cache:
|
282 |
+
audio_bytes = generate_text_to_speech(response)
|
283 |
+
st.session_state.audio_cache[response] = audio_bytes
|
284 |
|
285 |
|
286 |
if "chat_history_ui" in st.session_state:
|
|
|
289 |
message(chat["content"], is_user=True,key=f"user_{i}")
|
290 |
else:
|
291 |
message(chat["content"], is_user=False,key=f"bot_{i}")
|
292 |
+
audio_bytes = st.session_state.get("audio_cache", {}).get(chat["content"])
|
293 |
+
if audio_bytes:
|
294 |
+
st.audio(audio_bytes, format="audio/mpeg",start_time=0)
|
295 |
+
else:
|
296 |
+
if st.button(f"π Generate & Play Audio for Response {i}"):
|
297 |
+
audio = generate_text_to_speech(chat["content"])
|
298 |
+
if "audio_cache" not in st.session_state:
|
299 |
+
st.session_state.audio_cache = {}
|
300 |
+
st.session_state.audio_cache[chat["content"]] = audio
|
301 |
+
st.audio(audio, format="audio/mpeg", start_time=0)
|
302 |
|
303 |
|
304 |
if __name__ == "__main__":
|