Spaces:
Running
Running
| import os | |
| import sys | |
| # 1. 強制下載 Spacy 模型 | |
| os.system(f"{sys.executable} -m spacy download en_core_web_sm") | |
| # 2. 解決相容性問題的補丁 | |
| try: | |
| import huggingface_hub | |
| if not hasattr(huggingface_hub, 'HfFolder'): | |
| class MockHfFolder: | |
| def get_token(): return os.getenv("HF_TOKEN") | |
| def save_token(token): pass | |
| huggingface_hub.HfFolder = MockHfFolder | |
| except: | |
| pass | |
| import gradio as gr | |
| from fastcoref import FCoref | |
| from deep_translator import GoogleTranslator | |
| # 關鍵修正:將 model_name 改為 model_name_or_path | |
| print("🚀 [System] 正在進行阻塞式權重同步(約 300MB)...") | |
| try: | |
| # 使用相容性最高的參數名稱 | |
| model = FCoref(model_name_or_path='biu-nlp/f-coref', device='cpu') | |
| print("✅ [System] 模型載入完成,介面即將開啟。") | |
| except TypeError: | |
| # 萬一連上面的都不行,就用最原始的 positional argument | |
| model = FCoref('biu-nlp/f-coref', device='cpu') | |
| print("✅ [System] 使用原始參數模式載入完成。") | |
| def coref_chat(user_input): | |
| if not user_input.strip(): | |
| return "請輸入內容", "等待輸入..." | |
| try: | |
| # 1. 偵測語系並統一轉換為英文供模型運算 | |
| has_chinese = any('\u4e00' <= char <= '\u9fff' for char in user_input) | |
| if has_chinese: | |
| working_text = GoogleTranslator(source='zh-CN', target='en').translate(user_input) | |
| mode_notice = "【模式:中文 ➔ 英文解析】" | |
| else: | |
| working_text = user_input | |
| mode_notice = "【模式:純英文解析】" | |
| # 2. 執行指代消解 | |
| preds = model.predict(texts=[working_text]) | |
| clusters = preds[0].get_clusters() | |
| # 3. 整理輸出格式 (嚴格按照你的要求排版) | |
| result = f"✨ {mode_notice}\n" | |
| result += f"📝 英文邏輯空間: {working_text}\n" | |
| # --- 英文翻譯中文 --- | |
| try: | |
| translation_back = GoogleTranslator(source='en', target='zh-TW').translate(working_text) | |
| result += f"📖 英翻中: {translation_back}\n" | |
| except: | |
| result += f"📖 英翻中: (翻譯暫時無法讀取)\n" | |
| result += "---------------------------------\n" | |
| if not clusters: | |
| result += "🔍 分析結果:指代關係明確,或模型判定關聯度未達門檻。" | |
| else: | |
| result += "🎯【偵測到之實體鏈 (Entity Chains)】:\n" | |
| for i, cluster in enumerate(clusters): | |
| cluster_str_en = ' ↔ '.join(cluster) | |
| # 實體鏈個別翻譯 | |
| try: | |
| translated_items = [GoogleTranslator(source='en', target='zh-TW').translate(item) for item in cluster] | |
| cluster_str_zh = ' ↔ '.join(translated_items) | |
| except: | |
| cluster_str_zh = "(鏈結翻譯失敗)" | |
| result += f" 🔗 鏈結 {i+1} (繁中): {cluster_str_zh}\n" | |
| result += f" └─ (原文): {cluster_str_en}\n" | |
| return user_input, result | |
| except Exception as e: | |
| return user_input, f"運行錯誤: {str(e)}" | |
| # 介面設定 | |
| demo = gr.Interface( | |
| fn=coref_chat, | |
| inputs=gr.Textbox(label="輸入文本", lines=3, placeholder="輸入中文或英文段落..."), | |
| outputs=[gr.Textbox(label="原始輸入"), gr.Textbox(label="AI 語意分析報告")], | |
| title="AI 跨語言指代消解系統 (Stable Version)" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |