OpenIPOS commited on
Commit
393d365
·
verified ·
1 Parent(s): 1073ccc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -21
app.py CHANGED
@@ -1,44 +1,104 @@
1
  import os
2
  import gradio as gr
 
 
 
3
  from huggingface_hub import InferenceClient
4
 
5
- # 1. 引擎配置:MiniMax-M2.5 (229B 精品逻辑引擎)
6
  model_id = "MiniMaxAI/MiniMax-M2.5"
7
  client = InferenceClient(model_id, token=os.getenv("HF_TOKEN"))
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def chat_fn(message, history):
10
- messages = []
 
11
  for turn in history:
12
- messages.append({"role": turn["role"], "content": turn["content"]})
 
 
 
 
 
 
 
13
 
14
- messages.append({"role": "user", "content": message["text"]})
 
 
 
 
 
 
 
 
15
 
16
  response = ""
17
  try:
18
- for msg in client.chat_completion(messages, max_tokens=4096, stream=True):
19
- if msg.choices[0].delta.content:
20
- response += msg.choices[0].delta.content
21
- yield response
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- response += "\n\n---\n> **OpenIPOS MegaNode | MiniMax-M2.5 极速逻辑节点**"
24
- yield response
 
 
25
  except Exception as e:
26
- yield f"⚠️ 算力调度提示: {str(e)}"
27
 
28
  # 3. 统一品牌装修
29
  with gr.Blocks(fill_height=True) as demo:
30
- gr.HTML("""
31
- <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
32
- <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px;">
33
- <div>
34
- <h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
35
- <p style="margin: 0; color: #666;">中文精品人工智能算力出口 (MiniMax-M2.5)</p>
 
 
36
  </div>
37
- </div>
38
- """)
39
  gr.Markdown("---")
40
- gr.ChatInterface(chat_fn)
 
 
41
  gr.Markdown("---")
42
- gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Novita API Relay")
43
 
44
  demo.launch(theme=gr.themes.Soft())
 
1
  import os
2
  import gradio as gr
3
+ import spaces
4
+ import fitz # PyMuPDF
5
+ import docx # python-docx
6
  from huggingface_hub import InferenceClient
7
 
8
+ # 1. 引擎配置 (MiniMax-M2.5 229B)
9
  model_id = "MiniMaxAI/MiniMax-M2.5"
10
  client = InferenceClient(model_id, token=os.getenv("HF_TOKEN"))
11
 
12
+ # 2. 全能文件解析解析器
13
+ def extract_text(file_path):
14
+ if not file_path: return ""
15
+ try:
16
+ ext = str(file_path).lower()
17
+ # PDF 解析
18
+ if ext.endswith(".pdf"):
19
+ doc = fitz.open(file_path)
20
+ text = "".join([page.get_text() for page in doc])
21
+ doc.close()
22
+ return text[:15000] # 适当截取,保证稳定性
23
+ # Word 解析
24
+ elif ext.endswith(".docx"):
25
+ doc = docx.Document(file_path)
26
+ return "\n".join([p.text for p in doc.paragraphs])[:15000]
27
+ # 纯文本解析
28
+ elif ext.endswith(".txt"):
29
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
30
+ return f.read()[:15000]
31
+ return f"[暂不支持直接解析此格式: {os.path.basename(file_path)}]"
32
+ except Exception as e:
33
+ return f"[解析失败: {str(e)}]"
34
+
35
+ @spaces.GPU(duration=60)
36
  def chat_fn(message, history):
37
+ # 强制历史记录清洗 (MiniMax API 对此非常敏感)
38
+ clean_messages = []
39
  for turn in history:
40
+ # 确保 content 永远是字符串
41
+ u_content = turn.get("content", "")
42
+ if not isinstance(u_content, str): u_content = str(u_content)
43
+ clean_messages.append({"role": turn.get("role", "user"), "content": u_content})
44
+
45
+ # 提取当前输入
46
+ prompt_text = message.get("text", "")
47
+ files = message.get("files", [])
48
 
49
+ # 处理文件逻辑
50
+ if files and len(files) > 0:
51
+ f = files[0]
52
+ file_path = f.get("path") if isinstance(f, dict) else f
53
+ context = extract_text(file_path)
54
+ if context:
55
+ prompt_text = f"【参考文档】:\n{context}\n\n【指令】: {prompt_text}"
56
+
57
+ clean_messages.append({"role": "user", "content": prompt_text})
58
 
59
  response = ""
60
  try:
61
+ # 增加超时和异常保护
62
+ stream = client.chat_completion(
63
+ clean_messages,
64
+ max_tokens=2048,
65
+ stream=True,
66
+ temperature=0.7
67
+ )
68
+
69
+ for msg in stream:
70
+ # 严格检查 API 返回结构
71
+ if hasattr(msg, 'choices') and len(msg.choices) > 0:
72
+ delta = msg.choices[0].delta
73
+ if hasattr(delta, 'content') and delta.content:
74
+ response += delta.content
75
+ yield response
76
 
77
+ if response:
78
+ response += "\n\n---\n> **OpenIPOS MegaNode | MiniMax-M2.5 极速逻辑引擎**"
79
+ yield response
80
+
81
  except Exception as e:
82
+ yield f"⚠️ 调度波动: {str(e)}。提示:由于模型发布初期访问量大,API 偶尔会返回 Error,请稍后重试。"
83
 
84
  # 3. 统一品牌装修
85
  with gr.Blocks(fill_height=True) as demo:
86
+ with gr.Row():
87
+ gr.HTML("""
88
+ <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
89
+ <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
90
+ <div>
91
+ <h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
92
+ <p style="margin: 0; color: #666;">中文精品人工智能算力出口 (MiniMax-M2.5)</p>
93
+ </div>
94
  </div>
95
+ """)
96
+
97
  gr.Markdown("---")
98
+ # 开启多模态支持
99
+ gr.ChatInterface(chat_fn, multimodal=True)
100
+
101
  gr.Markdown("---")
102
+ gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face Inference Providers")
103
 
104
  demo.launch(theme=gr.themes.Soft())