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