apak commited on
Commit
f44c16c
·
verified ·
1 Parent(s): ffccf87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -5
app.py CHANGED
@@ -90,9 +90,121 @@ def refine_label_with_llm(entity_text, wiki_context, custom_label_definitions):
90
 
91
  return final_label, cog_output
92
 
93
- # --- Wikipedia ve Geri Kalan Fonksiyonlar ---
94
- # (get_wiki_summary, advanced_ner_pipeline, process_ner_request fonksiyonları aynen kalabilir)
95
- # Sadece advanced_ner_pipeline içinde refine_label_with_llm'i çağırdığın yer
96
- # artık @spaces.GPU üzerinden otomatik GPU tahsisli çalışacak.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- # ... (Gradio arayüz kodların) ...
 
 
 
 
90
 
91
  return final_label, cog_output
92
 
93
+ # --- NER Pipeline Fonksiyonu (RAG KISITLAMASI İLE) ---
94
+ def advanced_ner_pipeline(text, target_labels, progress=gr.Progress()):
95
+ """
96
+ Standart NER modelini çalıştırır. RAG/LLM süreci SADECE MISC etiketleri için çalışır.
97
+ """
98
+ log_messages = list(_initial_logs)
99
+
100
+ try:
101
+ initial_results = ner_pipe(text)
102
+ except Exception as e:
103
+ log_messages.append(f"❌ Hata: Temel NER modelinde sorun oluştu: {e}")
104
+ yield log_messages, []
105
+ return
106
+
107
+ final_results = []
108
+ total_entities = len(initial_results)
109
+
110
+ log_messages.append(f"🕵️‍♀️ Toplam {total_entities} varlık inceleniyor...")
111
+ yield log_messages, None
112
+
113
+ STANDARD_LABELS = ["PER", "LOC", "ORG"]
114
+
115
+ for i, entity in enumerate(initial_results):
116
+ word = entity['word']
117
+ label = entity['entity_group']
118
+ score = entity['score']
119
+
120
+ current_progress = (i + 1) / total_entities
121
+ progress(current_progress, desc=f"İşleniyor: {word}")
122
+
123
+ result_obj = {
124
+ "entity": word,
125
+ "initial_label": label,
126
+ "initial_score": score,
127
+ "final_label": label,
128
+ "source": "Model",
129
+ "reasoning": ""
130
+ }
131
+
132
+ # RAG KARAR NOKTASI: YALNIZCA MISC VE LLM AÇIKSA
133
+ if label == "MISC" and IS_LLM_ENABLED:
134
+ standardized_word = word.title()
135
+ log_messages.append(f" ⚠️ MISC tespit edildi: '{word}'. Wiki'ye soruluyor...")
136
+ yield log_messages, None
137
+
138
+ wiki_context = get_wiki_summary(standardized_word)
139
+
140
+ if wiki_context:
141
+ log_messages.append(f" 📄 Wiki Bağlamı bulundu. LLM (GPU) devreye giriyor...")
142
+ yield log_messages, None
143
+
144
+ # Bu fonksiyon @spaces.GPU ile işaretlendiği için burada GPU tahsis edilir
145
+ new_label, reasoning = refine_label_with_llm(standardized_word, wiki_context, target_labels)
146
+
147
+ result_obj["final_label"] = new_label
148
+ result_obj["source"] = "RAG+LLM (CoT)"
149
+ result_obj["reasoning"] = reasoning
150
+
151
+ log_messages.append(f" 🔄 Güncellendi: {new_label}")
152
+ yield log_messages, None
153
+ else:
154
+ log_messages.append(" ❌ Wiki'de bilgi bulunamadı.")
155
+ yield log_messages, None
156
+
157
+ elif label in STANDARD_LABELS:
158
+ log_messages.append(f" ➡️ {word}: {label} (Standart)")
159
+ yield log_messages, None
160
+
161
+ final_results.append(result_obj)
162
+
163
+ log_messages.append("✅ İşlem tamamlandı!")
164
+ yield log_messages, final_results
165
+
166
+ # --- Özel Etiket Tanımları ---
167
+ custom_label_definitions = {
168
+ "PER": "Kişi adları.", "ORG": "Kurumlar.", "LOC": "Yerler.",
169
+ "DATE": "Tarihler.", "NORP": "Milliyetler/Gruplar.", "BOOK": "Kitaplar.",
170
+ "LANGUAGE": "Diller.", "TITLE": "Unvanlar.", "PRODUCT": "Ürünler.",
171
+ "SOFTWARE": "Yazılımlar.", "MISC": "Diğer."
172
+ }
173
+
174
+ # --- Gradio Arayüz Fonksiyonu ---
175
+ def process_ner_request(text, progress=gr.Progress()):
176
+ all_logs = []
177
+ final_results = None
178
+
179
+ for logs_step, results_step in advanced_ner_pipeline(text, custom_label_definitions, progress=progress):
180
+ all_logs = logs_step
181
+ if results_step is not None:
182
+ final_results = results_step
183
+
184
+ # Logları HTML'e çevirme
185
+ log_output_html = "<div style='height: 200px; overflow-y: scroll; background: #f9f9f9; padding: 10px; border-radius: 8px; font-family: monospace;'>"
186
+ for log in all_logs:
187
+ log_output_html += f"<p style='margin: 0;'>{log}</p>"
188
+ log_output_html += "</div>"
189
+
190
+ # Tabloyu oluşturma (Kısaltılmış HTML yapısı)
191
+ current_results_html = "<table><tr><th>Varlık</th><th>Etiket</th><th>Kaynak</th></tr>"
192
+ if final_results:
193
+ for item in final_results:
194
+ current_results_html += f"<tr><td>{item['entity']}</td><td>{item['final_label']}</td><td>{item['source']}</td></tr>"
195
+ current_results_html += "</table>"
196
+
197
+ yield log_output_html, current_results_html
198
+
199
+ # --- Arayüzü Başlat ---
200
+ iface = gr.Interface(
201
+ fn=process_ner_request,
202
+ inputs=gr.Textbox(lines=5, label="Metin giriniz..."),
203
+ outputs=[gr.HTML(label="Sistem Logları"), gr.HTML(label="NER Sonuçları")],
204
+ title="Hibrit NER Sistemi (CPU + ZeroGPU)"
205
+ )
206
 
207
+ if __name__ == "__main__":
208
+ port = int(os.environ.get('PORT', 7860))
209
+ print(f"🚀 Uygulama {port} portunda başlatılıyor...")
210
+ iface.launch(server_name="0.0.0.0", server_port=port)