import os import gradio as gr import spaces import fitz import docx from huggingface_hub import InferenceClient # 1. 引擎配置:Llama-3-70B-Instruct (全球开源标杆) model_id = "meta-llama/Meta-Llama-3-70B-Instruct" hf_token = os.getenv("HF_TOKEN") client = InferenceClient( model=model_id, token=hf_token, headers={"Authorization": f"Bearer {hf_token}"} if hf_token else None ) def extract_text(file_path): if not file_path: return "" try: ext = str(file_path).lower() if ext.endswith(".pdf"): doc = fitz.open(file_path) text = "".join([page.get_text() for page in doc]) doc.close() return text[:15000] elif ext.endswith(".docx"): doc = docx.Document(file_path) return "\n".join([p.text for p in doc.paragraphs])[:15000] elif ext.endswith(".txt"): with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: return f.read()[:15000] return "" except: return "" @spaces.GPU(duration=60) def chat_fn(message, history): clean_messages = [] # 系统提示词:设定为国际化法律与技术专家 clean_messages.append({"role": "system", "content": "You are a professional global intellectual property and technical expert. Answer accurately and professionally."}) for turn in history: clean_messages.append({"role": turn["role"], "content": str(turn["content"])}) prompt_text = message.get("text", "") files = message.get("files", []) if files: f = files[0] file_path = f.get("path") if isinstance(f, dict) else f context = extract_text(file_path) if context: prompt_text = f"【Reference Document】:\n{context}\n\n【Instruction】: {prompt_text}" clean_messages.append({"role": "user", "content": prompt_text}) response = "" try: stream = client.chat_completion(clean_messages, max_tokens=4096, stream=True, temperature=0.5) for msg in stream: if hasattr(msg, 'choices') and len(msg.choices) > 0: delta_content = msg.choices[0].delta.content if delta_content: # Llama 的 API 响应通常比较标准,但我们依然保留清洗逻辑 response += str(delta_content) yield response if response: yield response + "\n\n---\n> **OpenIPOS MegaNode | Meta-Llama-3 全球通用节点**" except Exception as e: yield f"⚠️ 算力调度提示: {str(e)}。由于 Llama-3 节点访问量巨大,请刷新重试。" # 3. 统一品牌装修 with gr.Blocks(fill_height=True) as demo: gr.HTML("""

OpenIPOS MegaNode

全球通用人工智能算力出口 (Meta-Llama-3-70B)

""") gr.ChatInterface(chat_fn, multimodal=True) gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Together AI / Novita Relay") demo.launch(theme=gr.themes.Soft())