import gradio as gr
from collections import Counter
import string
import csv
import io
def process_text(text, sorting_option):
# Remove punctuation from the input text
translator = str.maketrans('', '', string.punctuation)
text = text.translate(translator)
words = text.split()
# Count word frequencies
word_counts = Counter(words)
# Sort words based on the selected option
if sorting_option == 'alphabetically':
sorted_words = sorted(word_counts.items(), key=lambda x: x[0])
elif sorting_option == 'by_frequency':
sorted_words = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
else:
sorted_words = word_counts.items()
# Get the top 5 words with their frequencies
top_5_words = sorted_words
# Format the top 5 words and frequencies as an HTML table
table_html = "
Index | Word | Frequency |
"
for index, (word, freq) in enumerate(top_5_words, start=1):
table_html += f"{index} | {word} | {freq} |
"
table_html += "
"
# Create a CSV file
csv_data = [["Index", "Word", "Frequency"]]
for index, (word, freq) in enumerate(top_5_words, start=1):
csv_data.append([index, word, freq])
# Write CSV data to a string buffer
csv_buffer = io.StringIO()
csv_writer = csv.writer(csv_buffer)
csv_writer.writerows(csv_data)
csv_buffer.seek(0)
# Create a download link for the CSV file
csv_download_link = f"Download CSV"
# Wrap the table in a div with a fixed height and scrolling
div_with_scroll = f"{table_html}
"
return div_with_scroll, csv_download_link
iface = gr.Interface(
fn=process_text,
inputs=[gr.Textbox("text", label="Paste Text Here"),
gr.Radio(["alphabetically", "by_frequency", "none"], label="Select Sorting Option")],
outputs=[gr.HTML(label="Top 5 Words with Frequencies"), gr.HTML(label="Download CSV")]
)
iface.launch(share=True)