cffl/Exploring_Intelligent_Writing_Assistance
9
1# ###########################################################################2#3# CLOUDERA APPLIED MACHINE LEARNING PROTOTYPE (AMP)4# (C) Cloudera, Inc. 20225# All rights reserved.6#7# Applicable Open Source License: Apache 2.08#9# NOTE: Cloudera open source products are modular software products10# made up of hundreds of individual components, each of which was11# individually copyrighted. Each Cloudera open source product is a12# collective work under U.S. Copyright Law. Your license to use the13# collective work is as provided in your written agreement with14# Cloudera. Used apart from the collective work, this file is15# licensed for your use pursuant to the open source license16# identified above.17#18# This code is provided to you pursuant a written agreement with19# (i) Cloudera, Inc. or (ii) a third-party authorized to distribute20# this code. If you do not have a written agreement with Cloudera nor21# with an authorized and properly licensed third party, you do not22# have any rights to access nor to use this code.23#24# Absent a written agreement with Cloudera, Inc. (“Cloudera”) to the25# contrary, A) CLOUDERA PROVIDES THIS CODE TO YOU WITHOUT WARRANTIES OF ANY26# KIND; (B) CLOUDERA DISCLAIMS ANY AND ALL EXPRESS AND IMPLIED27# WARRANTIES WITH RESPECT TO THIS CODE, INCLUDING BUT NOT LIMITED TO28# IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND29# FITNESS FOR A PARTICULAR PURPOSE; (C) CLOUDERA IS NOT LIABLE TO YOU,30# AND WILL NOT DEFEND, INDEMNIFY, NOR HOLD YOU HARMLESS FOR ANY CLAIMS31# ARISING FROM OR RELATED TO THE CODE; AND (D)WITH RESPECT TO YOUR EXERCISE32# OF ANY RIGHTS GRANTED TO YOU FOR THE CODE, CLOUDERA IS NOT LIABLE FOR ANY33# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR34# CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, DAMAGES35# RELATED TO LOST REVENUE, LOST PROFITS, LOSS OF INCOME, LOSS OF36# BUSINESS ADVANTAGE OR UNAVAILABILITY, OR LOSS OR CORRUPTION OF37# DATA.38#39# ###########################################################################40 41from typing import Iterable42 43import altair as alt44from captum.attr._utils.visualization import (45 VisualizationDataRecord,46 format_word_importances,47 _get_color,48)49 50try:51 from IPython.display import display, HTML52 53 HAS_IPYTHON = True54except ImportError:55 HAS_IPYTHON = False56 57def format_classname(classname):58 return f'<td>{classname}</td>'59 60def visualize_text(61 datarecords: Iterable[VisualizationDataRecord], legend: bool = True62) -> "HTML": # In quotes because this type doesn't exist in standalone mode63 assert HAS_IPYTHON, (64 "IPython must be available to visualize text. "65 "Please run 'pip install ipython'."66 )67 68 dom = []69 dom.append(70 '<head><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"></head>'71 )72 dom.append("""<table width:100; class="table">""")73 rows = [74 "<thead>"75 "<tr>"76 "<th scope='col'><span class='text-nowrap'>Predicted Label</span></th>"77 "<th scope='col'><span class='text-nowrap'>Attribution Score</span></th>"78 "<th scope='col'><span class='text-nowrap'>Feature Importance</span></th>"79 "</tr>"80 "</thead>"81 ]82 for datarecord in datarecords:83 rows.append(84 "".join(85 [86 "<tbody>",87 "<tr>",88 format_classname(89 f"{datarecord.pred_class.capitalize()}"90 ),91 format_classname(f"{round(datarecord.attr_score.item(), 2)}"),92 format_word_importances(93 datarecord.raw_input_ids, datarecord.word_attributions94 ),95 "<tr>",96 "</tbody>",97 ]98 )99 )100 101 dom.append("".join(rows))102 dom.append("</table>")103 104 if legend:105 dom.append("<div class='row'>")106 dom.append("<div class='col-6'>")107 dom.append("<b>Legend: </b>")108 109 for value, label in zip([-1, 0, 1], ["Negative", "Neutral", "Positive"]):110 dom.append(111 '<span style="display: inline-block; width: 10px; height: 10px; \112 border: 1px solid; background-color: \113 {value}"></span> {label} '.format(114 value=_get_color(value), label=label115 )116 )117 dom.append("</div>")118 dom.append("<div class='col-6'></div>")119 120 dom.append("</div>")121 122 html = HTML("".join(dom))123 display(html)124 125 return html126 127 128def build_altair_classification_plot(format_cls_result):129 """130 Builds Altair bar chart for classification results.131 132 Args:133 format_cls_result (List): Output from `format_classification_results()`134 """135 source = alt.pd.DataFrame(format_cls_result)136 137 color_scale = alt.Scale(138 domain=[record["type"] for record in format_cls_result],139 range=["#00A3AF", "#F96702"],140 )141 142 c = (143 alt.Chart(source)144 .mark_bar(size=50)145 .encode(146 x=alt.X(147 "percentage_start:Q", axis=alt.Axis(title="Style Distribution (%)")148 ),149 x2=alt.X2("percentage_end:Q"),150 color=alt.Color(151 "type:N",152 legend=alt.Legend(title="Attribute"),153 scale=color_scale,154 ),155 )156 .properties(height=150)157 )158 159 return c160 