Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,89 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import spaces
|
| 4 |
+
import fitz
|
| 5 |
+
import docx
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
# 1. 引擎配置:Llama-3-70B-Instruct (全球开源标杆)
|
| 9 |
+
model_id = "meta-llama/Meta-Llama-3-70B-Instruct"
|
| 10 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
client = InferenceClient(
|
| 13 |
+
model=model_id,
|
| 14 |
+
token=hf_token,
|
| 15 |
+
headers={"Authorization": f"Bearer {hf_token}"} if hf_token else None
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def extract_text(file_path):
|
| 19 |
+
if not file_path: return ""
|
| 20 |
+
try:
|
| 21 |
+
ext = str(file_path).lower()
|
| 22 |
+
if ext.endswith(".pdf"):
|
| 23 |
+
doc = fitz.open(file_path)
|
| 24 |
+
text = "".join([page.get_text() for page in doc])
|
| 25 |
+
doc.close()
|
| 26 |
+
return text[:15000]
|
| 27 |
+
elif ext.endswith(".docx"):
|
| 28 |
+
doc = docx.Document(file_path)
|
| 29 |
+
return "\n".join([p.text for p in doc.paragraphs])[:15000]
|
| 30 |
+
elif ext.endswith(".txt"):
|
| 31 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 32 |
+
return f.read()[:15000]
|
| 33 |
+
return ""
|
| 34 |
+
except:
|
| 35 |
+
return ""
|
| 36 |
+
|
| 37 |
+
@spaces.GPU(duration=60)
|
| 38 |
+
def chat_fn(message, history):
|
| 39 |
+
clean_messages = []
|
| 40 |
+
# 系统提示词:设定为国际化法律与技术专家
|
| 41 |
+
clean_messages.append({"role": "system", "content": "You are a professional global intellectual property and technical expert. Answer accurately and professionally."})
|
| 42 |
+
|
| 43 |
+
for turn in history:
|
| 44 |
+
clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
|
| 45 |
|
| 46 |
+
prompt_text = message.get("text", "")
|
| 47 |
+
files = message.get("files", [])
|
| 48 |
+
|
| 49 |
+
if files:
|
| 50 |
+
f = files[0]
|
| 51 |
+
file_path = f.get("path") if isinstance(f, dict) else f
|
| 52 |
+
context = extract_text(file_path)
|
| 53 |
+
if context:
|
| 54 |
+
prompt_text = f"【Reference Document】:\n{context}\n\n【Instruction】: {prompt_text}"
|
| 55 |
+
|
| 56 |
+
clean_messages.append({"role": "user", "content": prompt_text})
|
| 57 |
+
|
| 58 |
+
response = ""
|
| 59 |
+
try:
|
| 60 |
+
stream = client.chat_completion(clean_messages, max_tokens=4096, stream=True, temperature=0.5)
|
| 61 |
+
for msg in stream:
|
| 62 |
+
if hasattr(msg, 'choices') and len(msg.choices) > 0:
|
| 63 |
+
delta_content = msg.choices[0].delta.content
|
| 64 |
+
if delta_content:
|
| 65 |
+
# Llama 的 API 响应通常比较标准,但我们依然保留清洗逻辑
|
| 66 |
+
response += str(delta_content)
|
| 67 |
+
yield response
|
| 68 |
+
|
| 69 |
+
if response:
|
| 70 |
+
yield response + "\n\n---\n> **OpenIPOS MegaNode | Meta-Llama-3 全球通用节点**"
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
yield f"⚠️ 算力调度提示: {str(e)}。由于 Llama-3 节点访问量巨大,请刷新重试。"
|
| 74 |
+
|
| 75 |
+
# 3. 统一品牌装修
|
| 76 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 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;">
|
| 80 |
+
<div>
|
| 81 |
+
<h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
|
| 82 |
+
<p style="margin: 0; color: #666;">全球通用人工智能算力出口 (Meta-Llama-3-70B)</p>
|
| 83 |
+
</div>
|
| 84 |
+
</div>
|
| 85 |
+
""")
|
| 86 |
+
gr.ChatInterface(chat_fn, multimodal=True)
|
| 87 |
+
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Together AI / Novita Relay")
|
| 88 |
+
|
| 89 |
+
demo.launch(theme=gr.themes.Soft())
|