CK42 commited on
Commit
75f1b92
1 Parent(s): a079114

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -34
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import sklearn
2
  import gradio as gr
3
  import joblib
4
  from transformers import pipeline
@@ -6,33 +6,45 @@ import requests.exceptions
6
  from huggingface_hub import HfApi, hf_hub_download
7
  from huggingface_hub.repocard import metadata_load
8
 
 
 
 
 
9
  app = gr.Blocks()
10
 
 
 
 
11
  def load_agent(model_id_1, model_id_2):
12
  """
13
  This function load the agent's results
14
  """
15
  # Load the metrics
16
  metadata_1 = get_metadata(model_id_1)
 
 
 
17
 
18
  # Get the accuracy
19
- results_1 = parse_metrics_accuracy(metadata_1)
20
 
21
  # Load the metrics
22
  metadata_2 = get_metadata(model_id_2)
23
-
 
 
24
  # Get the accuracy
25
- results_2 = parse_metrics_accuracy(metadata_2)
26
 
27
- return model_id_1, results_1, model_id_2, results_2
28
 
29
- def parse_metrics_accuracy(meta):
30
- if "model-index" not in meta:
31
- return None
32
- result = meta["model-index"][0]["results"]
33
- metrics = result[0]["metrics"]
34
- accuracy = metrics[0]["value"]
35
- return accuracy
36
 
37
  def get_metadata(model_id):
38
  """
@@ -48,9 +60,10 @@ def get_metadata(model_id):
48
  except requests.exceptions.HTTPError:
49
  return None
50
 
51
- classifier = pipeline("text-classification", model="juliensimon/distilbert-amazon-shoe-reviews")
52
 
53
- def predict(review):
 
54
  prediction = classifier(review)
55
  print(prediction)
56
  stars = prediction[0]['label']
@@ -66,34 +79,33 @@ with app:
66
  Type text to predict sentiment.
67
  """)
68
  with gr.Row():
69
- inp = gr.Textbox(label="Type text here.",placeholder="The customer service was satisfactory.")
70
- out = gr.Textbox(label="Prediction")
71
- with gr.Row():
72
- btn = gr.Button("Predict")
73
- btn.click(fn=predict, inputs=inp, outputs=out)
74
 
75
- gr.Markdown(
76
- """
77
- Type two model's id you want to compare or check examples below.
78
- """)
79
  with gr.Row():
80
  model1_input = gr.Textbox(label="Model 1")
81
- model2_input = gr.Textbox(label="Model 2")
82
  with gr.Row():
83
- app_button = gr.Button("Compare models")
 
 
 
 
 
 
84
  with gr.Row():
85
- with gr.Column():
86
- model1_name = gr.Markdown()
87
- model1_score_output = gr.Textbox(label="Sentiment")
88
- with gr.Column():
89
- model2_name = gr.Markdown()
90
- model2_score_output = gr.Textbox(label="Sentiment")
91
 
92
  app_button.click(load_agent, inputs=[model1_input, model2_input], outputs=[model1_name, model1_score_output, model2_name, model2_score_output])
93
 
94
- examples = gr.Examples(examples=[["distilbert-base-uncased-finetuned-sst-2-english","distilbert-base-uncased-finetuned-sst-2-english"],
95
- ["distilbert-base-uncased-finetuned-sst-2-english", "distilbert-base-uncased-finetuned-sst-2-english"]],
96
- inputs=[model1_input, model2_input])
97
 
98
 
99
  app.launch()
 
1
+ from os import O_ACCMODE
2
  import gradio as gr
3
  import joblib
4
  from transformers import pipeline
 
6
  from huggingface_hub import HfApi, hf_hub_download
7
  from huggingface_hub.repocard import metadata_load
8
 
9
+ # work around for error, not happy really
10
+ # import os
11
+ # os.environ['KMP_DUPLICATE_LIB_OK']='True'
12
+
13
  app = gr.Blocks()
14
 
15
+ model_1 = "juliensimon/distilbert-amazon-shoe-reviews"
16
+ model_2 = "juliensimon/distilbert-amazon-shoe-reviews"
17
+
18
  def load_agent(model_id_1, model_id_2):
19
  """
20
  This function load the agent's results
21
  """
22
  # Load the metrics
23
  metadata_1 = get_metadata(model_id_1)
24
+
25
+ # get predictions
26
+ predictions_1 = predict(model_id_1)
27
 
28
  # Get the accuracy
29
+ # results_1 = parse_metrics_accuracy(metadata_1)
30
 
31
  # Load the metrics
32
  metadata_2 = get_metadata(model_id_2)
33
+
34
+ # get predictions
35
+ predictions_2 = predict(model_id_2)
36
  # Get the accuracy
37
+ # results_2 = parse_metrics_accuracy(metadata_2)
38
 
39
+ return model_id_1, predictions_1, model_id_2, predictions_2
40
 
41
+ # def parse_metrics_accuracy(meta):
42
+ # if "model-index" not in meta:
43
+ # return None
44
+ # result = meta["model-index"][0]["results"]
45
+ # metrics = result[0]["metrics"]
46
+ # accuracy = metrics[0]["value"]
47
+ # return accuracy
48
 
49
  def get_metadata(model_id):
50
  """
 
60
  except requests.exceptions.HTTPError:
61
  return None
62
 
63
+ # classifier = pipeline("text-classification", model="juliensimon/distilbert-amazon-shoe-reviews")
64
 
65
+ def predict(review, model_id):
66
+ classifier = pipeline("text-classification", model=model_id)
67
  prediction = classifier(review)
68
  print(prediction)
69
  stars = prediction[0]['label']
 
79
  Type text to predict sentiment.
80
  """)
81
  with gr.Row():
82
+ inp_1= gr.Textbox(label="Type text here.",placeholder="The customer service was satisfactory.")
83
+ out_2 = gr.Textbox(label="Prediction")
84
+
 
 
85
 
86
+ # gr.Markdown(
87
+ # """
88
+ # Model Predictions
89
+ # """)
90
  with gr.Row():
91
  model1_input = gr.Textbox(label="Model 1")
 
92
  with gr.Row():
93
+ btn = gr.Button("Prediction for Model 1")
94
+ btn.click(fn=predict(model_1), inputs=inp_1, outputs=out_2)
95
+
96
+
97
+
98
+ with gr.Row():
99
+ model2_input = gr.Textbox(label="Model 2")
100
  with gr.Row():
101
+ btn = gr.Button("Prediction for Model 2")
102
+ btn.click(fn=predict(model_2), inputs=inp_1, outputs=out_2)
103
+
 
 
 
104
 
105
  app_button.click(load_agent, inputs=[model1_input, model2_input], outputs=[model1_name, model1_score_output, model2_name, model2_score_output])
106
 
107
+ # examples = gr.Examples(examples=[["juliensimon/distilbert-amazon-shoe-reviews","juliensimon/distilbert-amazon-shoe-reviews"]],
108
+ # inputs=[model1_input, model2_input])
 
109
 
110
 
111
  app.launch()