Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,73 +9,72 @@ from huggingface_hub import InferenceClient
|
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
| 10 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
|
| 11 |
|
| 12 |
-
# 2. 全能文档解析引擎
|
| 13 |
def extract_text_from_file(file_path):
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
| 16 |
doc = fitz.open(file_path)
|
| 17 |
return "".join([page.get_text() for page in doc])
|
| 18 |
-
elif
|
| 19 |
doc = docx.Document(file_path)
|
| 20 |
return "\n".join([para.text for para in doc.paragraphs])
|
|
|
|
|
|
|
| 21 |
else:
|
| 22 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 23 |
return f.read()
|
| 24 |
except Exception as e:
|
| 25 |
-
return f"[
|
| 26 |
|
| 27 |
@spaces.GPU(duration=120)
|
| 28 |
def chat_fn(message, history):
|
| 29 |
-
# 处理当前输入
|
| 30 |
files = message.get("files", [])
|
| 31 |
current_text = message.get("text", "")
|
| 32 |
|
| 33 |
if files:
|
| 34 |
file_path = files[0] if isinstance(files[0], str) else files[0].path
|
| 35 |
doc_content = extract_text_from_file(file_path)
|
| 36 |
-
# 截取前
|
| 37 |
-
user_payload = f"【
|
| 38 |
else:
|
| 39 |
user_payload = current_text
|
| 40 |
|
| 41 |
-
#
|
| 42 |
messages = []
|
| 43 |
for turn in history:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# 过滤掉空的 content 或非字符串内容
|
| 47 |
-
if turn["content"] and isinstance(turn["content"], str):
|
| 48 |
-
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 49 |
|
| 50 |
-
# 加入当前请求
|
| 51 |
messages.append({"role": "user", "content": user_payload})
|
| 52 |
|
| 53 |
response = ""
|
| 54 |
try:
|
| 55 |
-
#
|
| 56 |
for msg in client.chat_completion(
|
| 57 |
messages,
|
| 58 |
max_tokens=4096,
|
| 59 |
stream=True,
|
| 60 |
temperature=0.6
|
| 61 |
):
|
| 62 |
-
if
|
| 63 |
token = msg.choices[0].delta.content
|
| 64 |
if token:
|
| 65 |
response += token
|
| 66 |
yield response
|
| 67 |
|
| 68 |
-
# 注入
|
| 69 |
response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
|
| 70 |
yield response
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
with gr.Blocks(fill_height=True) as demo:
|
| 77 |
gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
|
| 78 |
-
gr.Markdown("### 适配 Gradio 6.0
|
| 79 |
|
| 80 |
gr.ChatInterface(chat_fn, multimodal=True)
|
| 81 |
|
|
|
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
| 10 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
|
| 11 |
|
| 12 |
+
# 2. 全能文档解析引擎 (增加格式容错)
|
| 13 |
def extract_text_from_file(file_path):
|
| 14 |
try:
|
| 15 |
+
suffix = file_path.lower()
|
| 16 |
+
if suffix.endswith(".pdf"):
|
| 17 |
doc = fitz.open(file_path)
|
| 18 |
return "".join([page.get_text() for page in doc])
|
| 19 |
+
elif suffix.endswith(".docx"):
|
| 20 |
doc = docx.Document(file_path)
|
| 21 |
return "\n".join([para.text for para in doc.paragraphs])
|
| 22 |
+
elif suffix.endswith(".doc"):
|
| 23 |
+
return "⚠️ 提示:系统暂不支持旧版 .doc 格式,请将其另存为 .docx 或 .pdf 后再次上传。"
|
| 24 |
else:
|
| 25 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 26 |
return f.read()
|
| 27 |
except Exception as e:
|
| 28 |
+
return f"[解析失败: {str(e)}]"
|
| 29 |
|
| 30 |
@spaces.GPU(duration=120)
|
| 31 |
def chat_fn(message, history):
|
|
|
|
| 32 |
files = message.get("files", [])
|
| 33 |
current_text = message.get("text", "")
|
| 34 |
|
| 35 |
if files:
|
| 36 |
file_path = files[0] if isinstance(files[0], str) else files[0].path
|
| 37 |
doc_content = extract_text_from_file(file_path)
|
| 38 |
+
# 严格控制输入长度:截取前 30,000 字以换取更高的翻译稳定性
|
| 39 |
+
user_payload = f"【文档内容参考】:\n{doc_content[:30000]}\n\n【用户指令】: {current_text}"
|
| 40 |
else:
|
| 41 |
user_payload = current_text
|
| 42 |
|
| 43 |
+
# 严谨的消息过滤
|
| 44 |
messages = []
|
| 45 |
for turn in history:
|
| 46 |
+
if isinstance(turn, dict) and turn.get("role") and turn.get("content"):
|
| 47 |
+
messages.append({"role": turn["role"], "content": turn["content"]})
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 49 |
messages.append({"role": "user", "content": user_payload})
|
| 50 |
|
| 51 |
response = ""
|
| 52 |
try:
|
| 53 |
+
# 增加 stream 稳定性处理
|
| 54 |
for msg in client.chat_completion(
|
| 55 |
messages,
|
| 56 |
max_tokens=4096,
|
| 57 |
stream=True,
|
| 58 |
temperature=0.6
|
| 59 |
):
|
| 60 |
+
if msg.choices:
|
| 61 |
token = msg.choices[0].delta.content
|
| 62 |
if token:
|
| 63 |
response += token
|
| 64 |
yield response
|
| 65 |
|
| 66 |
+
# 正常结束注入指纹
|
| 67 |
response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
|
| 68 |
yield response
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
+
# 针对超时的友好提示
|
| 72 |
+
yield f"{response}\n\n⚠️ OpenIPOS 调度异常 (可能是文档过长导致超时): {str(e)}\n提示:请尝试清空对话或分段上传文档。"
|
| 73 |
|
| 74 |
+
# 3. 极简 UI
|
| 75 |
with gr.Blocks(fill_height=True) as demo:
|
| 76 |
gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
|
| 77 |
+
gr.Markdown("### 适配 Gradio 6.0 | 支持 PDF/DOCX/TXT")
|
| 78 |
|
| 79 |
gr.ChatInterface(chat_fn, multimodal=True)
|
| 80 |
|