Download utils/ai_utils.py from PerrinT/office: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/PerrinT/office/resolve/main/utils/ai_utils.py
- Command line
-
hf download hf://spaces/PerrinT/office/utils/ai_utils.py
-
curl -L -o ai_utils.py https://huggingface.co/spaces/PerrinT/office/resolve/main/utils/ai_utils.py
5.01 kB
| import streamlit as st | |
| import os | |
| from openai import OpenAI | |
| def get_mimo_client(): | |
| """获取mimo API客户端""" | |
| # 优先从环境变量读取 | |
| api_key = os.getenv("MIMO_API_KEY") | |
| base_url = os.getenv("MIMO_BASE_URL") | |
| # 如果环境变量没有,尝试从st.secrets读取 | |
| if not api_key: | |
| try: | |
| api_key = st.secrets["MIMO_API_KEY"] | |
| except: | |
| pass | |
| if not base_url: | |
| try: | |
| base_url = st.secrets["MIMO_BASE_URL"] | |
| except: | |
| base_url = "https://api.xiaomimimo.com/v1" | |
| if not api_key: | |
| return None | |
| return OpenAI( | |
| api_key=api_key, | |
| base_url=base_url | |
| ) | |
| def summarize_document(text: str, max_length: int = 500) -> str: | |
| """文档摘要""" | |
| client = get_mimo_client() | |
| if not client: | |
| return "错误:未配置 MIMO API Key,请在 Settings → Secrets 中配置" | |
| try: | |
| response = client.chat.completions.create( | |
| model="mimo-v2.5-pro", | |
| messages=[ | |
| {"role": "system", "content": "你是一个专业的文档摘要助手,请用简洁的语言总结文档核心内容。"}, | |
| {"role": "user", "content": f"请为以下文档生成摘要,控制在{max_length}字以内:\n\n{text}"} | |
| ], | |
| max_tokens=1000 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"API调用失败: {str(e)}" | |
| def translate_document(text: str, target_lang: str = "英文") -> str: | |
| """文档翻译""" | |
| client = get_mimo_client() | |
| if not client: | |
| return "错误:未配置 MIMO API Key,请在 Settings → Secrets 中配置" | |
| try: | |
| response = client.chat.completions.create( | |
| model="mimo-v2.5-pro", | |
| messages=[ | |
| {"role": "system", "content": f"你是一个专业的翻译助手,请将文档翻译成{target_lang},保持原文格式和专业术语。"}, | |
| {"role": "user", "content": f"请翻译以下文档:\n\n{text}"} | |
| ], | |
| max_tokens=2000 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"API调用失败: {str(e)}" | |
| def organize_meeting_notes(notes: str) -> str: | |
| """会议纪要整理""" | |
| client = get_mimo_client() | |
| if not client: | |
| return "错误:未配置 MIMO API Key,请在 Settings → Secrets 中配置" | |
| try: | |
| response = client.chat.completions.create( | |
| model="mimo-v2.5-pro", | |
| messages=[ | |
| {"role": "system", "content": "你是一个专业的会议纪要整理助手,请将杂乱的会议笔记整理成结构化的会议纪要格式,包含:会议主题、参会人员、主要议题、讨论要点、决议事项、待办事项。"}, | |
| {"role": "user", "content": f"请整理以下会议笔记:\n\n{notes}"} | |
| ], | |
| max_tokens=1500 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"API调用失败: {str(e)}" | |
| def explain_contract_terms(text: str) -> str: | |
| """合同条款解读""" | |
| client = get_mimo_client() | |
| if not client: | |
| return "错误:未配置 MIMO API Key,请在 Settings → Secrets 中配置" | |
| try: | |
| response = client.chat.completions.create( | |
| model="mimo-v2.5-pro", | |
| messages=[ | |
| {"role": "system", "content": "你是一个法律顾问助手,请用通俗易懂的语言解释合同条款,指出可能的风险点和注意事项。"}, | |
| {"role": "user", "content": f"请解读以下合同条款:\n\n{text}"} | |
| ], | |
| max_tokens=1500 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"API调用失败: {str(e)}" | |
| def generate_ppt_content(topic: str, slides_count: int = 10, style: str = "商务") -> dict: | |
| """生成PPT内容""" | |
| client = get_mimo_client() | |
| if not client: | |
| return None | |
| prompt = f"""请为以下主题生成PPT内容,要求: | |
| - 主题:{topic} | |
| - 页数:{slides_count}页 | |
| - 风格:{style} | |
| 请以JSON格式返回,结构如下: | |
| {{ | |
| "title": "PPT标题", | |
| "slides": [ | |
| {{ | |
| "title": "页面标题", | |
| "content": ["要点1", "要点2", "要点3"], | |
| "notes": "演讲者备注" | |
| }} | |
| ] | |
| }}""" | |
| try: | |
| response = client.chat.completions.create( | |
| model="mimo-v2.5-pro", | |
| messages=[ | |
| {"role": "system", "content": "你是一个专业的PPT内容创作助手,请生成结构清晰、内容专业的PPT大纲。"}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| max_tokens=3000 | |
| ) | |
| import json | |
| return json.loads(response.choices[0].message.content) | |
| except Exception as e: | |
| st.error(f"API调用失败: {str(e)}") | |
| return None | |