lalital commited on
Commit
574b23b
·
1 Parent(s): ce4f669

feat: update visualization

Browse files
Files changed (1) hide show
  1. app.py +46 -13
app.py CHANGED
@@ -1,14 +1,13 @@
 
1
  import json
2
  from functools import partial
3
- from typing import Callable, Dict
4
  import transformers
5
  from transformers import (
6
  AutoModelForSequenceClassification,
7
  AutoTokenizer,
8
  pipeline
9
  )
10
-
11
-
12
  import pythainlp
13
  from pprint import pprint
14
  from itertools import chain
@@ -16,33 +15,67 @@ from itertools import chain
16
  import gradio as gr
17
 
18
 
19
- tokenizer=AutoTokenizer.from_pretrained(
20
  'airesearch/wangchanberta-base-att-spm-uncased',
21
- revision='finetuned@wisesight_sentiment-v1.1'
22
  )
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  #pipeline
25
  text_cls_pipeline = pipeline(task='sentiment-analysis',
26
- tokenizer=tokenizer,
27
- model = 'airesearch/wangchanberta-base-att-spm-uncased',
28
- revision ='finetuned@wisesight_sentiment-v1.1')
29
 
 
 
 
 
30
 
 
 
 
 
 
 
 
 
 
31
  def classify_text(text: str):
32
- results = text_cls_pipeline(text)
 
33
  print(f'results:\n {results}')
34
- html_text = results
35
-
 
 
 
36
  return json.dumps(results, ensure_ascii=False, indent=4), html_text
37
 
38
 
39
  demo = gr.Interface(fn=classify_text,
40
  inputs=gr.Textbox(lines=5, placeholder='Input text in Thai', label='Input text'),
41
  examples=[
42
- ['งานจากผกกคนนี้ไม่เคยทำให้เราผิดหวัง ให้ต้องไปดูอีกรอบสอง'],
43
  ['ฟอร์ด บุกตลาด อีวี ในอินเดีย #prachachat #ตลาดรถยนต์'],
44
  ['สั่งไป2 เมนู คือมัชฉะลาเต้ร้อน กับ ไอศครีมชาเขียว มัชฉะลาเต้ร้อน รสชาเขียวเข้มข้น หอม มัน แต่ไม่กลมกล่อม มันจืดแบบจืดสนิท ส่วนไอศครีมชาเขียว ทานแล้วรสมันออกใบไม้ๆมากกว่าชาเขียว แล้วก็หวานไป โดยรวมแล้วเฉยมากก ดีแค่รสชาเขียวเข้ม มีน้ำเปล่าบริการฟรี'],
45
- ['ไม่ได้เรื่องเลย การบริการให้ 5 เต็ม ร้อย']
46
  ],
47
 
48
  outputs=[gr.Textbox(), gr.HTML()])
 
1
+ from cProfile import label
2
  import json
3
  from functools import partial
4
+ from typing import Callable, Dict, List
5
  import transformers
6
  from transformers import (
7
  AutoModelForSequenceClassification,
8
  AutoTokenizer,
9
  pipeline
10
  )
 
 
11
  import pythainlp
12
  from pprint import pprint
13
  from itertools import chain
 
15
  import gradio as gr
16
 
17
 
18
+ tokenizer = AutoTokenizer.from_pretrained(
19
  'airesearch/wangchanberta-base-att-spm-uncased',
20
+ # revision='finetuned@wisesight_sentiment-v1.1'
21
  )
22
 
23
+ model = AutoModelForSequenceClassification.from_pretrained(
24
+ 'airesearch/wangchanberta-base-att-spm-uncased',
25
+ revision='finetuned@wisesight_sentiment-v1.1',
26
+ )
27
+ model.config.return_all_scores = True
28
+ LABEL_MAPPING = {
29
+ 'pos': '🤗 Positive:',
30
+ 'neu': '😐 Neutral:',
31
+ 'neg': '😡 Negative:',
32
+ 'q': '🤔 Quesiton:',
33
+ }
34
+ CSS_PROGRESS_BAR_MAPPING = {
35
+ 'pos':'w3-green',
36
+ 'neu': 'w3-light-blue',
37
+ 'neg': 'w3-red',
38
+ 'q': 'w3-blue',
39
+ }
40
+ LABEL_MAPPING_REVERSED = {v:k for k,v in LABEL_MAPPING.items() }
41
  #pipeline
42
  text_cls_pipeline = pipeline(task='sentiment-analysis',
43
+ tokenizer=tokenizer,
44
+ model=model)
 
45
 
46
+ css_text = """<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">"""
47
+ def render_html(items: List[Dict]):
48
+ html_text = ''
49
+ for item in items:
50
 
51
+ label, score = item['label'], item['score']
52
+ label_id = LABEL_MAPPING_REVERSED[label]
53
+
54
+ progress_bar_class_text = CSS_PROGRESS_BAR_MAPPING[label_id]
55
+
56
+ html_text += f'<span>{label.replace(" ", "&nbsp;")}&nbsp;&nbsp;{(score*100):8.2f}%<span>' + \
57
+ f'<div class="w3-light-grey w3-round"><div class="{progress_bar_class_text} w3-round" style="height:19px;width:{round(score*100,2)}%"></div></div><div style="height:8px;"></div>'
58
+
59
+ return '<div class="w3-container">' + html_text + '</div>'
60
  def classify_text(text: str):
61
+ text = text.replace(' ', '<_>')
62
+ results = text_cls_pipeline(text)[0]
63
  print(f'results:\n {results}')
64
+ for i, result in enumerate(results):
65
+ results[i]['label'] = LABEL_MAPPING[result['label']]
66
+ results[i]['score'] = float(round(float(result['score']), 4))
67
+ html_text = css_text + render_html(results)
68
+ print(html_text)
69
  return json.dumps(results, ensure_ascii=False, indent=4), html_text
70
 
71
 
72
  demo = gr.Interface(fn=classify_text,
73
  inputs=gr.Textbox(lines=5, placeholder='Input text in Thai', label='Input text'),
74
  examples=[
75
+ ['งานจากผกก. คนนี้ไม่เคยทำให้เราผิดหวัง ต้องหาเวลาไปดูรอบสอง'],
76
  ['ฟอร์ด บุกตลาด อีวี ในอินเดีย #prachachat #ตลาดรถยนต์'],
77
  ['สั่งไป2 เมนู คือมัชฉะลาเต้ร้อน กับ ไอศครีมชาเขียว มัชฉะลาเต้ร้อน รสชาเขียวเข้มข้น หอม มัน แต่ไม่กลมกล่อม มันจืดแบบจืดสนิท ส่วนไอศครีมชาเขียว ทานแล้วรสมันออกใบไม้ๆมากกว่าชาเขียว แล้วก็หวานไป โดยรวมแล้วเฉยมากก ดีแค่รสชาเขียวเข้ม มีน้ำเปล่าบริการฟรี'],
78
+ ['สาขานี้มีลิปของ Etude ไหมอ่าคะ ']
79
  ],
80
 
81
  outputs=[gr.Textbox(), gr.HTML()])