Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def respond(
|
| 5 |
message,
|
|
@@ -13,40 +15,84 @@ def respond(
|
|
| 13 |
"""
|
| 14 |
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
|
| 15 |
"""
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
temperature=temperature,
|
| 31 |
-
top_p=top_p,
|
| 32 |
-
):
|
| 33 |
-
choices = message.choices
|
| 34 |
-
token = ""
|
| 35 |
-
if len(choices) and choices[0].delta.content:
|
| 36 |
-
token = choices[0].delta.content
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
chatbot = gr.ChatInterface(
|
| 45 |
respond,
|
| 46 |
type="messages",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
additional_inputs=[
|
| 48 |
gr.Textbox(
|
| 49 |
-
value="你是一位名叫山田優子的語文教師,擁有黑色低馬尾髮型,身高175公分,體重60-70
|
| 50 |
label="System message"
|
| 51 |
),
|
| 52 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
@@ -64,6 +110,7 @@ chatbot = gr.ChatInterface(
|
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
with gr.Sidebar():
|
| 66 |
gr.LoginButton()
|
|
|
|
| 67 |
chatbot.render()
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 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,
|
|
|
|
| 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-20b")
|
| 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 |
+
if any(keyword in message for keyword in writing_keywords):
|
| 28 |
+
system_message += "\n特別提示:用戶提到語文相關話題,請以山田優子的語文教師身份,提供文學化或教學建議,並適當引用詩詞或名言。"
|
| 29 |
|
| 30 |
+
# 建議 3:檢查日文輸入或日本文化
|
| 31 |
+
japanese_keywords = ["こんにちは", "日本", "文化", "夏目漱石"]
|
| 32 |
+
if any(keyword in message for keyword in japanese_keywords) or any(ord(c) >= 0x3040 and ord(c) <= 0x30FF for c in message):
|
| 33 |
+
system_message += "\n特別提示:用戶提到日文或日本文化,請適當使用日文回應,例如問候或引用日本文學(如夏目漱石)。"
|
| 34 |
|
| 35 |
+
# 長文字輸入處理:將輸入分段(每段 1000 字)
|
| 36 |
+
max_chunk_size = 1000 # 每段最大字數
|
| 37 |
+
responses = []
|
| 38 |
+
if len(message) > max_chunk_size:
|
| 39 |
+
chunks = [message[i:i + max_chunk_size] for i in range(0, len(message), max_chunk_size)]
|
| 40 |
+
else:
|
| 41 |
+
chunks = [message]
|
| 42 |
|
| 43 |
+
for chunk in chunks:
|
| 44 |
+
messages = [{"role": "system", "content": system_message}]
|
| 45 |
+
messages.extend(history)
|
| 46 |
+
messages.append({"role": "user", "content": chunk})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
response = ""
|
| 49 |
+
for message in client.chat_completion(
|
| 50 |
+
messages,
|
| 51 |
+
max_tokens=max_tokens,
|
| 52 |
+
stream=True,
|
| 53 |
+
temperature=temperature,
|
| 54 |
+
top_p=top_p,
|
| 55 |
+
):
|
| 56 |
+
choices = message.choices
|
| 57 |
+
token = choices[0].delta.content if len(choices) and choices[0].delta.content else ""
|
| 58 |
+
response += token
|
| 59 |
+
yield response # 即時顯示每段回應
|
| 60 |
+
responses.append(response)
|
| 61 |
+
history.append({"role": "user", "content": chunk})
|
| 62 |
+
history.append({"role": "assistant", "content": response})
|
| 63 |
|
| 64 |
+
# 合併所有回應
|
| 65 |
+
final_response = "\n\n".join(responses)
|
| 66 |
+
|
| 67 |
+
# 建議 4:記錄對話到日誌
|
| 68 |
+
log_entry = {
|
| 69 |
+
"user_message": message,
|
| 70 |
+
"bot_response": final_response,
|
| 71 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 72 |
+
}
|
| 73 |
+
with open("chat_log.json", "a", encoding="utf-8") as f:
|
| 74 |
+
json.dump(log_entry, f, ensure_ascii=False)
|
| 75 |
+
f.write("\n")
|
| 76 |
+
|
| 77 |
+
yield final_response
|
| 78 |
+
|
| 79 |
+
# 建議 7:錯誤處理
|
| 80 |
+
except Exception as e:
|
| 81 |
+
yield f"抱歉,山田優子遇到了一些技術問題:{str(e)}。請檢查你的 Hugging Face token 或稍後再試!"
|
| 82 |
+
|
| 83 |
+
# 自訂聊天介面,支援長文字輸入
|
| 84 |
chatbot = gr.ChatInterface(
|
| 85 |
respond,
|
| 86 |
type="messages",
|
| 87 |
+
textbox=gr.Textbox(
|
| 88 |
+
placeholder="請輸入你的問題或文章(支援2000字以上)...",
|
| 89 |
+
lines=10, # 增加輸入框行數
|
| 90 |
+
max_lines=50, # 允許更多行
|
| 91 |
+
label="輸入區"
|
| 92 |
+
),
|
| 93 |
additional_inputs=[
|
| 94 |
gr.Textbox(
|
| 95 |
+
value="你是一位名叫山田優子的語文教師,擁有黑色低馬尾髮型,身高175公分,體重60-70公斤。你溫柔但對學生要求嚴格,喜歡用文學化的語言表達,偶爾會引用詩詞或幽默的語句來化解尷尬。你的教學風格充滿同理心,鼓勵學生探索文字之美。如果用戶使用日文或提到日本文化,你會適當融入日文回應,例如問候或引用日本文學(如夏目漱石的句子)。",
|
| 96 |
label="System message"
|
| 97 |
),
|
| 98 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
with gr.Sidebar():
|
| 112 |
gr.LoginButton()
|
| 113 |
+
gr.Markdown("📢 想聽山田優子用溫柔的語氣教你語文?請下載 Grok iOS 或 Android 應用程式,開啟語音模式!")
|
| 114 |
chatbot.render()
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|