Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,5 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import spaces
|
| 4 |
-
import fitz
|
| 5 |
-
import docx
|
| 6 |
-
from huggingface_hub import InferenceClient
|
| 7 |
-
|
| 8 |
-
# 1. 引擎配置:DeepSeek-V3.2-Exp (685B 逻辑核弹)
|
| 9 |
-
model_id = "deepseek-ai/DeepSeek-V3.2-Exp"
|
| 10 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
-
|
| 12 |
-
client = InferenceClient(
|
| 13 |
-
model=model_id,
|
| 14 |
-
token=hf_token,
|
| 15 |
-
headers={"Authorization": f"Bearer {hf_token}"} if hf_token else None
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
def extract_text(file_path):
|
| 19 |
-
if not file_path: return ""
|
| 20 |
-
try:
|
| 21 |
-
ext = str(file_path).lower()
|
| 22 |
-
if ext.endswith(".pdf"):
|
| 23 |
-
doc = fitz.open(file_path)
|
| 24 |
-
text = "".join([page.get_text() for page in doc])
|
| 25 |
-
doc.close()
|
| 26 |
-
return text[:15000]
|
| 27 |
-
elif ext.endswith(".docx"):
|
| 28 |
-
doc = docx.Document(file_path)
|
| 29 |
-
return "\n".join([p.text for p in doc.paragraphs])[:15000]
|
| 30 |
-
elif ext.endswith(".txt"):
|
| 31 |
-
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 32 |
-
return f.read()[:15000]
|
| 33 |
-
return ""
|
| 34 |
-
except:
|
| 35 |
-
return ""
|
| 36 |
-
|
| 37 |
@spaces.GPU(duration=60)
|
| 38 |
def chat_fn(message, history):
|
| 39 |
-
# 清洗历史记录,适配 DeepSeek 的严苛格式
|
| 40 |
clean_messages = []
|
| 41 |
for turn in history:
|
| 42 |
clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
|
|
@@ -60,26 +23,19 @@ def chat_fn(message, history):
|
|
| 60 |
if hasattr(msg, 'choices') and len(msg.choices) > 0:
|
| 61 |
delta = msg.choices[0].delta
|
| 62 |
if hasattr(delta, 'content') and delta.content:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
yield response
|
|
|
|
| 65 |
|
| 66 |
if response:
|
| 67 |
yield response + "\n\n---\n> **OpenIPOS MegaNode | DeepSeek-V3.2-Exp 逻辑推理出口**"
|
| 68 |
except Exception as e:
|
| 69 |
-
yield f"⚠️ 算力调度提示: {str(e)}。由于 DeepSeek 节点极其繁忙,请刷新重试。"
|
| 70 |
-
|
| 71 |
-
# 3. UI 装修
|
| 72 |
-
with gr.Blocks(fill_height=True) as demo:
|
| 73 |
-
gr.HTML("""
|
| 74 |
-
<div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
|
| 75 |
-
<img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
|
| 76 |
-
<div>
|
| 77 |
-
<h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
|
| 78 |
-
<p style="margin: 0; color: #666;">全球领先法律与逻辑推理节点 (DeepSeek-V3.2-Exp)</p>
|
| 79 |
-
</div>
|
| 80 |
-
</div>
|
| 81 |
-
""")
|
| 82 |
-
gr.ChatInterface(chat_fn, multimodal=True)
|
| 83 |
-
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Novita API Cluster")
|
| 84 |
-
|
| 85 |
-
demo.launch(theme=gr.themes.Soft())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
@spaces.GPU(duration=60)
|
| 2 |
def chat_fn(message, history):
|
|
|
|
| 3 |
clean_messages = []
|
| 4 |
for turn in history:
|
| 5 |
clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
|
|
|
|
| 23 |
if hasattr(msg, 'choices') and len(msg.choices) > 0:
|
| 24 |
delta = msg.choices[0].delta
|
| 25 |
if hasattr(delta, 'content') and delta.content:
|
| 26 |
+
# --- 核心修复逻辑开始 ---
|
| 27 |
+
content = delta.content
|
| 28 |
+
# 如果返回的是列表格式的字符串,尝试清洗它
|
| 29 |
+
if isinstance(content, list) and len(content) > 0:
|
| 30 |
+
content = content[0].get("text", "")
|
| 31 |
+
elif isinstance(content, dict):
|
| 32 |
+
content = content.get("text", "")
|
| 33 |
+
|
| 34 |
+
response += str(content)
|
| 35 |
yield response
|
| 36 |
+
# --- 核心修复逻辑结束 ---
|
| 37 |
|
| 38 |
if response:
|
| 39 |
yield response + "\n\n---\n> **OpenIPOS MegaNode | DeepSeek-V3.2-Exp 逻辑推理出口**"
|
| 40 |
except Exception as e:
|
| 41 |
+
yield f"⚠️ 算力调度提示: {str(e)}。由于 DeepSeek 节点极其繁忙,请刷新重试。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|