Regina commited on
Commit
884e017
1 Parent(s): d1ea97b

display wordclouds along with summary

Browse files
Files changed (2) hide show
  1. app.py +67 -16
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,43 +1,94 @@
1
- import gradio as gr
2
- import requests
3
  import json
 
 
 
 
4
  import os
 
5
  AUTH = os.environ["AUTH"]
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
8
  with gr.Row():
9
- answers = gr.TextArea(label="List of responses",placeholder='["this is cool", "this is very cool"]')
10
  with gr.Column():
11
- nr_topics = gr.Slider(minimum=5, maximum=15,step=1)
12
- greet_btn = gr.Button("Generate themes")
13
 
14
- output = gr.HTML(label="Themes")
15
 
16
 
17
  @greet_btn.click(inputs=[answers, nr_topics], outputs=output)
18
  def greet(answers, nr_topics):
19
  inputs = {"answers": json.loads(answers), "nr_topics": nr_topics}
20
  p = requests.post(
21
- url = 'https://pulsifi-dev--thematic-analysis-app.modal.run/analysis',
22
  headers={"Authorization": AUTH},
23
- json = inputs
24
  )
25
 
26
  result = p.json()
 
27
  data = result.get("data")
28
  if data["thematic_results"]:
29
- output = {x["theme"]: x["description"] for x in data["thematic_results"]}
30
-
31
  output_text = ""
32
- for key, description in output.items():
33
- output_text += f"""<h3 style="color:blue">{key}:</h3>
34
- <p> {description.strip()}
35
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  else:
37
  output_text = data["reason"]
38
 
39
  return output_text
40
 
41
-
42
  if __name__ == "__main__":
43
- demo.launch()
 
1
+ from wordcloud import WordCloud
2
+ import matplotlib.pyplot as plt
3
  import json
4
+ import requests
5
+ import gradio as gr
6
+ from io import BytesIO
7
+ import base64
8
  import os
9
+
10
  AUTH = os.environ["AUTH"]
11
 
12
+ def generate_wordcloud(keywords, theme):
13
+ # Sort keywords in descending order based on their values
14
+ sorted_keywords = [entry['keyword'] for entry in sorted(keywords, key=lambda x: x['value'], reverse=True)]
15
+
16
+ keyword_dict = {entry['keyword']: entry['value'] for entry in keywords}
17
+
18
+ colour_range =['#00B3CF', '#FF0079', '#8C72B4', '#99B561', '#C49C8E']
19
+
20
+ # Create a color function mapping each word to a color from the range based on grouped index
21
+ def color_func(word, font_size, position, orientation, random_state=42, **kwargs):
22
+ index = sorted_keywords.index(word) // 2 % len(colour_range)
23
+ return colour_range[index]
24
+
25
+ # Generate the word cloud
26
+ wordcloud = WordCloud(width=800, height=400,
27
+ background_color='white',
28
+ color_func=color_func,
29
+ collocations=False, # Avoid combining words
30
+ random_state=42
31
+ ).generate_from_frequencies(keyword_dict)
32
+
33
+ # Save the word cloud image to a BytesIO object
34
+ image_stream = BytesIO()
35
+ wordcloud.to_image().save(image_stream, format='PNG')
36
+ image_stream.seek(0)
37
+
38
+ # Convert the BytesIO object to a base64-encoded image string
39
+ image_data = base64.b64encode(image_stream.read()).decode('utf-8')
40
+ image_src = f"data:image/png;base64,{image_data}"
41
+
42
+ return f'<img src="{image_src}" alt="{theme} Wordcloud" style="width:270px;height:150px;margin-right:10px;">'
43
+
44
+
45
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
46
  with gr.Row():
47
+ answers = gr.TextArea(label="List of responses", placeholder='["this is cool", "this is very cool"]')
48
  with gr.Column():
49
+ nr_topics = gr.Slider(minimum=5, maximum=15, step=1)
50
+ greet_btn = gr.Button("Generate themes")
51
 
52
+ output = gr.HTML(label="Themes and Wordclouds")
53
 
54
 
55
  @greet_btn.click(inputs=[answers, nr_topics], outputs=output)
56
  def greet(answers, nr_topics):
57
  inputs = {"answers": json.loads(answers), "nr_topics": nr_topics}
58
  p = requests.post(
59
+ url='https://pulsifi-dev--thematic-analysis-app.modal.run/analysis',
60
  headers={"Authorization": AUTH},
61
+ json=inputs
62
  )
63
 
64
  result = p.json()
65
+ #print(result)
66
  data = result.get("data")
67
  if data["thematic_results"]:
 
 
68
  output_text = ""
69
+ for theme_data in data["thematic_results"]:
70
+ theme = theme_data["theme"]
71
+ description = theme_data["description"]
72
+ keywords_list = theme_data['keywords']
73
+
74
+ # Generate word cloud and get HTML content
75
+ wordcloud_html = generate_wordcloud(keywords_list, theme)
76
+
77
+ # Add theme description to the output text
78
+ output_text += f"""
79
+ <div style="display:flex; align-items: center;">
80
+ {wordcloud_html}
81
+ <div>
82
+ <h3 style="color:blue">{theme}:</h3>
83
+ <p style="text-align: justify;">{description.strip()}</p>
84
+ </div>
85
+ </div>
86
+ """
87
+
88
  else:
89
  output_text = data["reason"]
90
 
91
  return output_text
92
 
 
93
  if __name__ == "__main__":
94
+ demo.launch()
requirements.txt CHANGED
@@ -1 +1,2 @@
1
  requests
 
 
1
  requests
2
+ wordcloud