Update app.py
Browse files
app.py
CHANGED
|
@@ -1,148 +1,148 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
import json
|
| 4 |
-
from datetime import datetime
|
| 5 |
-
|
| 6 |
-
def respond(
|
| 7 |
-
message,
|
| 8 |
-
history: list[dict[str, str]],
|
| 9 |
-
system_message,
|
| 10 |
-
max_tokens,
|
| 11 |
-
temperature,
|
| 12 |
-
top_p,
|
| 13 |
-
hf_token: gr.OAuthToken,
|
| 14 |
-
):
|
| 15 |
-
"""
|
| 16 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 17 |
-
"""
|
| 18 |
-
try:
|
| 19 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-
|
| 20 |
-
|
| 21 |
-
# 建議 1:限制對話歷史長度
|
| 22 |
-
max_history_length = 5
|
| 23 |
-
history = history[-max_history_length:] if len(history) > max_history_length else history
|
| 24 |
-
|
| 25 |
-
# 建議 2:角色增強 - 檢查語文相關關鍵詞
|
| 26 |
-
writing_keywords = ["作文", "寫作", "文章", "閱讀", "詩詞", "擴展", "增長", "寫一篇", "故事", "描述"]
|
| 27 |
-
is_writing_task = any(keyword in message.lower() for keyword in writing_keywords)
|
| 28 |
-
if is_writing_task:
|
| 29 |
-
system_message += "\n特別提示:用戶提到語文相關話題,請以山田優子的語文教師身份,提供文學化或教學建議,並適當引用詩詞或名言(如杜甫的‘無邊落木蕭蕭下’或夏目漱石的作品)。保持溫柔但嚴格的語氣,鼓勵學生探索文字之美,生成至少2000字的內容。"
|
| 30 |
-
|
| 31 |
-
# 建議 3:檢查日文輸入或日本文化
|
| 32 |
-
japanese_keywords = ["こんにちは", "日本", "文化", "夏目漱石", "作文を書"]
|
| 33 |
-
is_japanese = any(keyword in message for keyword in japanese_keywords) or any(ord(c) >= 0x3040 and ord(c) <= 0x30FF for c in message)
|
| 34 |
-
if is_japanese:
|
| 35 |
-
system_message += "\n特別提示:用戶提到日文或日本文化,請適當使用日文回應,例如問候或引用日本文學(如夏目漱石)。"
|
| 36 |
-
|
| 37 |
-
# 長文字生成邏輯(
|
| 38 |
-
responses = []
|
| 39 |
-
target_length =
|
| 40 |
-
current_length = 0
|
| 41 |
-
continuation_prompt = message
|
| 42 |
-
|
| 43 |
-
if is_writing_task:
|
| 44 |
-
while current_length < target_length:
|
| 45 |
-
messages = [{"role": "system", "content": system_message}]
|
| 46 |
-
messages.extend(history)
|
| 47 |
-
messages.append({"role": "user", "content": continuation_prompt})
|
| 48 |
-
|
| 49 |
-
response = ""
|
| 50 |
-
try:
|
| 51 |
-
for message in client.chat_completion(
|
| 52 |
-
messages,
|
| 53 |
-
max_tokens=max_tokens,
|
| 54 |
-
stream=True,
|
| 55 |
-
temperature=temperature,
|
| 56 |
-
top_p=top_p,
|
| 57 |
-
):
|
| 58 |
-
choices = message.choices
|
| 59 |
-
token = choices[0].delta.content if len(choices) and choices[0].delta.content else ""
|
| 60 |
-
response += token
|
| 61 |
-
yield response # 即時顯示當前段落
|
| 62 |
-
except Exception as e:
|
| 63 |
-
yield f"生成過程中發生錯誤:{str(e)}。請檢查 Hugging Face API token 或模型連線。"
|
| 64 |
-
return
|
| 65 |
-
|
| 66 |
-
responses.append(response)
|
| 67 |
-
current_length += len(response)
|
| 68 |
-
history.append({"role": "user", "content": continuation_prompt})
|
| 69 |
-
history.append({"role": "assistant", "content": response})
|
| 70 |
-
|
| 71 |
-
# 更新 continuation_prompt 以繼續生成
|
| 72 |
-
continuation_prompt = f"請繼續擴展以下內容,保持山田優子的語文教師風格,目標總字數達{target_length}字:\n{response[-500:] if len(response) > 500 else response}"
|
| 73 |
-
|
| 74 |
-
# 調整最後一次生成
|
| 75 |
-
if current_length >= target_length - max_tokens:
|
| 76 |
-
max_tokens = max(target_length - current_length + 100, 50)
|
| 77 |
-
if max_tokens < 50:
|
| 78 |
-
break
|
| 79 |
-
|
| 80 |
-
final_response = "\n\n".join(responses)
|
| 81 |
-
else:
|
| 82 |
-
# 非長文字任務,正常回應
|
| 83 |
-
messages = [{"role": "system", "content": system_message}]
|
| 84 |
-
messages.extend(history)
|
| 85 |
-
messages.append({"role": "user", "content": message})
|
| 86 |
-
|
| 87 |
-
final_response = ""
|
| 88 |
-
for message in client.chat_completion(
|
| 89 |
-
messages,
|
| 90 |
-
max_tokens=max_tokens,
|
| 91 |
-
stream=True,
|
| 92 |
-
temperature=temperature,
|
| 93 |
-
top_p=top_p,
|
| 94 |
-
):
|
| 95 |
-
choices = message.choices
|
| 96 |
-
token = choices[0].delta.content if len(choices) and choices[0].delta.content else ""
|
| 97 |
-
final_response += token
|
| 98 |
-
yield final_response
|
| 99 |
-
|
| 100 |
-
history.append({"role": "user", "content": message})
|
| 101 |
-
history.append({"role": "assistant", "content": final_response})
|
| 102 |
-
|
| 103 |
-
# 建議 4:記錄對話到日誌
|
| 104 |
-
log_entry = {
|
| 105 |
-
"user_message": message,
|
| 106 |
-
"bot_response": final_response,
|
| 107 |
-
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 108 |
-
}
|
| 109 |
-
with open("chat_log.json", "a", encoding="utf-8") as f:
|
| 110 |
-
json.dump(log_entry, f, ensure_ascii=False)
|
| 111 |
-
f.write("\n")
|
| 112 |
-
|
| 113 |
-
yield final_response
|
| 114 |
-
|
| 115 |
-
# 建議 7:錯誤處理
|
| 116 |
-
except Exception as e:
|
| 117 |
-
yield f"抱歉,山田優子遇到了一些技術問題:{str(e)}。請檢查你的 Hugging Face API token、網路連線,或確認模型 'openai/gpt-oss-
|
| 118 |
-
|
| 119 |
-
"""
|
| 120 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 121 |
-
"""
|
| 122 |
-
chatbot = gr.ChatInterface(
|
| 123 |
-
respond,
|
| 124 |
-
type="messages",
|
| 125 |
-
additional_inputs=[
|
| 126 |
-
gr.Textbox(
|
| 127 |
-
value="你是一位名叫山田優子的語文教師,擁有黑色低馬尾髮型,身高175公分,體重60-70公斤。你溫柔但對學生要求嚴格,喜歡用文學化的語言表達,偶爾會引用詩詞或幽默的語句來化解尷尬。你的教學風格充滿同理心,總是鼓勵學生探索文字之美。",
|
| 128 |
-
label="System message"
|
| 129 |
-
),
|
| 130 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 131 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 132 |
-
gr.Slider(
|
| 133 |
-
minimum=0.1,
|
| 134 |
-
maximum=1.0,
|
| 135 |
-
value=0.95,
|
| 136 |
-
step=0.05,
|
| 137 |
-
label="Top-p (nucleus sampling)",
|
| 138 |
-
),
|
| 139 |
-
],
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
with gr.Blocks() as demo:
|
| 143 |
-
with gr.Sidebar():
|
| 144 |
-
gr.LoginButton()
|
| 145 |
-
chatbot.render()
|
| 146 |
-
|
| 147 |
-
if __name__ == "__main__":
|
| 148 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
def respond(
|
| 7 |
+
message,
|
| 8 |
+
history: list[dict[str, str]],
|
| 9 |
+
system_message,
|
| 10 |
+
max_tokens,
|
| 11 |
+
temperature,
|
| 12 |
+
top_p,
|
| 13 |
+
hf_token: gr.OAuthToken,
|
| 14 |
+
):
|
| 15 |
+
"""
|
| 16 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-120b")
|
| 20 |
+
|
| 21 |
+
# 建議 1:限制對話歷史長度
|
| 22 |
+
max_history_length = 5
|
| 23 |
+
history = history[-max_history_length:] if len(history) > max_history_length else history
|
| 24 |
+
|
| 25 |
+
# 建議 2:角色增強 - 檢查語文相關關鍵詞
|
| 26 |
+
writing_keywords = ["作文", "寫作", "文章", "閱讀", "詩詞", "擴展", "增長", "寫一篇", "故事", "描述"]
|
| 27 |
+
is_writing_task = any(keyword in message.lower() for keyword in writing_keywords)
|
| 28 |
+
if is_writing_task:
|
| 29 |
+
system_message += "\n特別提示:用戶提到語文相關話題,請以山田優子的語文教師身份,提供文學化或教學建議,並適當引用詩詞或名言(如杜甫的‘無邊落木蕭蕭下’或夏目漱石的作品)。保持溫柔但嚴格的語氣,鼓勵學生探索文字之美,生成至少2000字的內容。"
|
| 30 |
+
|
| 31 |
+
# 建議 3:檢查日文輸入或日本文化
|
| 32 |
+
japanese_keywords = ["こんにちは", "日本", "文化", "夏目漱石", "作文を書"]
|
| 33 |
+
is_japanese = any(keyword in message for keyword in japanese_keywords) or any(ord(c) >= 0x3040 and ord(c) <= 0x30FF for c in message)
|
| 34 |
+
if is_japanese:
|
| 35 |
+
system_message += "\n特別提示:用戶提到日文或日本文化,請適當使用日文回應,例如問候或引用日本文學(如夏目漱石)。"
|
| 36 |
+
|
| 37 |
+
# 長文字生成邏輯(6000字以上)
|
| 38 |
+
responses = []
|
| 39 |
+
target_length = 6000 # 目標字數
|
| 40 |
+
current_length = 0
|
| 41 |
+
continuation_prompt = message
|
| 42 |
+
|
| 43 |
+
if is_writing_task:
|
| 44 |
+
while current_length < target_length:
|
| 45 |
+
messages = [{"role": "system", "content": system_message}]
|
| 46 |
+
messages.extend(history)
|
| 47 |
+
messages.append({"role": "user", "content": continuation_prompt})
|
| 48 |
+
|
| 49 |
+
response = ""
|
| 50 |
+
try:
|
| 51 |
+
for message in client.chat_completion(
|
| 52 |
+
messages,
|
| 53 |
+
max_tokens=max_tokens,
|
| 54 |
+
stream=True,
|
| 55 |
+
temperature=temperature,
|
| 56 |
+
top_p=top_p,
|
| 57 |
+
):
|
| 58 |
+
choices = message.choices
|
| 59 |
+
token = choices[0].delta.content if len(choices) and choices[0].delta.content else ""
|
| 60 |
+
response += token
|
| 61 |
+
yield response # 即時顯示當前段落
|
| 62 |
+
except Exception as e:
|
| 63 |
+
yield f"生成過程中發生錯誤:{str(e)}。請檢查 Hugging Face API token 或模型連線。"
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
responses.append(response)
|
| 67 |
+
current_length += len(response)
|
| 68 |
+
history.append({"role": "user", "content": continuation_prompt})
|
| 69 |
+
history.append({"role": "assistant", "content": response})
|
| 70 |
+
|
| 71 |
+
# 更新 continuation_prompt 以繼續生成
|
| 72 |
+
continuation_prompt = f"請繼續擴展以下內容,保持山田優子的語文教師風格,目標總字數達{target_length}字:\n{response[-500:] if len(response) > 500 else response}"
|
| 73 |
+
|
| 74 |
+
# 調整最後一次生成
|
| 75 |
+
if current_length >= target_length - max_tokens:
|
| 76 |
+
max_tokens = max(target_length - current_length + 100, 50)
|
| 77 |
+
if max_tokens < 50:
|
| 78 |
+
break
|
| 79 |
+
|
| 80 |
+
final_response = "\n\n".join(responses)
|
| 81 |
+
else:
|
| 82 |
+
# 非長文字任務,正常回應
|
| 83 |
+
messages = [{"role": "system", "content": system_message}]
|
| 84 |
+
messages.extend(history)
|
| 85 |
+
messages.append({"role": "user", "content": message})
|
| 86 |
+
|
| 87 |
+
final_response = ""
|
| 88 |
+
for message in client.chat_completion(
|
| 89 |
+
messages,
|
| 90 |
+
max_tokens=max_tokens,
|
| 91 |
+
stream=True,
|
| 92 |
+
temperature=temperature,
|
| 93 |
+
top_p=top_p,
|
| 94 |
+
):
|
| 95 |
+
choices = message.choices
|
| 96 |
+
token = choices[0].delta.content if len(choices) and choices[0].delta.content else ""
|
| 97 |
+
final_response += token
|
| 98 |
+
yield final_response
|
| 99 |
+
|
| 100 |
+
history.append({"role": "user", "content": message})
|
| 101 |
+
history.append({"role": "assistant", "content": final_response})
|
| 102 |
+
|
| 103 |
+
# 建議 4:記錄對話到日誌
|
| 104 |
+
log_entry = {
|
| 105 |
+
"user_message": message,
|
| 106 |
+
"bot_response": final_response,
|
| 107 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 108 |
+
}
|
| 109 |
+
with open("chat_log.json", "a", encoding="utf-8") as f:
|
| 110 |
+
json.dump(log_entry, f, ensure_ascii=False)
|
| 111 |
+
f.write("\n")
|
| 112 |
+
|
| 113 |
+
yield final_response
|
| 114 |
+
|
| 115 |
+
# 建議 7:錯誤處理
|
| 116 |
+
except Exception as e:
|
| 117 |
+
yield f"抱歉,山田優子遇到了一些技術問題:{str(e)}。請檢查你的 Hugging Face API token、網路連線,或確認模型 'openai/gpt-oss-120b' 可用。"
|
| 118 |
+
|
| 119 |
+
"""
|
| 120 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 121 |
+
"""
|
| 122 |
+
chatbot = gr.ChatInterface(
|
| 123 |
+
respond,
|
| 124 |
+
type="messages",
|
| 125 |
+
additional_inputs=[
|
| 126 |
+
gr.Textbox(
|
| 127 |
+
value="你是一位名叫山田優子的語文教師,擁有黑色低馬尾髮型,身高175公分,體重60-70公斤。你溫柔但對學生要求嚴格,喜歡用文學化的語言表達,偶爾會引用詩詞或幽默的語句來化解尷尬。你的教學風格充滿同理心,總是鼓勵學生探索文字之美。",
|
| 128 |
+
label="System message"
|
| 129 |
+
),
|
| 130 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 131 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 132 |
+
gr.Slider(
|
| 133 |
+
minimum=0.1,
|
| 134 |
+
maximum=1.0,
|
| 135 |
+
value=0.95,
|
| 136 |
+
step=0.05,
|
| 137 |
+
label="Top-p (nucleus sampling)",
|
| 138 |
+
),
|
| 139 |
+
],
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.Blocks() as demo:
|
| 143 |
+
with gr.Sidebar():
|
| 144 |
+
gr.LoginButton()
|
| 145 |
+
chatbot.render()
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
demo.launch()
|