Nucha commited on
Commit
94327b4
·
verified ·
1 Parent(s): 616f9f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -27
app.py CHANGED
@@ -1,18 +1,12 @@
1
- import os
2
- os.system("python -m spacy download en_core_web_sm")
3
-
4
  import streamlit as st
5
  from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
6
- import spacy
7
- from spacy import displacy
8
 
9
- # ตรวจสอบว่ามีการติดตั้งโมเดล spacy หรือไม่
10
- try:
11
- nlp = spacy.load("en_core_web_sm")
12
- except OSError:
13
- import os
14
- os.system("python -m spacy download en_core_web_sm")
15
- nlp = spacy.load("en_core_web_sm")
16
 
17
  # โหลดโมเดล NER จาก Hugging Face
18
  model_name = "Nucha/Nucha_SkillNER_BERT"
@@ -23,23 +17,17 @@ model = AutoModelForTokenClassification.from_pretrained(model_name)
23
  ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
24
 
25
  # UI ด้วย Streamlit
26
- st.title("NER Analysis with Nucha SkillNER BERT and spacy displacy")
27
 
28
  text = st.text_area("Enter text for NER analysis:")
29
 
30
  if st.button("Analyze"):
31
  ner_results = ner_pipeline(text)
32
-
33
- # เตรียมข้อมูลสำหรับ displacy
34
- ents = []
35
- for entity in ner_results:
36
- ents.append({
37
- "start": entity['start'],
38
- "end": entity['end'],
39
- "label": entity['entity'],
40
- })
41
-
42
- # แสดงผล displacy ผ่าน Streamlit
43
- options = {"colors": {"SKILL": "lightblue"}} # เพิ่มสีให้แต่ละ label ถ้าต้องการ
44
- html = displacy.render({"text": text, "ents": ents}, style="ent", manual=True, options=options)
45
- st.write(html, unsafe_allow_html=True)
 
 
 
 
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
3
+ from sparknlp_display import Display
4
+ from pyspark.sql import SparkSession
5
 
6
+ # สร้าง Spark Session
7
+ spark = SparkSession.builder \
8
+ .appName("NER Analysis") \
9
+ .getOrCreate()
 
 
 
10
 
11
  # โหลดโมเดล NER จาก Hugging Face
12
  model_name = "Nucha/Nucha_SkillNER_BERT"
 
17
  ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
18
 
19
  # UI ด้วย Streamlit
20
+ st.title("NER Analysis with Nucha SkillNER BERT and Spark NLP Display")
21
 
22
  text = st.text_area("Enter text for NER analysis:")
23
 
24
  if st.button("Analyze"):
25
  ner_results = ner_pipeline(text)
26
+
27
+ # สร้าง DataFrame สำหรับผลลัพธ์
28
+ data = [{"word": entity['word'], "start": entity['start'], "end": entity['end'], "label": entity['entity']} for entity in ner_results]
29
+ ner_df = spark.createDataFrame(data)
30
+
31
+ # แสดงผลด้วย sparknlp_display
32
+ display = Display()
33
+ st.write(display.display(ner_df, "word", "label"))