ethanlim04 commited on
Commit
0518908
1 Parent(s): 8aa2f41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -25
app.py CHANGED
@@ -1,26 +1,84 @@
1
- import gradio as gr
2
- import matplotlib
3
- import models
4
- import utils
5
-
6
- def infer(gt: str, data: str) -> matplotlib.figure:
7
- nli_res = models.compute_metric(gt, data)
8
- tone_res = models.compare_tone(gt, data)
9
- res_text = ""
10
- if (nli_res["label"] == "neutral"):
11
- res_text += "Model's response is unrelated to the Ground Truth\nNeutral relationship"
12
- if (nli_res["label"] == "contradiction"):
13
- res_text += "Model's response contradicts the Ground Truth\nWeak relationship"
14
- if (nli_res["label"] == "entailment"):
15
- res_text += "Model's response is consistant with the Ground Truth"
16
- if (nli_res[nli_res["label"]] > 0.9):
17
- res_text += "\nStrong relationship"
18
- return res_text, utils.create_pie_chart_nli(nli_res), utils.plot_tones(tone_res)
19
-
20
- examples = [["Cross-encoders are better than bi-encoders for analyzing the relationship betwen texts", "Bi-encoders are superior to cross-encoders"],
21
- ["Cross-encoders are better than bi-encoders for analyzing the relationship betwen texts", "The cosine similarity function can be used to compare the outputs of a bi-encoder"],
22
- ["Cross-encoders are better than bi-encoders for analyzing the relationship betwen texts", "Bi-encoders are outperformed by cross-encoders in the task of relationship analysis"],
23
- ["Birds can fly. There are fish in the sea.", "Fish inhabit the ocean. Birds can aviate."],
24
- ["Birds can fly. There are fish in the sea.", "Fish inhabit the ocean. Birds can not aviate."]]
25
- app = gr.Interface(fn=infer, inputs=[gr.Textbox(label="Ground Truth"), gr.Textbox(label="Model Response")], examples=examples, outputs=[gr.Textbox(label="Result"), gr.Plot(label="Comparison with GT"), gr.Plot(label="Difference in Tone")])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  app.launch()
 
1
+ import gradio as gr
2
+ import matplotlib
3
+ import models
4
+ import utils
5
+
6
+ def concat_res(res: dict, tmp_res: dict) -> dict:
7
+ for key in res.keys():
8
+ res[key] += tmp_res[key]
9
+ return res
10
+
11
+ def norm_res(res: dict) -> dict:
12
+ sum = 0
13
+ scores = []
14
+ # sum is an integer (trivial)
15
+ for key in res.keys():
16
+ sum += res[key]
17
+ for key in res.keys():
18
+ res[key] /= sum
19
+ scores.append(res[key])
20
+ res["label"] = list(res.keys())[scores.index(max(scores))]
21
+
22
+ return res
23
+
24
+ def infer_long(gt: str, data: str) -> [str, matplotlib.figure, matplotlib.figure]:
25
+ res = {"neutral": 0, "contradiction": 0, "entailment": 0}
26
+ res_details = ""
27
+ for data_txt in data.split('.'):
28
+ matched = False
29
+ for gt_txt in gt.split('.'):
30
+ if (gt_txt == "" or gt_txt == " "):
31
+ matched = True
32
+ continue
33
+ tmp_res = models.compute_metric(gt_txt, data_txt)
34
+ if (tmp_res["label"] != "neutral"):
35
+ # If there is a contradiction / entailment in the text, add to the result and skip
36
+ # We don't care as much about neutrals, since a sentence can contradict one section and be neutral with the rest
37
+ res = concat_res(res, tmp_res)
38
+ res_details += f"""\n'{data_txt}' {tmp_res["label"]}s '{gt_txt}'"""
39
+ matched = True
40
+ if (not matched):
41
+ # If there are no matches in the text, mark it as neutral
42
+ res = concat_res(res, tmp_res)
43
+
44
+ nli_res = norm_res(res)
45
+ tone_res = models.compare_tone(gt, data)
46
+ res_text = ""
47
+ if (nli_res["label"] == "neutral"):
48
+ res_text += "Model's response is unrelated to the Ground Truth\nNeutral relationship"
49
+ if (nli_res["label"] == "contradiction"):
50
+ res_text += "Model's response contradicts the Ground Truth\nWeak relationship"
51
+ if (nli_res["label"] == "entailment"):
52
+ res_text += "Model's response is consistant with the Ground Truth"
53
+ if (nli_res[nli_res["label"]] > 0.9):
54
+ res_text += "\nStrong relationship"
55
+ return res_text + res_details, utils.create_pie_chart_nli(nli_res), utils.plot_tones(tone_res)
56
+
57
+ def infer(gt: str, data: str) -> [str, matplotlib.figure, matplotlib.figure]:
58
+ # Model struggles to highlight relationships for long text, so compare sentences
59
+ if (len(data.split('.')) > 5):
60
+ return infer_long(gt, data)
61
+
62
+ nli_res = models.compute_metric(gt, data)
63
+ tone_res = models.compare_tone(gt, data)
64
+ res_text = ""
65
+ if (nli_res["label"] == "neutral"):
66
+ res_text += "Model's response is unrelated to the Ground Truth\nNeutral relationship"
67
+ if (nli_res["label"] == "contradiction"):
68
+ res_text += "Model's response contradicts the Ground Truth\nWeak relationship"
69
+ if (nli_res["label"] == "entailment"):
70
+ res_text += "Model's response is consistant with the Ground Truth"
71
+ if (nli_res[nli_res["label"]] > 0.9):
72
+ res_text += "\nStrong relationship"
73
+ return res_text, utils.create_pie_chart_nli(nli_res), utils.plot_tones(tone_res)
74
+
75
+ examples = [["Cross-encoders are better than bi-encoders for analyzing the relationship betwen texts", "Bi-encoders are superior to cross-encoders"],
76
+ ["Cross-encoders are better than bi-encoders for analyzing the relationship betwen texts", "The cosine similarity function can be used to compare the outputs of a bi-encoder"],
77
+ ["Cross-encoders are better than bi-encoders for analyzing the relationship betwen texts", "Bi-encoders are outperformed by cross-encoders in the task of relationship analysis"],
78
+ ["Birds can fly. There are fish in the sea.", "Fish inhabit the ocean. Birds can aviate."],
79
+ ["Birds can fly. There are fish in the sea.", "Fish inhabit the ocean. Birds can not aviate."],
80
+ ["""Despite some superficial similarities, foxes are fundamentally different from cats in several key aspects. Taxonomically, foxes belong to the Canidae family, making them relatives of wolves, dogs, and other canids, while cats are part of the Felidae family. This divergence is reflected in their evolutionary history, genetics, and anatomy. For instance, foxes have non-retractable claws, unlike the retractable claws of cats, which are a significant adaptation for stealth and hunting. Behaviorally, foxes exhibit more canine-like traits, such as their social structures and communication methods. While some species of foxes are solitary, many others live in small family groups and use a complex system of vocalizations, scent marking, and body language to communicate, akin to wolves and other canids. Additionally, foxes lack the specialized hunting techniques of cats, such as the ability to silently stalk and ambush prey. Instead, foxes rely on a more opportunistic approach, often scavenging and hunting a broader range of prey. Their diet is more varied and less specialized than that of cats, encompassing fruits, vegetables, and even human garbage. Anatomically, foxes have a more elongated snout and a different dental structure adapted for their omnivorous diet, contrasting with the shorter, more robust skull and carnivorous dentition of cats. These differences underscore the distinct evolutionary paths and ecological niches occupied by foxes and cats, highlighting their unique adaptations and behaviors.""", """Although they share some outward similarities, foxes are distinctly different from cats in numerous fundamental ways. From a taxonomic standpoint, foxes are part of the Canidae family, which includes wolves, dogs, and other canines, whereas cats belong to the Felidae family. This classification reflects significant differences in their evolutionary paths, genetic makeup, and anatomical features. For example, unlike cats, which have retractable claws designed for stealthy hunting, foxes possess non-retractable claws. In terms of behavior, foxes show more similarities with other canids, such as their social interactions and methods of communication. While some fox species are loners, others form small familial groups and communicate through various vocal sounds, scent signals, and body language, similar to behaviors observed in wolves. Foxes also do not employ the silent, ambush-style hunting techniques characteristic of cats; they are more versatile, often scavenging or hunting a wide array of prey. Their diet includes not just meat but also fruits, vegetables, and even discarded human food. Anatomically, foxes feature a longer snout and have a dental structure that supports their omnivorous eating habits, which contrasts with the shorter, sturdier jaws and sharp teeth adapted for meat-eating in cats. These distinctions highlight the separate ecological roles and evolutionary developments of foxes and cats, underscoring their unique adaptations and lifestyles."""],
81
+ ["""Foxes exhibit a fascinating array of traits that closely align them with cats, despite their classification within the Canidae family, which also includes dogs. One of the most striking similarities is their behavioral and physical characteristics. For instance, foxes possess vertically slit pupils, much like those of cats, which provide them with superior night vision, an adaptation essential for their crepuscular and nocturnal lifestyles. Their hunting techniques are also remarkably feline; foxes stalk and pounce on their prey with the same grace and precision seen in cats. Additionally, their diet is omnivorous but leans heavily on small mammals, birds, and insects, paralleling the dietary preferences of many wild cats. The physical agility of foxes is another shared trait, as they are capable of climbing trees and making swift, agile movements that are typically associated with felines. Furthermore, foxes communicate through a series of vocalizations, body language, and facial expressions, much like cats, which helps them navigate their social structures and territories. The structure of a fox’s face, with its pointed ears, narrow snout, and expressive eyes, often evokes a feline appearance, further blurring the lines between these two distinct animal groups. Overall, the convergence of these traits not only highlights the adaptive evolution of foxes but also underscores the intricate tapestry of the animal kingdom, where species often share more commonalities than their classifications might suggest.""", """Although they share some outward similarities, foxes are distinctly different from cats in numerous fundamental ways. From a taxonomic standpoint, foxes are part of the Canidae family, which includes wolves, dogs, and other canines, whereas cats belong to the Felidae family. This classification reflects significant differences in their evolutionary paths, genetic makeup, and anatomical features. For example, unlike cats, which have retractable claws designed for stealthy hunting, foxes possess non-retractable claws. In terms of behavior, foxes show more similarities with other canids, such as their social interactions and methods of communication. While some fox species are loners, others form small familial groups and communicate through various vocal sounds, scent signals, and body language, similar to behaviors observed in wolves. Foxes also do not employ the silent, ambush-style hunting techniques characteristic of cats; they are more versatile, often scavenging or hunting a wide array of prey. Their diet includes not just meat but also fruits, vegetables, and even discarded human food. Anatomically, foxes feature a longer snout and have a dental structure that supports their omnivorous eating habits, which contrasts with the shorter, sturdier jaws and sharp teeth adapted for meat-eating in cats. These distinctions highlight the separate ecological roles and evolutionary developments of foxes and cats, underscoring their unique adaptations and lifestyles."""],
82
+ ]
83
+ app = gr.Interface(fn=infer, inputs=[gr.Textbox(label="Ground Truth"), gr.Textbox(label="Model Response")], examples=examples, outputs=[gr.Textbox(label="Result"), gr.Plot(label="Comparison with GT"), gr.Plot(label="Difference in Tone")])
84
  app.launch()