File size: 6,187 Bytes
5c91778
 
 
 
85c5439
5c91778
85c5439
 
5c91778
85c5439
5c91778
 
85c5439
5c91778
85c5439
 
 
 
 
 
 
5c91778
 
 
 
 
 
 
85c5439
 
5c91778
 
85c5439
 
5c91778
85c5439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c91778
85c5439
 
 
 
 
 
 
 
 
 
 
aa06471
85c5439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fb02e0
85c5439
 
 
 
 
 
 
 
 
 
 
3fb02e0
85c5439
71a80e6
85c5439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71a80e6
85c5439
5c91778
85c5439
 
6efce3f
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
import os
import streamlit as st
from dotenv import load_dotenv
from pymongo import MongoClient
import pandas as pd
from bson import ObjectId
import cloudinary
import cloudinary.uploader

# Load environment variables from .env file
load_dotenv()

# Get MongoDB URI from .env file
mongodb_uri = os.getenv('MONGODB_URI')

# Configure Cloudinary
cloudinary.config(
    cloud_name = os.getenv('CLOUDINARY_CLOUD_NAME'),
    api_key = os.getenv('CLOUDINARY_API_KEY'),
    api_secret = os.getenv('CLOUDINARY_API_SECRET')
)

# Connect to MongoDB
@st.cache_resource
def init_connection():
    return MongoClient(mongodb_uri)

client = init_connection()

# Access the 'mitra' database
db = client['mitra']

# Access the 'base-voices' collection
collection = db['base-voices']

# Streamlit app
st.title('Base Voices Data Management')

# Sidebar for actions
st.sidebar.header('Actions')
action = st.sidebar.radio('Choose an action:', ['View Data', 'Add Category', 'Remove Category', 'Add Voice', 'Remove Voice'])

if action == 'View Data':
    # Retrieve all documents from the collection
    @st.cache_data
    def get_base_voices():
        base_voices = list(collection.find({}, {'_id': 0}))  # Exclude the '_id' field
        return base_voices

    data = get_base_voices()

    # Display the data
    if data:
        # Normalize the data for better display
        normalized_data = []
        for category in data:
            for voice in category['voices']:
                normalized_data.append({
                    'Category': category['category'],
                    'Voice Name': voice['name'],
                    'Is Free': 'Yes' if voice['is_free'] else 'No',
                    'File URL': voice['file_url']
                })
        
        df = pd.DataFrame(normalized_data)
        
        # Display the table
        st.subheader("Voice Data Table")
        st.dataframe(df, use_container_width=True)

        # Display audio players for each voice
        st.subheader("Audio Samples")
        for category in data:
            st.write(f"**{category['category']}**")
            for voice in category['voices']:
                col1, col2 = st.columns([3, 1])
                with col1:
                    st.write(f"{voice['name']} ({'Free' if voice['is_free'] else 'Paid'})")
                    st.audio(voice['file_url'])
                with col2:
                    st.markdown(f"[Download]({voice['file_url']})")
            st.write("---")

        # Optional: Add a download button
        csv = df.to_csv(index=False)
        st.download_button(
            label="Download data as CSV",
            data=csv,
            file_name="base_voices.csv",
            mime="text/csv",
        )
    else:
        st.write("No data found in the 'base-voices' collection.")

elif action == 'Add Category':
    st.header('Add New Category')
    new_category = st.text_input('Enter new category name:')
    if st.button('Add Category'):
        if new_category:
            new_doc = {'category': new_category, 'voices': []}
            result = collection.insert_one(new_doc)
            st.success(f'Category "{new_category}" added successfully!')
        else:
            st.error('Please enter a category name.')

elif action == 'Remove Category':
    st.header('Remove Category')
    categories = [doc['category'] for doc in collection.find({}, {'category': 1})]
    category_to_remove = st.selectbox('Select category to remove:', categories)
    if st.button('Remove Category'):
        result = collection.delete_one({'category': category_to_remove})
        if result.deleted_count > 0:
            st.success(f'Category "{category_to_remove}" removed successfully!')
        else:
            st.error('Failed to remove category. Please try again.')

elif action == 'Add Voice':
    st.header('Add Voice to Category')
    categories = [doc['category'] for doc in collection.find({}, {'category': 1})]
    selected_category = st.selectbox('Select category:', categories)
    voice_name = st.text_input('Enter voice name:')
    voice_file = st.file_uploader("Upload voice file", type=['mp3', 'wav'])
    is_free = st.checkbox('Is this voice free?')
    
    if st.button('Add Voice'):
        if voice_name and voice_file:
            # Upload file to Cloudinary
            upload_result = cloudinary.uploader.upload(voice_file, resource_type="auto")
            voice_url = upload_result['secure_url']
            
            new_voice = {
                'name': voice_name, 
                'file_url': voice_url,
                'is_free': is_free
            }
            result = collection.update_one(
                {'category': selected_category},
                {'$push': {'voices': new_voice}}
            )
            if result.modified_count > 0:
                st.success(f'Voice "{voice_name}" added to category "{selected_category}" successfully!')
            else:
                st.error('Failed to add voice. Please try again.')
        else:
            st.error('Please enter a voice name and upload a file.')

elif action == 'Remove Voice':
    st.header('Remove Voice from Category')
    categories = [doc['category'] for doc in collection.find({}, {'category': 1})]
    selected_category = st.selectbox('Select category:', categories)
    category_doc = collection.find_one({'category': selected_category})
    if category_doc and 'voices' in category_doc:
        voice_names = [voice['name'] for voice in category_doc['voices']]
        voice_to_remove = st.selectbox('Select voice to remove:', voice_names)
        if st.button('Remove Voice'):
            result = collection.update_one(
                {'category': selected_category},
                {'$pull': {'voices': {'name': voice_to_remove}}}
            )
            if result.modified_count > 0:
                st.success(f'Voice "{voice_to_remove}" removed from category "{selected_category}" successfully!')
            else:
                st.error('Failed to remove voice. Please try again.')
    else:
        st.warning(f'No voices found in category "{selected_category}".')

# Refresh data after actions
if st.button('Refresh Data'):
    st.rerun()