lalital commited on
Commit
0e3bb07
·
verified ·
1 Parent(s): 18f574d

Update app.py

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