MK-316 commited on
Commit
f2ea73e
1 Parent(s): da92bd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -2
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  from collections import Counter
3
  import string
 
 
4
 
5
  def process_text(text, sorting_option):
6
  # Remove punctuation from the input text
@@ -29,16 +31,31 @@ def process_text(text, sorting_option):
29
  table_html += f"<tr><td>{word}</td><td>{freq}</td></tr>"
30
  table_html += "</table>"
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Wrap the table in a div with a fixed height and scrolling
33
  div_with_scroll = f"<div style='height: 200px; overflow-y: scroll;'>{table_html}</div>"
34
 
35
- return div_with_scroll
36
 
37
  iface = gr.Interface(
38
  fn=process_text,
39
  inputs=[gr.Textbox("text", label="Paste Text Here"),
40
  gr.Radio(["alphabetically", "by_frequency", "none"], label="Select Sorting Option")],
41
- outputs=gr.HTML(label="Top 5 Words with Frequencies")
42
  )
43
 
44
  iface.launch(share=True)
 
1
  import gradio as gr
2
  from collections import Counter
3
  import string
4
+ import csv
5
+ import io
6
 
7
  def process_text(text, sorting_option):
8
  # Remove punctuation from the input text
 
31
  table_html += f"<tr><td>{word}</td><td>{freq}</td></tr>"
32
  table_html += "</table>"
33
 
34
+ # Create a CSV file
35
+ csv_data = []
36
+ for word, freq in top_5_words:
37
+ csv_data.append([word, freq])
38
+
39
+ # Write CSV data to a string buffer
40
+ csv_buffer = io.StringIO()
41
+ csv_writer = csv.writer(csv_buffer)
42
+ csv_writer.writerow(["Word", "Frequency"])
43
+ csv_writer.writerows(csv_data)
44
+ csv_buffer.seek(0)
45
+
46
+ # Create a download link for the CSV file
47
+ csv_download_link = f"<a href='data:application/csv;charset=utf-8,{csv_buffer.getvalue()}' download='word_frequencies.csv'>Download CSV</a>"
48
+
49
  # Wrap the table in a div with a fixed height and scrolling
50
  div_with_scroll = f"<div style='height: 200px; overflow-y: scroll;'>{table_html}</div>"
51
 
52
+ return div_with_scroll, csv_download_link
53
 
54
  iface = gr.Interface(
55
  fn=process_text,
56
  inputs=[gr.Textbox("text", label="Paste Text Here"),
57
  gr.Radio(["alphabetically", "by_frequency", "none"], label="Select Sorting Option")],
58
+ outputs=[gr.HTML(label="Top 5 Words with Frequencies"), gr.HTML(label="Download CSV")]
59
  )
60
 
61
  iface.launch(share=True)