Spaces:
Running
Running
import streamlit as st | |
from sentence_transformers import SentenceTransformer | |
import weaviate | |
import base64 | |
import requests | |
bg_color = "#343434" # Set your desired background color | |
st.markdown( | |
f""" | |
<style> | |
body {{ | |
background-color: {bg_color}; | |
}} | |
</style> | |
""", | |
unsafe_allow_html=True, | |
) | |
st.sidebar.header("Try your query on the following: ") | |
topics = ["Character", "Compassion", "Culture", "Devotion", "Dharma", "Discipline", "Education", "Festivals", | |
"Gayatri Devi", "God", "Gratitude", "Guru", "Human Values", "India", "Love", "Meditation", "Mind", | |
"Money", "Namasmarana", "Parents", "Pleasure and Pain", "Practice", "Sacrifice", "Sadhana", "Senses", | |
"Service", "Time"] | |
for topic in topics: | |
st.sidebar.write(topic) | |
# Set up the layout using columns | |
col1, col2 = st.columns([2, 1]) | |
# Left column: Title, Search Bar, and Search Button | |
with col1: | |
st.header('Harmony Hub: Connecting with Spiritual Messages for Inner Peace') | |
query_text = st.text_input('Enter your query:') | |
search_button = st.button('Search') | |
# Display the search results | |
if search_button: | |
try: | |
# Encode the query text | |
embedder = SentenceTransformer('all-MiniLM-L6-v2') | |
query_embeddings = embedder.encode([query_text], convert_to_tensor=True) | |
query_vector = query_embeddings.flatten().tolist() | |
# Perform Weaviate search | |
client = weaviate.connect_to_embedded( | |
persistence_data_path='Transcript Data' | |
) | |
info = client.collections.get("Audio") | |
query_vector = query_vector | |
response = info.query.near_vector( | |
near_vector=query_vector, | |
limit=3, | |
return_properties=["audio", "transcript"] | |
) | |
result = {'audio': '', 'transcript': ''} | |
if response.objects: | |
obj = response.objects[0] | |
base64_audio = obj.properties.get("audio", "") | |
transcript = obj.properties.get("transcript", "") | |
if base64_audio: | |
decoded_audio = base64.b64decode(base64_audio.encode("utf-8")) | |
audio_data = base64.b64encode(decoded_audio).decode("utf-8") | |
result['audio'] = audio_data | |
result['transcript'] = transcript | |
# Display audio player and transcript | |
st.audio(base64.b64decode(result['audio']), format='audio/mp3') | |
st.write('Transcript:', result['transcript']) | |
except Exception as e: | |
st.write('Error:', str(e)) | |
# Right column: Image | |
with col2: | |
image_path = 'Swami1.jpeg' | |
st.image(image_path, caption='Conscience is our real power, strength, and awareness.', use_column_width=True) |