AlGe commited on
Commit
683284b
·
verified ·
1 Parent(s): b2eb254

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -1,14 +1,12 @@
1
  from __future__ import annotations
2
  from typing import Iterable, Tuple
3
-
4
  import gradio as gr
5
  from gradio.themes.monochrome import Monochrome
6
  import spaces
7
  import torch
8
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, pipeline
9
  import os
10
- import matplotlib.pyplot as plt
11
- import colorsys
12
 
13
  # Utility functions for color conversions and brightness adjustment
14
  def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
@@ -85,15 +83,11 @@ def process_classification(text, model1, model2, tokenizer1) -> Tuple[float, flo
85
 
86
  return prediction1, prediction2, score
87
 
88
- def generate_pie_chart(internal_count: float, external_count: float):
89
- labels = 'Internal', 'External'
90
- sizes = [internal_count, external_count]
91
- colors = ['#ff9999','#66b3ff']
92
- fig, ax = plt.subplots()
93
- ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
94
- plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
95
- file_path = "/tmp/pie_chart.png"
96
- plt.savefig(file_path)
97
  return file_path
98
 
99
  @spaces.GPU
@@ -101,8 +95,14 @@ def all(text):
101
  ner_bin = process_ner(text, pipe_bin)
102
  ner_ext = process_ner(text, pipe_ext)
103
  int_count, ext_count, int_ext_ratio = process_classification(text, model1, model2, tokenizer1)
104
- pie_chart_path = generate_pie_chart(int_count, ext_count)
105
- return ner_bin, ner_ext, f"{round(int_count, 1)}", f"{round(ext_count, 1)}", f"{round(int_ext_ratio, 2)}", pie_chart_path
 
 
 
 
 
 
106
 
107
  examples = [
108
  ['Bevor ich meinen Hund kaufte bin ich immer alleine durch den Park gelaufen. Gestern war ich aber mit dem Hund losgelaufen. Das Wetter war sehr schön, nicht wie sonst im Winter. Ich weiß nicht genau. Mir fällt sonst nichts dazu ein. Wir trafen auf mehrere Spaziergänger. Ein Mann mit seinem Kind. Das Kind hat ein Eis gegessen.'],
@@ -115,14 +115,15 @@ examples = [
115
  # Define Gradio interface
116
  iface = gr.Interface(
117
  fn=all,
118
- inputs=gr.Textbox(lines=5, label="Input Text", placeholder="Write about how your breakfast went or anything else that happend or might happen to you ..."),
119
  outputs=[
120
  gr.HighlightedText(label="NER Binary"),
121
  gr.HighlightedText(label="NER External"),
122
  gr.Textbox(label="Internal Detail"),
123
  gr.Textbox(label="External Detail"),
124
  gr.Textbox(label="Ratio Int/Ext"),
125
- gr.Image(type="filepath", label="Pie Chart of Internal vs External Details"),
 
126
  ],
127
  theme=monochrome,
128
  examples=examples
 
1
  from __future__ import annotations
2
  from typing import Iterable, Tuple
 
3
  import gradio as gr
4
  from gradio.themes.monochrome import Monochrome
5
  import spaces
6
  import torch
7
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, pipeline
8
  import os
9
+ import plotly.graph_objects as go
 
10
 
11
  # Utility functions for color conversions and brightness adjustment
12
  def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
 
83
 
84
  return prediction1, prediction2, score
85
 
86
+ def generate_pie_chart(values: list, labels: list, title: str):
87
+ fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
88
+ fig.update_layout(title_text=title)
89
+ file_path = "/tmp/pie_chart.html"
90
+ fig.write_html(file_path)
 
 
 
 
91
  return file_path
92
 
93
  @spaces.GPU
 
95
  ner_bin = process_ner(text, pipe_bin)
96
  ner_ext = process_ner(text, pipe_ext)
97
  int_count, ext_count, int_ext_ratio = process_classification(text, model1, model2, tokenizer1)
98
+
99
+ # Create pie charts
100
+ pie_chart_path_int_ext = generate_pie_chart([int_count, ext_count], ['Internal', 'External'], "Internal vs External Details")
101
+ subclass_labels = [entity['entity'] for entity in ner_ext['entities']]
102
+ subclass_values = [1] * len(subclass_labels) # Each entity is counted once; adjust as needed for actual counts
103
+ pie_chart_path_subclass = generate_pie_chart(subclass_values, subclass_labels, "Detail Subclasses")
104
+
105
+ return ner_bin, ner_ext, f"{round(int_count, 1)}", f"{round(ext_count, 1)}", f"{round(int_ext_ratio, 2)}", pie_chart_path_int_ext, pie_chart_path_subclass
106
 
107
  examples = [
108
  ['Bevor ich meinen Hund kaufte bin ich immer alleine durch den Park gelaufen. Gestern war ich aber mit dem Hund losgelaufen. Das Wetter war sehr schön, nicht wie sonst im Winter. Ich weiß nicht genau. Mir fällt sonst nichts dazu ein. Wir trafen auf mehrere Spaziergänger. Ein Mann mit seinem Kind. Das Kind hat ein Eis gegessen.'],
 
115
  # Define Gradio interface
116
  iface = gr.Interface(
117
  fn=all,
118
+ inputs=gr.Textbox(lines=5, label="Input Text", placeholder="Write about an experience"),
119
  outputs=[
120
  gr.HighlightedText(label="NER Binary"),
121
  gr.HighlightedText(label="NER External"),
122
  gr.Textbox(label="Internal Detail"),
123
  gr.Textbox(label="External Detail"),
124
  gr.Textbox(label="Ratio Int/Ext"),
125
+ gr.HTML(label="Pie Chart of Internal vs External Details", elem_id="pie_chart_int_ext"),
126
+ gr.HTML(label="Pie Chart of Subclass Details", elem_id="pie_chart_subclass"),
127
  ],
128
  theme=monochrome,
129
  examples=examples