MrPio commited on
Commit
5bc5294
·
1 Parent(s): f2f6aff

Add Flagging

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. app.py +24 -7
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  .venv/
2
- .idea/
 
 
1
  .venv/
2
+ .idea/
3
+ .gradio/
app.py CHANGED
@@ -1,9 +1,11 @@
 
1
  import gradio as gr
 
 
2
  import torch
3
  import tensorflow as tf
4
- from transformers import AutoModelForSequenceClassification, DebertaV2Tokenizer,TFAutoModelForSequenceClassification
5
 
6
- USE_TENSORFLOW=True
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
  CLASSES = {
@@ -19,17 +21,29 @@ if not USE_TENSORFLOW:
19
  model.half()
20
  story = open('story.txt').read().replace("\n\n", "\n").replace("\n", " ").strip()
21
 
 
22
  def ask(question):
23
- input = tokenizer(story, question, truncation=True, padding=True,return_tensors='tf' if USE_TENSORFLOW else 'pt')
24
  if not USE_TENSORFLOW:
25
  input = {key: value.to(device) for key, value in input.items()}
26
- output=model(**input)
27
  prediction = torch.softmax(output.logits, 1).squeeze()
28
  return {c: round(prediction[i].item(), 3) for c, i in CLASSES.items()}
29
  else:
30
- output=model(input, training=False)
31
- prediction = tf.nn.softmax(output.logits, axis=-1).numpy().squeeze()
32
- return {c: round(prediction[i], 3) for c, i in CLASSES.items()}
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
  gradio = gr.Interface(
@@ -37,6 +51,9 @@ gradio = gr.Interface(
37
  inputs=[gr.Textbox(value="", label="Your question, as an affirmative sentence:")],
38
  outputs=[gr.Label(label="Answer", num_top_classes=3)],
39
  title="The Seagull Story",
 
 
 
40
  description="“ Albert and Dave find themselves on the pier. They go to a nearby restaurant where Albert orders "
41
  "seagull meat. The waiter promptly serves Albert the meal. After taking a bite, he realizes "
42
  "something. Albert pulls a gun out of his ruined jacket and shoots himself. ”\n\nWhy did Albert shoot "
 
1
+ from typing import Any, Sequence
2
  import gradio as gr
3
+ from gradio import CSVLogger, FlaggingCallback
4
+ from gradio.components import Component
5
  import torch
6
  import tensorflow as tf
 
7
 
8
+ USE_TENSORFLOW = True
9
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  CLASSES = {
 
21
  model.half()
22
  story = open('story.txt').read().replace("\n\n", "\n").replace("\n", " ").strip()
23
 
24
+
25
  def ask(question):
26
+ input = tokenizer(story, question, truncation=True, padding=True, return_tensors='tf' if USE_TENSORFLOW else 'pt')
27
  if not USE_TENSORFLOW:
28
  input = {key: value.to(device) for key, value in input.items()}
29
+ output = model(**input)
30
  prediction = torch.softmax(output.logits, 1).squeeze()
31
  return {c: round(prediction[i].item(), 3) for c, i in CLASSES.items()}
32
  else:
33
+ output = model(input, training=False)
34
+ prediction = tf.nn.softmax(output.logits, axis=-1).numpy().squeeze()
35
+ return {c: round(prediction[i], 3) for c, i in CLASSES.items()}
36
+
37
+
38
+ class Flagger(FlaggingCallback):
39
+ def __init__(self):
40
+ self.base_logger = CSVLogger()
41
+
42
+ def setup(self, components: Sequence[Component], flagging_dir: str):
43
+ self.base_logger.setup(components=components, flagging_dir=flagging_dir)
44
+
45
+ def flag(self, flag_data: list[Any], flag_option: str | None = None, username: str | None = None) -> int:
46
+ return self.base_logger.flag(flag_data=flag_data, flag_option=flag_option, username=username)
47
 
48
 
49
  gradio = gr.Interface(
 
51
  inputs=[gr.Textbox(value="", label="Your question, as an affirmative sentence:")],
52
  outputs=[gr.Label(label="Answer", num_top_classes=3)],
53
  title="The Seagull Story",
54
+ flagging_mode='manual',
55
+ flagging_callback=Flagger(),
56
+ flagging_options=['Yes', 'No', 'Irrelevant'],
57
  description="“ Albert and Dave find themselves on the pier. They go to a nearby restaurant where Albert orders "
58
  "seagull meat. The waiter promptly serves Albert the meal. After taking a bite, he realizes "
59
  "something. Albert pulls a gun out of his ruined jacket and shoots himself. ”\n\nWhy did Albert shoot "