import glob import os from tokenizers import Tokenizer import gradio as gr # Load the tokenizer tokenizer = Tokenizer.from_file("hindi_bpe_tokenizer.json") # Define the compression ratio function def compression_ratio(text): encoded = tokenizer.encode(text) compressed_length = len(encoded.ids) original_length = len(text) compression = original_length / compressed_length decoded_text = tokenizer.decode(encoded.ids) encoded_tokens = encoded.tokens vocab_size = len(tokenizer.get_vocab()) return encoded_tokens, vocab_size, compression, decoded_text # Sample text sample_text1 = "कावेरी नदी, जिसे कावेरी भी कहा जाता है, दक्षिण भारत की प्रमुख नदियों में से एक है।" sample_text2 = "कावेरी नदी कर्नाटक और तमिलनाडु राज्यों से होकर बहती है।" sample_text3 = "कर्नाटक में पश्चिमी घाट से उत्पन्न होकर, कावेरी नदी लगभग 800 किलोमीटर की दूरी तय करने के बाद बंगाल की खाड़ी में मिलती है।" sample_text4 = "कावेरी नदी हिन्दुओं के लिए पवित्र मानी जाती है और इसे अक्सर 'दक्षिण की गंगा' कहा जाता है।" sample_text5 = "वृंदावन गार्डन, जो कर्नाटक में मैसूर शहर के पास स्थित है, भारत के सबसे सुंदर टेरेस्ड गार्डनों में से एक है।" sample_text6 = "यह गार्डन कृष्णराजसागर बांध के नीचे स्थित है, जो कावेरी नदी पर बना है।" sample_text7 = "वृंदावन गार्डन अपने सममित डिजाइन, विस्तृत फूलों की क्यारियों और संगीत फव्वारों के शो के लिए प्रसिद्ध है।" sample_text8 = "वृंदावन गार्डन का निर्माण 1927 में शुरू हुआ और 1932 में पूरा हुआ।" sample_text9 = "कावेरी नदी सिंचाई, पीने के पानी और जलविद्युत के लिए आवश्यक पानी प्रदान करती है।" sample_text10 = "वृंदावन गार्डन की हरी-भरी हरियाली और अच्छी तरह से बनाए गए परिदृश्य इसे एक लोकप्रिय पिकनिक स्थल बनाते हैं।" # Define the Gradio interface iface = gr.Interface( fn=compression_ratio, inputs=gr.Textbox(lines=5, placeholder="Enter text here..."), examples=[[sample_text1], [sample_text2], [sample_text3] ,[sample_text4], [sample_text5], [sample_text6] ,[sample_text7], [sample_text8], [sample_text9],[sample_text10]], outputs=[ gr.JSON(label="Encoded Text"), gr.Textbox(label="Vocabulary Size"), gr.Textbox(label="Compression Ratio"), gr.Textbox(label="Decoded Text") ] ) # Launch the interface if __name__ == "__main__": iface.launch()