SamitF commited on
Commit
e47737d
ยท
verified ยท
1 Parent(s): 9e8dd52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +223 -3
app.py CHANGED
@@ -812,6 +812,226 @@ SAMPLES = {
812
  "Simple English": "Hello World! Feel free to paste any text here to test the NLP Text Explorer system. It works immediately without external libraries.",
813
  "Multilingual & Emoji": "Multilingual test: Bonjour, ใ“ใ‚“ใซใกใฏ, ์•ˆ๋…•ํ•˜์„ธ์š”! Here is an emoji breakdown to inspect UTF-8 bytes: ๐Ÿ˜Š ๐Ÿš€ ๐Ÿ•. What is their representation in binary memory space?",
814
  "HTML document sample": '<!DOCTYPE html>\n<html>\n<head>\n <title>Sample Sandbox</title>\n</head>\n<body>\n <div class="content">\n <h1>Welcome to Text Explorer!</h1>\n <p>This is standard markup designed to verify our pure HTML-Parser extraction utilities.</p>\n <a href="https://example.com">Visit our project</a>\n </div>\n</body>\n</html>',
815
- "Tabular CSV format": "username,email,role,joined_date\nsmith_john,john.smith@gmail.com,Administrator,2024-03-24\nelizabeth_k,k.elizabeth@yahoo.com,Contributor,2025-05-15\ntech_support,support@domain.org,User,2026-01-10",
816
- "Markdown document": "# Markdown Document Guide\n\nWelcome to this structural text parsing review. Here are some key attributes:\n\n- Support lists\n- Supports link items: [Hugging Face Space](https://huggingface.co/spaces)\n\n## Sub-headers\n",
817
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812
  "Simple English": "Hello World! Feel free to paste any text here to test the NLP Text Explorer system. It works immediately without external libraries.",
813
  "Multilingual & Emoji": "Multilingual test: Bonjour, ใ“ใ‚“ใซใกใฏ, ์•ˆ๋…•ํ•˜์„ธ์š”! Here is an emoji breakdown to inspect UTF-8 bytes: ๐Ÿ˜Š ๐Ÿš€ ๐Ÿ•. What is their representation in binary memory space?",
814
  "HTML document sample": '<!DOCTYPE html>\n<html>\n<head>\n <title>Sample Sandbox</title>\n</head>\n<body>\n <div class="content">\n <h1>Welcome to Text Explorer!</h1>\n <p>This is standard markup designed to verify our pure HTML-Parser extraction utilities.</p>\n <a href="https://example.com">Visit our project</a>\n </div>\n</body>\n</html>',
815
+ "Tabular CSV format": "username,email,role,joined_date\nsmith_john,john.smith@gmail.com,Administrator,2024-03-24\elizabeth_k,k.elizabeth@yahoo.com,Contributor,2025-05-15\ntech_support,support@domain.org,User,2026-01-10",
816
+ "Markdown document": (
817
+ "# Markdown Document Guide\n\n"
818
+ "Welcome to this structural text parsing review. Here are some key attributes:\n\n"
819
+ "- Support lists\n- Supports link items: [Hugging Face Space](https://huggingface.co/spaces)\n\n"
820
+ "## Sub-headers\n"
821
+ "```python\n"
822
+ "def test():\n"
823
+ " return 'Hello, Markdown analyzer!'\n"
824
+ "```"
825
+ )
826
+ }
827
+
828
+ # =====================================================================
829
+ # GRADIO BLOCKS APPLICATION VIEW
830
+ # =====================================================================
831
+
832
+ with gr.Blocks(css=CSS, title="Text Explorer - Educational Pre-NLP Inspector") as demo:
833
+
834
+ # Hero Title Banner
835
+ gr.HTML("""
836
+ <div class="header-hero">
837
+ <h1>๐Ÿ” Text Explorer</h1>
838
+ <p>Analyze and demystify raw character representation, encodings, byte streams, and structural text layouts before high-level NLP tokenization.</p>
839
+ </div>
840
+ """)
841
+
842
+ with gr.Row():
843
+ # Input column
844
+ with gr.Column(scale=4):
845
+ gr.Markdown("### ๐Ÿ“ฅ Document Source Input")
846
+ input_text = gr.Textbox(
847
+ label="Source Text Area",
848
+ placeholder="Paste your paragraphs, CSV structure, HTML content, or multilingual strings here...",
849
+ lines=10,
850
+ max_lines=25,
851
+ elem_id="input-textbox"
852
+ )
853
+
854
+ with gr.Row():
855
+ file_upload = gr.File(
856
+ label="Or Upload Text File (.txt, .csv, .json, .html, .md)",
857
+ file_types=[".txt", ".csv", ".json", ".html", ".md"],
858
+ type="filepath",
859
+ )
860
+
861
+ gr.Markdown("๐Ÿ’ก **Educational Presets**")
862
+ with gr.Row():
863
+ # Loop samples to create buttons
864
+ for name, content in SAMPLES.items():
865
+ gr.Button(name, size="sm").click(
866
+ fn=lambda val=content: val,
867
+ outputs=input_text
868
+ )
869
+
870
+ # Output / Analysis display tab matrix
871
+ with gr.Column(scale=6):
872
+ gr.Markdown("### โšก Real-Time Analytical Breakdown")
873
+
874
+ with gr.Tabs():
875
+ # Overview
876
+ with gr.TabItem("๐Ÿ“Š Overview"):
877
+ with gr.Accordion("๐Ÿ“š Educational Spotlight: The Lexical Units", open=True):
878
+ gr.HTML("""
879
+ <div class="educational-card">
880
+ <h4>How Computers Partition Text</h4>
881
+ <p>Before an NLP model can comprehend context, the document must be mathematically measured:</p>
882
+ <ul style="list-style-type: disc; margin-left: 1.25rem; margin-top: 0.5rem;">
883
+ <li><b>Bytes</b>: The actual digital footprint in physical memory (UTF-8 allocates 1 to 4 bytes per character).</li>
884
+ <li><b>Words & Sentences</b>: Fundamental logic nodes. Without standard tools like NLTK/spaCy, computers extract tokens using punctuation borders and boundary regex.</li>
885
+ </ul>
886
+ </div>
887
+ """)
888
+ overview_html = gr.HTML("<div class='text-center text-slate-400 py-6'>Analyze text to generate document insights.</div>")
889
+ overview_chart = gr.HTML()
890
+
891
+ # Character Encodings
892
+ with gr.TabItem("๐Ÿ”ข Encodings Table"):
893
+ with gr.Accordion("๐Ÿซ Educational Spotlight: Encodings Simplified", open=False):
894
+ gr.HTML("""
895
+ <div class="educational-card">
896
+ <h4>Encoding Architecture Explained</h4>
897
+ <p>Every character is assigned a unique tracking coordinate:</p>
898
+ <ul style="list-style-type: disc; margin-left: 1.25rem; margin-top: 0.5rem;">
899
+ <li><b>ASCII</b>: Traditional 7-bit system spanning decimal positions 0 through 127. Standard English keys only.</li>
900
+ <li><b>Unicode</b>: A universal catalog assigning identical codepoint indices (e.g., U+1F60A) for all global languages and emojis.</li>
901
+ <li><b>UTF-8</b>: An optimized encoding translating those codepoints into binary arrays, using smaller slices (1 byte) for English and wider slices for eastern languages or emojis.</li>
902
+ </ul>
903
+ </div>
904
+ """)
905
+ encoding_table = gr.DataFrame(
906
+ headers=["Char", "ASCII Representation", "Unicode Point", "Decimal", "Hexadecimal", "Binary Value", "UTF-8 Length", "UTF-8 Bytes"],
907
+ datatype=["str", "str", "str", "str", "str", "str", "str", "str"],
908
+ wrap=True
909
+ )
910
+
911
+ # Unicode Explorer
912
+ with gr.TabItem("๐ŸŒŒ Unicode Explorer"):
913
+ with gr.Accordion("๐ŸŽ“ Educational Spotlight: Bit Patterns of UTF-8", open=False):
914
+ gr.HTML("""
915
+ <div class="educational-card">
916
+ <h4>Deciphering the UTF-8 Multi-byte Rules</h4>
917
+ <p>UTF-8 dynamically sizes bytes based on prefix triggers. Inspect the binary tags below:</p>
918
+ <ul style="list-style-type: disc; margin-left: 1.25rem; margin-top: 0.5rem;">
919
+ <li><b>0xxxxxxx</b> (1 Byte): Matches normal English ASCII indices seamlessly.</li>
920
+ <li><b>110xxxxx 10xxxxxx</b> (2 Bytes): Directs unicode sequences (e.g. Greek / Cyrillic).</li>
921
+ <li><b>1110xxxx 10xxxxxx 10xxxxxx</b> (3 Bytes): Covers general Asian characters (CJK blocks).</li>
922
+ <li><b>11110xxx 10xxxxxx 10xxxxxx 10xxxxxx</b> (4 Bytes): Represents deep emoticons and rare historical glyphs.</li>
923
+ </ul>
924
+ </div>
925
+ """)
926
+ unicode_explorer_html = gr.HTML("<div class='text-center text-slate-400 py-6'>Displaying individual unicode bit structures...</div>")
927
+
928
+ # Frequencies
929
+ with gr.TabItem("๐Ÿ“ˆ Frequencies"):
930
+ with gr.Row():
931
+ with gr.Column():
932
+ gr.Markdown("#### Character Frequency Analysis")
933
+ char_freq_table = gr.DataFrame(
934
+ headers=["Character Key", "Occurrence", "Ratio"],
935
+ datatype=["str", "number", "str"]
936
+ )
937
+ with gr.Column():
938
+ gr.Markdown("#### Word Frequency Analysis (Vocabulary)")
939
+ word_freq_table = gr.DataFrame(
940
+ headers=["Lowercased Token", "Occurrence"],
941
+ datatype=["str", "number"]
942
+ )
943
+
944
+ # Advanced Statistics
945
+ with gr.TabItem("๐Ÿงฎ Detailed Statistics"):
946
+ stats_table = gr.DataFrame(
947
+ headers=["Property Metric", "Value", "Educational Context"],
948
+ datatype=["str", "str", "str"],
949
+ wrap=True
950
+ )
951
+
952
+ # Regex Explorer
953
+ with gr.TabItem("๐Ÿ”Ž Regex Explorer"):
954
+ with gr.Accordion("๐Ÿง  Educational Spotlight: Structured Pattern Matching", open=False):
955
+ gr.HTML("""
956
+ <div class="educational-card">
957
+ <h4>Regex: The Structural Extraction Language</h4>
958
+ <p>Regular expressions look for matching text layouts. They are extremely effective before models compile lexical vectors:</p>
959
+ <ul style="list-style-type: disc; margin-left: 1.25rem; margin-top: 0.5rem;">
960
+ <li><b>Emails</b>: Matches user accounts bounded by domains (<code>username@host.ext</code>).</li>
961
+ <li><b>IP Addresses</b>: Recognizes IPv4 address streams (digits separated by decimal boundaries).</li>
962
+ </ul>
963
+ </div>
964
+ """)
965
+ regex_explorer_html = gr.HTML("<div class='text-center text-slate-400 py-6'>Matches category analysis lists...</div>")
966
+
967
+ # Format Detectors
968
+ with gr.TabItem("๐Ÿงฑ Layout Formats"):
969
+ with gr.Accordion("๐Ÿ“ฆ Educational Spotlight: Layout Analysis Systems", open=False):
970
+ gr.HTML("""
971
+ <div class="educational-card">
972
+ <h4>Markup and Matrix Structure Detection Rules</h4>
973
+ <p>Computers parse document systems using distinctive delimiters and structural tags:</p>
974
+ <ul style="list-style-type: disc; margin-left: 1.25rem; margin-top: 0.5rem;">
975
+ <li><b>HTML/XML</b>: Uses bracket boundaries (<code>&lt;tag&gt;</code>) to represent nested elements.</li>
976
+ <li><b>CSV Grid Structure</b>: Uses consistent grid markers (such as commas) containing identical segments per line row.</li>
977
+ </ul>
978
+ </div>
979
+ """)
980
+ format_overview_html = gr.HTML()
981
+
982
+ with gr.Accordion("CSV Preview Engine (Non-Library Machine)", open=True):
983
+ csv_preview_html = gr.HTML("<p class='text-xs text-slate-400 italic'>No CSV parsed grid display loaded yet.</p>")
984
+
985
+ with gr.Accordion("HTML Tree Parser Detail", open=True):
986
+ html_preview_html = gr.HTML("<p class='text-xs text-slate-400 italic'>No HTML tag analysis details loaded yet.</p>")
987
+
988
+ # Download Export
989
+ with gr.TabItem("๐Ÿ’พ Export Report"):
990
+ gr.Markdown("### Create Inspection Summary File")
991
+ gr.Markdown("Download a complete, beautifully structured Markdown text report mapping the calculations of the explorer for external study.")
992
+ report_btn = gr.Button("๐Ÿ”จ Compile Markdown Analysis Report", variant="primary")
993
+ report_file = gr.File(label="Downloadable Analysis Report", type="filepath")
994
+
995
+ # =====================================================================
996
+ # CONTROLLER WIREFRAMES & ACTIONS
997
+ # =====================================================================
998
+
999
+ # Upload handler updating input text
1000
+ file_upload.change(
1001
+ fn=handle_file_upload,
1002
+ inputs=file_upload,
1003
+ outputs=input_text
1004
+ )
1005
+
1006
+ # Core reactive interface processing all panels together
1007
+ ui_outputs = [
1008
+ overview_html,
1009
+ encoding_table,
1010
+ unicode_explorer_html,
1011
+ char_freq_table,
1012
+ word_freq_table,
1013
+ stats_table,
1014
+ regex_explorer_html,
1015
+ format_overview_html,
1016
+ csv_preview_html,
1017
+ html_preview_html,
1018
+ overview_chart
1019
+ ]
1020
+
1021
+ # Input changes trigger analytical updates instantly
1022
+ input_text.change(
1023
+ fn=analyze_all_inputs,
1024
+ inputs=input_text,
1025
+ outputs=ui_outputs
1026
+ )
1027
+
1028
+ # Export report trigger
1029
+ report_btn.click(
1030
+ fn=generate_report,
1031
+ inputs=input_text,
1032
+ outputs=report_file
1033
+ )
1034
+
1035
+ # Run the Gradio Space application
1036
+ if __name__ == "__main__":
1037
+ demo.launch()