OpenIPOS commited on
Commit
2d3288d
·
verified ·
1 Parent(s): 8f4b72e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -16,40 +16,48 @@ def extract_text(file_path):
16
  doc = fitz.open(file_path)
17
  text = "".join([page.get_text() for page in doc])
18
  doc.close()
19
- return text[:20000] # 截取前2万字,防止超长导致API崩溃
20
  return f"[检测到附件: {os.path.basename(file_path)}]"
21
  except Exception as e:
22
  return f"[解析失败: {str(e)}]"
23
 
24
  @spaces.GPU(duration=60)
25
  def chat_fn(message, history):
26
- # 构造对话历史
27
- messages = []
28
  for turn in history:
29
- messages.append({"role": turn["role"], "content": turn["content"]})
 
 
 
 
30
 
31
- # 提取用户输入和文件
32
  prompt_text = message.get("text", "")
33
  files = message.get("files", [])
34
 
35
- # 处理文件逻辑
36
  if files and len(files) > 0:
37
- # 兼容处理 Gradio 6.0 的文件对象格式
38
  f = files[0]
39
  file_path = f.get("path") if isinstance(f, dict) else f
40
  context = extract_text(file_path)
41
  if context:
42
- prompt_text = f"【参考文档】:\n{context}\n\n【用户指令】: {prompt_text}"
 
43
 
44
- messages.append({"role": "user", "content": prompt_text})
45
 
46
  response = ""
47
  try:
48
- # 增加流式输出的安全性校验
49
- stream = client.chat_completion(messages, max_tokens=4096, stream=True, temperature=0.6)
 
 
 
 
 
50
 
51
  for msg in stream:
52
- # 这里的判断是修复 'list index out of range' 的关键
53
  if hasattr(msg, 'choices') and len(msg.choices) > 0:
54
  delta = msg.choices[0].delta
55
  if hasattr(delta, 'content') and delta.content:
@@ -57,17 +65,15 @@ def chat_fn(message, history):
57
  yield response
58
 
59
  if response:
60
- response += "\n\n---\n> **OpenIPOS MegaNode | Kimi-K2.5 旗舰算力**"
61
  yield response
62
 
63
  except Exception as e:
64
- # 捕获所有异常并友好提示
65
- yield f"⚠️ 算力调度波动: {str(e)}。由于 Kimi-K2.5 访问量巨大,请刷新重试。"
66
 
67
- # 3. UI 界面 (Logo 完美适配)
68
  with gr.Blocks(fill_height=True) as demo:
69
  with gr.Row():
70
- # 使用 HTML 确保 Logo 比例协调
71
  gr.HTML("""
72
  <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
73
  <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
@@ -83,5 +89,4 @@ with gr.Blocks(fill_height=True) as demo:
83
  gr.Markdown("---")
84
  gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face Inference Providers")
85
 
86
- # 启动配置
87
  demo.launch(theme=gr.themes.Soft())
 
16
  doc = fitz.open(file_path)
17
  text = "".join([page.get_text() for page in doc])
18
  doc.close()
19
+ return text[:20000] # 截取前2万字,防止超长
20
  return f"[检测到附件: {os.path.basename(file_path)}]"
21
  except Exception as e:
22
  return f"[解析失败: {str(e)}]"
23
 
24
  @spaces.GPU(duration=60)
25
  def chat_fn(message, history):
26
+ # --- 关键修复:清洗历史记录,确保只有纯字符串 ---
27
+ clean_messages = []
28
  for turn in history:
29
+ # 强制将历史记录内容转化为纯字符串,剔除任何嵌套的对象/文件
30
+ content = turn.get("content", "")
31
+ if not isinstance(content, str):
32
+ content = str(content)
33
+ clean_messages.append({"role": turn["role"], "content": content})
34
 
35
+ # 提取当前用户输入
36
  prompt_text = message.get("text", "")
37
  files = message.get("files", [])
38
 
39
+ # 处理新上传的文件逻辑
40
  if files and len(files) > 0:
 
41
  f = files[0]
42
  file_path = f.get("path") if isinstance(f, dict) else f
43
  context = extract_text(file_path)
44
  if context:
45
+ # 只有在第一轮或新传文件时注入上下文
46
+ prompt_text = f"【参考文档内容】:\n{context}\n\n【当前指令】: {prompt_text}"
47
 
48
+ clean_messages.append({"role": "user", "content": prompt_text})
49
 
50
  response = ""
51
  try:
52
+ # 调用 API
53
+ stream = client.chat_completion(
54
+ clean_messages,
55
+ max_tokens=4096,
56
+ stream=True,
57
+ temperature=0.6
58
+ )
59
 
60
  for msg in stream:
 
61
  if hasattr(msg, 'choices') and len(msg.choices) > 0:
62
  delta = msg.choices[0].delta
63
  if hasattr(delta, 'content') and delta.content:
 
65
  yield response
66
 
67
  if response:
68
+ response += "\n\n---\n> **OpenIPOS MegaNode | 旗舰算力输出**"
69
  yield response
70
 
71
  except Exception as e:
72
+ yield f"⚠️ 算力调度波动: {str(e)}。由于 Kimi-K2.5 访问量巨大,请尝试刷新。建议:翻译长文档时请分段进行。"
 
73
 
74
+ # 3. UI 界面
75
  with gr.Blocks(fill_height=True) as demo:
76
  with gr.Row():
 
77
  gr.HTML("""
78
  <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
79
  <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
 
89
  gr.Markdown("---")
90
  gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face Inference Providers")
91
 
 
92
  demo.launch(theme=gr.themes.Soft())