OpenIPOS commited on
Commit
5ebb5b8
·
verified ·
1 Parent(s): cfff901

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -5,15 +5,16 @@ import fitz
5
  import docx
6
  from huggingface_hub import InferenceClient
7
 
8
- # 1. 强制显式调用 Token
 
9
  hf_token = os.getenv("HF_TOKEN")
10
  model_id = "MiniMaxAI/MiniMax-M2.5"
11
 
12
- # 如果没有 Token,界面会直接提示
13
- if not hf_token:
14
- print("❌ 警告:未检测到 HF_TOKEN,请在 Settings -> Secrets 中添加")
15
-
16
- client = InferenceClient(model_id, token=hf_token)
17
 
18
  def extract_text(file_path):
19
  if not file_path: return ""
@@ -21,7 +22,9 @@ def extract_text(file_path):
21
  ext = str(file_path).lower()
22
  if ext.endswith(".pdf"):
23
  doc = fitz.open(file_path)
24
- return "".join([page.get_text() for page in doc])[:15000]
 
 
25
  elif ext.endswith(".docx"):
26
  doc = docx.Document(file_path)
27
  return "\n".join([p.text for p in doc.paragraphs])[:15000]
@@ -34,7 +37,7 @@ def extract_text(file_path):
34
 
35
  @spaces.GPU(duration=60)
36
  def chat_fn(message, history):
37
- # 彻底压扁历史记录为标准 API 格式
38
  clean_messages = []
39
  for turn in history:
40
  clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
@@ -47,41 +50,42 @@ def chat_fn(message, history):
47
  file_path = f.get("path") if isinstance(f, dict) else f
48
  context = extract_text(file_path)
49
  if context:
50
- prompt_text = f"【参考文档】:\n{context}\n\n【指令说明】: 请基于以上内容回答:{prompt_text}"
51
 
52
  clean_messages.append({"role": "user", "content": prompt_text})
53
 
54
  response = ""
55
  try:
56
- # 增加 headers 确保 Token 穿透
57
- for msg in client.chat_completion(
58
  clean_messages,
59
  max_tokens=2048,
60
  stream=True,
61
- temperature=0.7,
62
- headers={"Authorization": f"Bearer {hf_token}"} if hf_token else None
63
- ):
64
- if msg.choices[0].delta.content:
65
- response += msg.choices[0].delta.content
66
- yield response
 
 
 
67
 
68
  if response:
69
  yield response + "\n\n---\n> **OpenIPOS MegaNode | MiniMax-M2.5 极速逻辑节点**"
70
 
71
  except Exception as e:
72
- # 如果是权限问题,这里会给出明确提示
73
- err_msg = str(e)
74
- if "401" in err_msg or "token" in err_msg.lower():
75
- yield "❌ 授权失败:请检查 Space Settings 中的 HF_TOKEN 是否正确配置。"
76
- else:
77
- yield f"⚠️ 调度波动: {err_msg}。请尝试刷新重试。"
78
 
 
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;">
83
- <div><h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
84
- <p style="margin: 0; color: #666;">中文精品人工智能算力出口 (MiniMax-M2.5)</p></div>
 
 
85
  </div>
86
  """)
87
  gr.ChatInterface(chat_fn, multimodal=True)
 
5
  import docx
6
  from huggingface_hub import InferenceClient
7
 
8
+ # 1. 引擎配置与强制授权
9
+ # 关键修复:headers 在此处传入
10
  hf_token = os.getenv("HF_TOKEN")
11
  model_id = "MiniMaxAI/MiniMax-M2.5"
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 ""
 
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]
 
37
 
38
  @spaces.GPU(duration=60)
39
  def chat_fn(message, history):
40
+ # 彻底压扁历史记录
41
  clean_messages = []
42
  for turn in history:
43
  clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
 
50
  file_path = f.get("path") if isinstance(f, dict) else f
51
  context = extract_text(file_path)
52
  if context:
53
+ prompt_text = f"【参考文档】:\n{context}\n\n【用户指令】: {prompt_text}"
54
 
55
  clean_messages.append({"role": "user", "content": prompt_text})
56
 
57
  response = ""
58
  try:
59
+ # 修复:移除 chat_completion 里的 headers 参数
60
+ stream = client.chat_completion(
61
  clean_messages,
62
  max_tokens=2048,
63
  stream=True,
64
+ temperature=0.7
65
+ )
66
+
67
+ for msg in stream:
68
+ if hasattr(msg, 'choices') and len(msg.choices) > 0:
69
+ delta = msg.choices[0].delta
70
+ if hasattr(delta, 'content') and delta.content:
71
+ response += delta.content
72
+ yield response
73
 
74
  if response:
75
  yield response + "\n\n---\n> **OpenIPOS MegaNode | MiniMax-M2.5 极速逻辑节点**"
76
 
77
  except Exception as e:
78
+ yield f"⚠️ 算力调度提示: {str(e)}。由于万亿级模型访问量大,若持续报错请检查 Token 权限。"
 
 
 
 
 
79
 
80
+ # 3. 极简黑金 UI
81
  with gr.Blocks(fill_height=True) as demo:
82
  gr.HTML("""
83
  <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
84
+ <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
85
+ <div>
86
+ <h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
87
+ <p style="margin: 0; color: #666;">中文精品人工智能算力出口 (MiniMax-M2.5)</p>
88
+ </div>
89
  </div>
90
  """)
91
  gr.ChatInterface(chat_fn, multimodal=True)