Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,21 +78,37 @@ tools = [{"type": "function", "function": record_user_details_json},
|
|
| 78 |
class Me:
|
| 79 |
|
| 80 |
def __init__(self):
|
| 81 |
-
# 🌟 關鍵修改 1:
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
self.openai = OpenAI(
|
|
|
|
| 85 |
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 86 |
)
|
| 87 |
self.name = "Rika Choi"
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
def handle_tool_call(self, tool_calls):
|
| 98 |
results = []
|
|
@@ -102,8 +118,6 @@ class Me:
|
|
| 102 |
print(f"Tool called: {tool_name}", flush=True)
|
| 103 |
tool = globals().get(tool_name)
|
| 104 |
result = tool(**arguments) if tool else {}
|
| 105 |
-
# 注意:Gemini API (透過 OpenAI 庫) 對於工具調用的回應格式略有不同
|
| 106 |
-
# 這裡沿用 OpenAI 的格式,它通常在底層會進行處理
|
| 107 |
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
| 108 |
return results
|
| 109 |
|
|
@@ -125,7 +139,6 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 125 |
done = False
|
| 126 |
while not done:
|
| 127 |
# 🌟 關鍵修改 2: 替換成 Gemini 模型名稱
|
| 128 |
-
# 使用支援工具呼叫的 Gemini 模型,例如 'gemini-2.5-flash'
|
| 129 |
response = self.openai.chat.completions.create(
|
| 130 |
model="gemini-2.5-flash",
|
| 131 |
messages=messages,
|
|
@@ -146,4 +159,45 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
me = Me()
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
class Me:
|
| 79 |
|
| 80 |
def __init__(self):
|
| 81 |
+
# 🌟 關鍵修改 1 (修正錯誤): 明確讀取環境變數並傳入
|
| 82 |
+
gemini_api_key = os.getenv("OPENAI_API_KEY")
|
| 83 |
+
|
| 84 |
+
# 建議: 檢查金鑰是否存在,如果沒有則拋出更明確的錯誤
|
| 85 |
+
if not gemini_api_key:
|
| 86 |
+
raise ValueError("API Key not found. Please set your Gemini API Key as OPENAI_API_KEY in Hugging Face Secrets.")
|
| 87 |
+
|
| 88 |
+
# 由於使用 OpenAI 函式庫來呼叫 Gemini API,必須指定 api_key 和 base_url
|
| 89 |
self.openai = OpenAI(
|
| 90 |
+
api_key=gemini_api_key, # 明确傳入讀取到的金鑰
|
| 91 |
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 92 |
)
|
| 93 |
self.name = "Rika Choi"
|
| 94 |
+
|
| 95 |
+
# 處理檔案讀取錯誤,避免應用程式崩潰
|
| 96 |
+
try:
|
| 97 |
+
reader = PdfReader("me/linkedin.pdf")
|
| 98 |
+
self.linkedin = ""
|
| 99 |
+
for page in reader.pages:
|
| 100 |
+
text = page.extract_text()
|
| 101 |
+
if text:
|
| 102 |
+
self.linkedin += text
|
| 103 |
+
except FileNotFoundError:
|
| 104 |
+
self.linkedin = "LinkedIn file not found. AI will rely on summary only."
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
| 108 |
+
self.summary = f.read()
|
| 109 |
+
except FileNotFoundError:
|
| 110 |
+
self.summary = "Summary file not found."
|
| 111 |
+
|
| 112 |
|
| 113 |
def handle_tool_call(self, tool_calls):
|
| 114 |
results = []
|
|
|
|
| 118 |
print(f"Tool called: {tool_name}", flush=True)
|
| 119 |
tool = globals().get(tool_name)
|
| 120 |
result = tool(**arguments) if tool else {}
|
|
|
|
|
|
|
| 121 |
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
| 122 |
return results
|
| 123 |
|
|
|
|
| 139 |
done = False
|
| 140 |
while not done:
|
| 141 |
# 🌟 關鍵修改 2: 替換成 Gemini 模型名稱
|
|
|
|
| 142 |
response = self.openai.chat.completions.create(
|
| 143 |
model="gemini-2.5-flash",
|
| 144 |
messages=messages,
|
|
|
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
me = Me()
|
| 162 |
+
|
| 163 |
+
# 🌟 新增 Gradio 介面美化和介紹資訊 (使用您提供的網址內容)
|
| 164 |
+
intro_markdown = f"""
|
| 165 |
+
<div style="text-align: center;">
|
| 166 |
+
<h1 style="color: #0047b3;">💼 與 {me.name} (徐可瑜) 的 AI 助手對話</h1>
|
| 167 |
+
<p>這是徐可瑜經理的 AI 分身,專門回答有關她的職涯、背景、技能與經驗的問題。</p>
|
| 168 |
+
<hr>
|
| 169 |
+
</div>
|
| 170 |
+
|
| 171 |
+
## 👤 關於徐可瑜 (Rika Choi) 經理
|
| 172 |
+
|
| 173 |
+
徐經理目前任職於 **資誠聯合會計師事務所 (PwC Taiwan) 臺北辦公室**,職稱為**經理**。她同時也是企業智權管理實務委員會委員。
|
| 174 |
+
|
| 175 |
+
**聯絡資訊:**
|
| 176 |
+
* **電話:** +886 2 27295200, 分機 23528
|
| 177 |
+
* **PwC 頁面:** [徐可瑜 - 資誠聯合會計師事務所 PwC Taiwan](https://www.pwc.tw/zh/contacts/r/rika-choi.html)
|
| 178 |
+
|
| 179 |
+
## ✨ 擅長領域/專業經驗
|
| 180 |
+
|
| 181 |
+
徐經理的專業領域集中在智慧財產權管理與租稅優惠輔導:
|
| 182 |
+
|
| 183 |
+
* **輔導服務:** 研發、智機及資安投抵之輔導服務。
|
| 184 |
+
* **智財管理:** 智財管理制度建置之輔導及諮詢服務。
|
| 185 |
+
* **專利/商標:** 國內外專利申請服務、國內外商標申請服務。
|
| 186 |
+
* **資格:** 中華民國專利師、TIPS智財管理制度自評員。
|
| 187 |
+
|
| 188 |
+
---
|
| 189 |
+
"""
|
| 190 |
+
|
| 191 |
+
# 使用 gr.Blocks 來組織 Markdown 和 ChatInterface
|
| 192 |
+
with gr.Blocks(title=f"{me.name} AI Chatbot") as demo:
|
| 193 |
+
# 使用 Markdown 顯示介紹資訊
|
| 194 |
+
gr.Markdown(intro_markdown)
|
| 195 |
+
|
| 196 |
+
# 創建 ChatInterface
|
| 197 |
+
gr.ChatInterface(
|
| 198 |
+
me.chat,
|
| 199 |
+
title="請開始提問!",
|
| 200 |
+
theme="soft",
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
demo.launch()
|