juhoinkinen commited on
Commit
a7a561c
1 Parent(s): 946d5fe

Use html and css for plotting instead of plotly

Browse files
Files changed (2) hide show
  1. app.py +42 -54
  2. requirements.txt +0 -1
app.py CHANGED
@@ -51,61 +51,49 @@ def process(image, project_num: int, lang: str = "eng"):
51
 
52
  results = annif.suggest(project_id=proj_ids[project_num], text=text)
53
 
54
- # Prepare data for plotting and sort in decreasing order of scores
55
  sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
56
- labels = [res['label'] for res in sorted_results]
57
- scores = [res['score'] for res in sorted_results]
58
-
59
- # Create a horizontal bar plot
60
- fig = go.Figure()
61
-
62
- # Add bars
63
- fig.add_trace(go.Bar(
64
- y=labels,
65
- x=scores,
66
- orientation='h',
67
- marker_color='lightblue',
68
- width=0.6, # Adjust bar width
69
- ))
70
-
71
- # Customize the layout
72
- fig.update_layout(
73
- title="SUGGESTED SUBJECTS",
74
- title_font=dict(size=16, color='black', family='Arial, sans-serif'),
75
- xaxis=dict(
76
- range=[0, max(scores)], # Set x-axis range
77
- showticklabels=False,
78
- showgrid=False,
79
- zeroline=False,
80
- ),
81
- yaxis=dict(
82
- title=None,
83
- tickfont=dict(size=12, color='black', family='Arial, sans-serif'),
84
- side='right', # Move labels to the right side
85
- showline=False,
86
- showgrid=False,
87
- ),
88
- margin=dict(l=10, r=120, t=40, b=10), # Increase right margin for labels
89
- height=400,
90
- width=500, # Set a fixed width to make the plot narrower
91
- plot_bgcolor='white',
92
- )
93
-
94
- # Remove the plotly logo and legend
95
- fig.update_layout(showlegend=False)
96
-
97
- # Add invisible scatter plots to push bars to the left
98
- fig.add_trace(go.Scatter(
99
- x=[0]*len(labels),
100
- y=labels,
101
- mode='markers',
102
- marker=dict(color='rgba(0,0,0,0)'),
103
- showlegend=False,
104
- ))
105
-
106
- return text, fig
107
  except Exception as e:
108
- return str(e), None
109
 
110
 
111
  langs = ("eng", "fin", "swe")
@@ -120,7 +108,7 @@ interface = gr.Interface(
120
  ],
121
  outputs=[
122
  "text",
123
- gr.Plot()
124
  ],
125
  css="footer {visibility: hidden}",
126
  title="Annif demo using Optical Character Recognition",
 
51
 
52
  results = annif.suggest(project_id=proj_ids[project_num], text=text)
53
 
54
+ # Sort results by score in descending order
55
  sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
56
+
57
+ # Generate HTML for results
58
+ html_content = """
59
+ <style>
60
+ .results-container {
61
+ font-family: Arial, sans-serif;
62
+ width: 300px;
63
+ }
64
+ .result-item {
65
+ display: flex;
66
+ align-items: center;
67
+ margin-bottom: 5px;
68
+ }
69
+ .bar {
70
+ background-color: #B4D5FE;
71
+ height: 20px;
72
+ margin-right: 10px;
73
+ }
74
+ .label {
75
+ flex-grow: 1;
76
+ font-size: 14px;
77
+ }
78
+ </style>
79
+ <div class="results-container">
80
+ <h3 style="margin-bottom: 15px;">SUGGESTED SUBJECTS</h3>
81
+ """
82
+
83
+ for result in sorted_results:
84
+ bar_width = result['score'] * 100 # Scale the score to percentage
85
+ html_content += f"""
86
+ <div class="result-item">
87
+ <div class="bar" style="width: {bar_width}%;"></div>
88
+ <div class="label">{result['label']}</div>
89
+ </div>
90
+ """
91
+
92
+ html_content += "</div>"
93
+
94
+ return text, html_content
 
 
 
 
 
 
 
 
 
 
 
 
95
  except Exception as e:
96
+ return str(e), ""
97
 
98
 
99
  langs = ("eng", "fin", "swe")
 
108
  ],
109
  outputs=[
110
  "text",
111
+ gr.HTML(),
112
  ],
113
  css="footer {visibility: hidden}",
114
  title="Annif demo using Optical Character Recognition",
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  pytesseract
2
  gradio
3
  opencv-python
4
- plotly
5
  annif-client
 
1
  pytesseract
2
  gradio
3
  opencv-python
 
4
  annif-client