Asanaly commited on
Commit
db3da85
·
verified ·
1 Parent(s): 447e924

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -36,38 +36,42 @@ def predict_ner(text):
36
  return results
37
 
38
  # ============================
39
- # 3. Түсті HTML шығару
40
  # ============================
41
- def highlight_ner(text):
42
  entities = predict_ner(text)
43
  if not entities:
44
  return "Атаулар табылған жоқ"
45
-
46
- colored_text = text
47
  for word, label in entities:
48
- color = ""
49
- if label.startswith("PER"):
50
- color = "red"
51
- elif label.startswith("ORG"):
52
- color = "blue"
53
- elif label.startswith("LOC"):
54
- color = "green"
55
- # Мәтіндегі сөзді HTML span тегпен орау
56
- colored_text = colored_text.replace(word, f"<span style='color:{color}; font-weight:bold'>{word}</span>")
57
- return colored_text
 
 
 
58
 
59
  # ============================
60
  # 4. Gradio интерфейсі
61
  # ============================
62
  iface = gr.Interface(
63
- fn=highlight_ner,
64
  inputs=gr.Textbox(lines=10, placeholder="Қазақ мәтінін осында енгізіңіз..."),
65
- outputs=gr.HTML(label="Анықталған атаулар"),
66
  title="Қазақ тіліндегі NER",
67
- description="PER – адам, ORG – ұйым, LOC – орын. Атаулар мәтінде түспен ерекшеленеді."
68
  )
69
 
70
  # ============================
71
  # 5. Іске қосу
72
  # ============================
73
  iface.launch()
 
 
36
  return results
37
 
38
  # ============================
39
+ # 3. Атауларды текстпен шығару
40
  # ============================
41
+ def format_ner(text):
42
  entities = predict_ner(text)
43
  if not entities:
44
  return "Атаулар табылған жоқ"
45
+
46
+ output_dict = {"PER": [], "ORG": [], "LOC": []}
47
  for word, label in entities:
48
+ if label.endswith("PER"):
49
+ output_dict["PER"].append(word)
50
+ elif label.endswith("ORG"):
51
+ output_dict["ORG"].append(word)
52
+ elif label.endswith("LOC"):
53
+ output_dict["LOC"].append(word)
54
+
55
+ # Бір қатарға қосып шығару
56
+ output_text = ""
57
+ for key, words in output_dict.items():
58
+ if words:
59
+ output_text += f"{key}: {' '.join(words)}\n"
60
+ return output_text.strip()
61
 
62
  # ============================
63
  # 4. Gradio интерфейсі
64
  # ============================
65
  iface = gr.Interface(
66
+ fn=format_ner,
67
  inputs=gr.Textbox(lines=10, placeholder="Қазақ мәтінін осында енгізіңіз..."),
68
+ outputs=gr.Textbox(label="Анықталған атаулар"),
69
  title="Қазақ тіліндегі NER",
70
+ description="PER – адам, ORG – ұйым, LOC – орын. Атаулар қарапайым текстпен шығарылады."
71
  )
72
 
73
  # ============================
74
  # 5. Іске қосу
75
  # ============================
76
  iface.launch()
77
+