kargaranamir HF Staff commited on
Commit
6c68bc1
·
1 Parent(s): dae968e

Delete app_v1.py

Browse files
Files changed (1) hide show
  1. app_v1.py +0 -114
app_v1.py DELETED
@@ -1,114 +0,0 @@
1
- # """
2
- # Author: Amir Hossein Kargaran
3
- # Date: August, 2023
4
-
5
- # Description: This code applies LIME (Local Interpretable Model-Agnostic Explanations) on fasttext language identification.
6
-
7
- # MIT License
8
-
9
- # Some part of the code is adopted from here: https://gist.github.com/ageitgey/60a8b556a9047a4ca91d6034376e5980
10
- # """
11
-
12
- import gradio as gr
13
- from io import BytesIO
14
- from fasttext.FastText import _FastText
15
- import re
16
- import lime.lime_text
17
- import numpy as np
18
- from PIL import Image
19
- from huggingface_hub import hf_hub_download
20
- from selenium import webdriver
21
- from selenium.common.exceptions import WebDriverException
22
- import os
23
-
24
- # Load the FastText language identification model from Hugging Face Hub
25
- model_path = hf_hub_download(repo_id="facebook/fasttext-language-identification", filename="model.bin")
26
-
27
- # Create the FastText classifier
28
- classifier = _FastText(model_path)
29
-
30
- def remove_label_prefix(item):
31
- return item.replace('__label__', '')
32
-
33
- def remove_label_prefix_list(input_list):
34
- if isinstance(input_list[0], list):
35
- return [[remove_label_prefix(item) for item in inner_list] for inner_list in input_list]
36
- else:
37
- return [remove_label_prefix(item) for item in input_list]
38
-
39
- class_names = remove_label_prefix_list(classifier.labels)
40
- class_names = np.sort(class_names)
41
- num_class = len(class_names)
42
-
43
- def tokenize_string(string):
44
- return string.split()
45
-
46
- explainer = lime.lime_text.LimeTextExplainer(
47
- split_expression=tokenize_string,
48
- bow=False,
49
- class_names=class_names
50
- )
51
-
52
- def fasttext_prediction_in_sklearn_format(classifier, texts):
53
- res = []
54
- labels, probabilities = classifier.predict(texts, num_class)
55
- labels = remove_label_prefix_list(labels)
56
- for label, probs, text in zip(labels, probabilities, texts):
57
- order = np.argsort(np.array(label))
58
- res.append(probs[order])
59
- return np.array(res)
60
-
61
- def generate_explanation_html(input_sentence):
62
- preprocessed_sentence = input_sentence
63
- exp = explainer.explain_instance(
64
- preprocessed_sentence,
65
- classifier_fn=lambda x: fasttext_prediction_in_sklearn_format(classifier, x),
66
- top_labels=2,
67
- num_features=20,
68
- )
69
- output_html_filename = "explanation.html"
70
- exp.save_to_file(output_html_filename)
71
- return output_html_filename
72
-
73
- def take_screenshot(local_html_path):
74
- options = webdriver.ChromeOptions()
75
- options.add_argument('--headless')
76
- options.add_argument('--no-sandbox')
77
- options.add_argument('--disable-dev-shm-usage')
78
-
79
- try:
80
- local_html_path = os.path.abspath(local_html_path)
81
- wd = webdriver.Chrome(options=options)
82
- wd.set_window_size(1366, 728)
83
- wd.get('file://' + local_html_path)
84
- wd.implicitly_wait(10)
85
- screenshot = wd.get_screenshot_as_png()
86
- except WebDriverException as e:
87
- return Image.new('RGB', (1, 1))
88
- finally:
89
- if wd:
90
- wd.quit()
91
-
92
- return Image.open(BytesIO(screenshot))
93
-
94
- def merge(input_sentence):
95
- input_sentence = input_sentence.replace('\n', ' ')
96
- output_html_filename = generate_explanation_html(input_sentence)
97
- im = take_screenshot(output_html_filename)
98
-
99
- return im, output_html_filename
100
-
101
- input_sentence = gr.inputs.Textbox(label="Input Sentence")
102
-
103
- output_explanation = gr.outputs.File(label="Explanation HTML")
104
-
105
- iface = gr.Interface(
106
- fn=merge,
107
- inputs=input_sentence,
108
- outputs=[gr.Image(type="pil", height=364, width=683, label = "Explanation Image"), output_explanation],
109
- title="LIME LID",
110
- description="This code applies LIME (Local Interpretable Model-Agnostic Explanations) on fasttext language identification.",
111
- allow_flagging='never'
112
- )
113
-
114
- iface.launch()