Upload 3 files
Browse files- app.py +16 -0
- appthunghiem.py +43 -0
- thunghiemxuly.py +101 -0
app.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from Model.NER.app_NER import show_ner
|
| 3 |
+
from Model.MultimodelNER.app_MNER import show_mner
|
| 4 |
+
|
| 5 |
+
title = "Vietnamese Multimodel NER"
|
| 6 |
+
|
| 7 |
+
st.title(title)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
st.sidebar.title('Model')
|
| 11 |
+
page = st.sidebar.selectbox("NER or Multimodel NER", ("NER", "Multimodel NER"))
|
| 12 |
+
|
| 13 |
+
if page == "NER":
|
| 14 |
+
show_ner()
|
| 15 |
+
else:
|
| 16 |
+
show_mner()
|
appthunghiem.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
# Title
|
| 4 |
+
st.title("Vietnamese Multimodel NER")
|
| 5 |
+
def save_uploaded_image(image, directory):
|
| 6 |
+
if not os.path.exists(directory):
|
| 7 |
+
os.makedirs(directory)
|
| 8 |
+
file_path = os.path.join(directory, image.name)
|
| 9 |
+
with open(file_path, "wb") as f:
|
| 10 |
+
f.write(image.getbuffer())
|
| 11 |
+
return file_path
|
| 12 |
+
|
| 13 |
+
# Sidebar for selection
|
| 14 |
+
st.sidebar.title('Selection')
|
| 15 |
+
page = st.sidebar.selectbox("Choose a page", ["NER", "Multimodal NER"])
|
| 16 |
+
|
| 17 |
+
# NER page
|
| 18 |
+
if page == "NER":
|
| 19 |
+
st.header("NER")
|
| 20 |
+
text = st.text_area("Enter your text for NER:", height=300)
|
| 21 |
+
if st.button("Process NER"):
|
| 22 |
+
st.write("Processing text with NER model...")
|
| 23 |
+
# Add your NER processing code here
|
| 24 |
+
st.write(f"Input text: {text}")
|
| 25 |
+
|
| 26 |
+
# Multimodal NER page
|
| 27 |
+
elif page == "Multimodal NER":
|
| 28 |
+
st.header("Multimodal NER")
|
| 29 |
+
text = st.text_area("Enter your text for Multimodal NER:", height=300)
|
| 30 |
+
image = st.file_uploader("Upload an image:", type=["png", "jpg", "jpeg"])
|
| 31 |
+
if st.button("Process Multimodal NER"):
|
| 32 |
+
st.write("Processing text and image with Multimodal NER model...")
|
| 33 |
+
# Add your Multimodal NER processing code here
|
| 34 |
+
st.write(f"Input text: {text}")
|
| 35 |
+
if image:
|
| 36 |
+
save_path='E:/demo_datn/pythonProject1/Model/MultimodelNER/VLSP2016/Image'
|
| 37 |
+
image_name = image.name
|
| 38 |
+
print(image_name)
|
| 39 |
+
saved_image_path = save_uploaded_image(image, save_path)
|
| 40 |
+
|
| 41 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 42 |
+
else:
|
| 43 |
+
st.write("No image uploaded.")
|
thunghiemxuly.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import os
|
| 3 |
+
from Model.NER.VLSP2021.Predict_Ner import ViTagger,normalize_text
|
| 4 |
+
def process_text(text):
|
| 5 |
+
# Loại bỏ dấu cách thừa và dấu cách ở đầu và cuối văn bản
|
| 6 |
+
processed_text = re.sub(r'\s+', ' ', text.strip())
|
| 7 |
+
return processed_text
|
| 8 |
+
|
| 9 |
+
# Sử dụng hàm process_text để xử lý văn bản
|
| 10 |
+
text = """
|
| 11 |
+
Trang Footballogue vừa đăng tải đoạn video được cho là quay ở phòng tập thể dục của CLB Al Nassr vào hôm 7/8. Trong đoạn video đó, C.Ronaldo vẫn miệt mài tập luyện một mình, dù cho cả đội đã ra về từ lâu.
|
| 12 |
+
|
| 13 |
+
Tờ báo này bình luận: "Khi tất cả các đồng đội ở Al Nassr ra về, C.Ronaldo vẫn miệt mài tập luyện. Kỷ luật của CR7 thật đáng ngưỡng mộ khi cầu thủ này đã có trong tay mọi thứ".
|
| 14 |
+
|
| 15 |
+
Trên trang Twitter, những người hâm mộ đã bày tỏ sự thán phục sự chăm chỉ và chuyên nghiệp của C.Ronaldo. Dưới đây là một vài dòng bình luận:
|
| 16 |
+
|
| 17 |
+
"C.Ronaldo là biểu tượng của sự tận hiến trong bóng đá".
|
| 18 |
+
|
| 19 |
+
"Ở tuổi 38, khi nhiều cầu thủ treo giày, C.Ronaldo vẫn miệt mài tập luyện. Bạn sẽ không tìm cầu thủ thứ hai trong lịch sử như vậy".
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# processed_text = process_text(text)
|
| 23 |
+
# print(processed_text)
|
| 24 |
+
|
| 25 |
+
LABEL2ID_VLSP2021 = ['O', 'LOCATION-GPE', 'QUANTITY-NUM', 'EVENT-CUL', 'DATETIME', 'PERSONTYPE', 'PERSON', 'QUANTITY-PER', 'ORGANIZATION', 'LOCATION-GEO', 'LOCATION-STRUC', 'PRODUCT-COM', 'DATETIME-DATE', 'QUANTITY-DIM', 'PRODUCT', 'QUANTITY', 'DATETIME-DURATION', 'PERSON', 'QUANTITY-CUR', 'DATETIME-TIME', 'QUANTITY-TEM', 'DATETIME-TIMERANGE', 'EVENT-GAMESHOW', 'QUANTITY-AGE', 'QUANTITY-ORD', 'PRODUCT-LEGAL', 'PERSONTYPE', 'LOCATION', 'ORGANIZATION-MED', 'URL', 'PHONENUMBER', 'ORGANIZATION-SPORTS', 'EVENT-SPORT', 'SKILL', 'EVENT-NATURAL', 'ADDRESS', 'IP', 'EMAIL', 'ORGANIZATION-STOCK', 'DATETIME-SET', 'PRODUCT-AWARD', 'MISCELLANEOUS', 'LOCATION-GPE-GEO']
|
| 26 |
+
# print(len(LABEL2ID_VLSP2021))
|
| 27 |
+
|
| 28 |
+
def save_uploaded_image(image, directory):
|
| 29 |
+
if not os.path.exists(directory):
|
| 30 |
+
os.makedirs(directory)
|
| 31 |
+
file_path = os.path.join(directory, image.name)
|
| 32 |
+
with open(file_path, "wb") as f:
|
| 33 |
+
f.write(image.getbuffer())
|
| 34 |
+
# def convert_text_to_txt(text,file_path):
|
| 35 |
+
# # Gộp các dòng văn bản thành một đoạn văn
|
| 36 |
+
# paragraph = text.replace('\n', ' ')
|
| 37 |
+
#
|
| 38 |
+
# # Sử dụng biểu thức chính quy để tách từ và dấu câu
|
| 39 |
+
# words_list = re.findall(r'\w+|[.,]', paragraph)
|
| 40 |
+
# with open(file_path, 'w', encoding='utf-8') as file:
|
| 41 |
+
# for word in words_list:
|
| 42 |
+
# file.write(word + '\n')
|
| 43 |
+
# return words_list
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# # Văn bản mẫu
|
| 48 |
+
# text = """Toi ten la Minh"""
|
| 49 |
+
# # Sử dụng hàm để chuyển đổi văn bản
|
| 50 |
+
# sa='E:/demo_datn/pythonProject1/Model/MultimodelNER/VLSP2016/list.txt'
|
| 51 |
+
# convert_text_to_txt(text ,sa)
|
| 52 |
+
|
| 53 |
+
def add_string_to_txt(string, file_path):
|
| 54 |
+
# Đọc dữ liệu từ tệp
|
| 55 |
+
file_name = string.split('.')[0]
|
| 56 |
+
|
| 57 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 58 |
+
lines = file.readlines()
|
| 59 |
+
|
| 60 |
+
# Thêm chuỗi vào dòng đầu tiên
|
| 61 |
+
lines.insert(0, f"IMGID:{file_name}\n")
|
| 62 |
+
|
| 63 |
+
# Ghi lại dữ liệu vào tệp
|
| 64 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 65 |
+
file.writelines(lines)
|
| 66 |
+
|
| 67 |
+
# string= 'namngo.jpg'
|
| 68 |
+
# add_string_to_txt(string, sa)
|
| 69 |
+
# # In kết quả
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
import os
|
| 73 |
+
import re
|
| 74 |
+
|
| 75 |
+
def convert_text_to_txt(text, file_path):
|
| 76 |
+
# Merge lines of text into a paragraph
|
| 77 |
+
paragraph = text.replace('\n', ' ')
|
| 78 |
+
|
| 79 |
+
# Use regular expression to separate words and punctuation marks
|
| 80 |
+
words_list = re.findall(r'\w+|[.,]', paragraph)
|
| 81 |
+
|
| 82 |
+
# Ensure the directory exists
|
| 83 |
+
directory = os.path.dirname(file_path)
|
| 84 |
+
if not os.path.exists(directory):
|
| 85 |
+
os.makedirs(directory)
|
| 86 |
+
|
| 87 |
+
# Write words to the file
|
| 88 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 89 |
+
for word in words_list:
|
| 90 |
+
file.write(word + '\n')
|
| 91 |
+
|
| 92 |
+
return words_list
|
| 93 |
+
|
| 94 |
+
# Example usage
|
| 95 |
+
# text = "This is some example text."
|
| 96 |
+
# output_file_path = 'E:/demo_datn/pythonProject1/Model/MultimodelNER/VLSP2016/Filetxt/output.txt'
|
| 97 |
+
# convert_text_to_txt(text, output_file_path)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|