Spaces:
Sleeping
Sleeping
Asanaly commited on
Update app.py
Browse files35 unmodified lines363738394041424344454647484950515253545556575859606162636465666768697071727335 unmodified linesreturn results# ============================# 3. Түсті HTML шығару# ============================def highlight_ner(text):entities = predict_ner(text)if not entities:return "Атаулар табылған жоқ"colored_text = textfor word, label in entities:color = ""if label.startswith("PER"):color = "red"elif label.startswith("ORG"):color = "blue"elif label.startswith("LOC"):color = "green"# Мәтіндегі сөзді HTML span тегпен орауcolored_text = colored_text.replace(word, f"<span style='color:{color}; font-weight:bold'>{word}</span>")return colored_text# ============================# 4. Gradio интерфейсі# ============================iface = gr.Interface(fn=highlight_ner,inputs=gr.Textbox(lines=10, placeholder="Қазақ мәтінін осында енгізіңіз..."),outputs=gr.HTML(label="Анықталған атаулар"),title="Қазақ тіліндегі NER",description="PER – адам, ORG – ұйым, LOC – орын. Атаулар мәтінде түспен ерекшеленеді.")# ============================# 5. Іске қосу# ============================iface.launch()35 unmodified lines36373839404142434445464748495051525354555657585960616263646566676869707172737475767735 unmodified linesreturn results# ============================# 3. Атауларды текстпен шығару# ============================def format_ner(text):entities = predict_ner(text)if not entities:return "Атаулар табылған жоқ"output_dict = {"PER": [], "ORG": [], "LOC": []}for word, label in entities:if label.endswith("PER"):output_dict["PER"].append(word)elif label.endswith("ORG"):output_dict["ORG"].append(word)elif label.endswith("LOC"):output_dict["LOC"].append(word)# Бір қатарға қосып шығаруoutput_text = ""for key, words in output_dict.items():if words:output_text += f"{key}: {' '.join(words)}\n"return output_text.strip()# ============================# 4. Gradio интерфейсі# ============================iface = gr.Interface(fn=format_ner,inputs=gr.Textbox(lines=10, placeholder="Қазақ мәтінін осында енгізіңіз..."),outputs=gr.Textbox(label="Анықталған атаулар"),title="Қазақ тіліндегі NER",description="PER – адам, ORG – ұйым, LOC – орын. Атаулар қарапайым текстпен шығарылады.")# ============================# 5. Іске қосу# ============================iface.launch()
app.py+21-17