CoolFace
Apppublic

MKhubaib/Library

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py69 linesDownload Raw Back to root
1import streamlit as st2import pandas as pd3from gtts import gTTS4import os5 6# Function to convert text to speech7def text_to_speech(text, lang='en'):8    tts = gTTS(text=text, lang=lang)9    tts.save("output.mp3")10    return "output.mp3"11 12# Function to process the uploaded file13def process_file(uploaded_file):14    # Read the uploaded Excel file15    df = pd.read_excel(uploaded_file)16    return df17 18# Function to detect language (simplified version: checks for Urdu characters)19def detect_language(text):20    # Check if Urdu characters exist in the text21    if any("\u0600" <= char <= "\u06FF" for char in text):  # Urdu Unicode range22        return 'ur'  # Urdu23    return 'en'  # Default to English24 25# Function to search for a book in the catalog based on text input26def search_book_by_text(df):27    # Get the search query from the user input28    query = st.text_input("Enter the book title or part of the title to search:")29 30    if query:31        # Search for the book in the dataframe32        result = df[df['TITLE'].str.contains(query, case=False, na=False)]33 34        if not result.empty:35            st.write("Found the following results:")36            st.write(result[['TITLE', 'AUTHOR', 'PUBLISHER', 'YEAR', 'PRICE']])37 38            # Convert the result to speech and return the audio39            result_text = result[['TITLE', 'AUTHOR', 'PUBLISHER', 'YEAR', 'PRICE']].to_string(index=False)40            41            # Detect language of the result text (Urdu or English)42            lang = detect_language(result_text)43            44            # Convert result to speech based on the detected language45            audio_file = text_to_speech(result_text, lang)46            st.audio(audio_file)47 48        else:49            st.write("No results found for your query.")50            return None51 52# Streamlit app UI53def main():54    st.title("Book Search by Text")55 56    uploaded_file = st.file_uploader("Upload your books catalog file", type=["xlsx", "xls"])57 58    if uploaded_file is not None:59        # Process the uploaded file60        df = process_file(uploaded_file)61        st.write("File uploaded successfully! Now you can search for books.")62        63        # Button to initiate text search64        search_book_by_text(df)65 66# Run the Streamlit app67if __name__ == "__main__":68    main()69