mdj1412 commited on
Commit
6671a55
1 Parent(s): 9d95b50

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -29
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import gradio as gr
2
- import torch
3
- import os
4
- from huggingface_hub import login
5
- from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
6
- from transformers import AutoTokenizer
7
 
8
  README = """
9
  # Movie Review Score Discriminator
@@ -19,8 +17,6 @@ learning_rate = 5e-5
19
  batch_size_train = 64
20
  step = 1900
21
 
22
- file_name = "model-{}.pt".format(step)
23
- state_dict = torch.load(os.path.join(file_name))
24
 
25
  id2label = {0: "NEGATIVE", 1: "POSITIVE"}
26
  label2id = {"NEGATIVE": 0, "POSITIVE": 1}
@@ -28,34 +24,20 @@ label2id = {"NEGATIVE": 0, "POSITIVE": 1}
28
 
29
  title = "Movie Review Score Discriminator"
30
  description = "It is a program that classifies whether it is positive or negative by entering movie reviews. You can choose between the Korean version and the English version."
31
- examples = ["the greatest musicians ", "cold movie "]
32
-
33
 
34
 
35
- def tokenized_data(tokenizer, inputs):
36
- return tokenizer.batch_encode_plus(
37
- inputs,
38
- return_tensors="pt",
39
- padding="max_length",
40
- max_length=64,
41
- truncation=True)
42
 
43
 
44
- def greet(text):
45
- tokenizer = AutoTokenizer.from_pretrained(model_name)
46
- model = AutoModelForSequenceClassification.from_pretrained(
47
- model_name, num_labels=2, id2label=id2label, label2id=label2id,
48
- state_dict=state_dict
49
- )
50
- inputs = tokenized_data(tokenizer, text)
51
 
52
- model.eval()
 
53
 
54
- with torch.no_grad():
55
- # logits.shape = torch.Size([ batch_size, 2 ])
56
- logits = model(input_ids=inputs[0], attention_mask=inputs[1]).logits
57
-
58
- return logits
59
 
60
  demo1 = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment", inputs="text", outputs="text",
61
  title=title, theme="peach",
@@ -68,6 +50,15 @@ demo1 = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment", in
68
  # allow_flagging="auto",
69
  # description=description, examples=examples)
70
 
 
 
 
 
 
 
 
 
 
71
 
72
  demo3 = gr.Interface.load("models/mdj1412/movie_review_score_discriminator_eng", inputs="text", outputs="text",
73
  title=title, theme="peach",
@@ -75,4 +66,5 @@ demo3 = gr.Interface.load("models/mdj1412/movie_review_score_discriminator_eng",
75
  description=description, examples=examples)
76
 
77
  if __name__ == "__main__":
 
78
  demo3.launch()
 
1
  import gradio as gr
2
+ from datasets import load_dataset
3
+ import random
4
+
 
 
5
 
6
  README = """
7
  # Movie Review Score Discriminator
 
17
  batch_size_train = 64
18
  step = 1900
19
 
 
 
20
 
21
  id2label = {0: "NEGATIVE", 1: "POSITIVE"}
22
  label2id = {"NEGATIVE": 0, "POSITIVE": 1}
 
24
 
25
  title = "Movie Review Score Discriminator"
26
  description = "It is a program that classifies whether it is positive or negative by entering movie reviews. You can choose between the Korean version and the English version."
 
 
27
 
28
 
29
+ imdb_dataset = load_dataset('imdb')
30
+ examples = []
31
+ # examples = ["the greatest musicians ", "cold movie "]
32
+ for i in range(3):
33
+ idx = random.randrange(len(imdb_dataset['train']))
34
+ examples.append(imdb_dataset['train'][idx]['text'])
 
35
 
36
 
 
 
 
 
 
 
 
37
 
38
+ def fn(text):
39
+ return "hello, " + text
40
 
 
 
 
 
 
41
 
42
  demo1 = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment", inputs="text", outputs="text",
43
  title=title, theme="peach",
 
50
  # allow_flagging="auto",
51
  # description=description, examples=examples)
52
 
53
+ here = gr.Interface(fn,
54
+ inputs= gr.inputs.Textbox( lines=1, placeholder=None, default="", label=None),
55
+ outputs='text',
56
+ title="Sentiment analysis of movie reviews",
57
+ description=description,
58
+ theme="peach",
59
+ allow_flagging="auto",
60
+ flagging_dir='flagging records')
61
+
62
 
63
  demo3 = gr.Interface.load("models/mdj1412/movie_review_score_discriminator_eng", inputs="text", outputs="text",
64
  title=title, theme="peach",
 
66
  description=description, examples=examples)
67
 
68
  if __name__ == "__main__":
69
+ # here.launch()
70
  demo3.launch()