Browen0311 commited on
Commit
003f2e8
·
verified ·
1 Parent(s): d4bb78b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -39,7 +39,9 @@ def format_history(history: List[Tuple[str, str]]) -> List[dict]:
39
  formatted_messages = [
40
  {
41
  "role": "system",
42
- "content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy."
 
 
43
  }
44
  ]
45
 
@@ -57,7 +59,7 @@ def format_history(history: List[Tuple[str, str]]) -> List[dict]:
57
 
58
  return formatted_messages
59
 
60
- def respond(message: str, history: List[Tuple[str, str]]) -> List[Tuple[str, str]]:
61
  """處理用戶輸入並生成回應"""
62
  # 格式化歷史記錄並添加新消息
63
  history_for_api = format_history(history + [(message, None)])
@@ -65,31 +67,35 @@ def respond(message: str, history: List[Tuple[str, str]]) -> List[Tuple[str, str
65
  # 獲取Grok的回應
66
  response = call_grok_api(history_for_api)
67
 
68
- # 返回更新後的歷史記錄
69
  history.append((message, response))
70
- return history
71
 
72
  # 創建Gradio界面
73
  with gr.Blocks() as demo:
74
  gr.Markdown("""
75
- # Grok Chatbot
76
- 與Elon Musk的Grok AI聊天!這個聊天機器人使用了xAI的Grok API。
77
  """)
78
 
79
  chatbot = gr.Chatbot(
80
  value=[],
81
- height=500
 
 
82
  )
83
 
84
- msg = gr.Textbox(
85
- placeholder="輸入訊息...",
86
- container=False,
87
- scale=7
88
- )
89
-
90
- clear = gr.ClearButton([msg, chatbot], variant="secondary", scale=1)
 
91
 
92
- msg.submit(respond, [msg, chatbot], [chatbot], api_name="chat")
 
93
 
94
  demo.queue()
95
 
 
39
  formatted_messages = [
40
  {
41
  "role": "system",
42
+ "content": """You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy.
43
+ Please respond in Traditional Chinese (繁體中文).
44
+ Keep your responses natural and conversational while maintaining Traditional Chinese characters."""
45
  }
46
  ]
47
 
 
59
 
60
  return formatted_messages
61
 
62
+ def respond(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
63
  """處理用戶輸入並生成回應"""
64
  # 格式化歷史記錄並添加新消息
65
  history_for_api = format_history(history + [(message, None)])
 
67
  # 獲取Grok的回應
68
  response = call_grok_api(history_for_api)
69
 
70
+ # 返回更新後的歷史記錄和空字符串(用於清除輸入框)
71
  history.append((message, response))
72
+ return "", history
73
 
74
  # 創建Gradio界面
75
  with gr.Blocks() as demo:
76
  gr.Markdown("""
77
+ # Grok 聊天機器人
78
+ Elon Musk Grok AI 聊天!這個聊天機器人使用了 xAI Grok API。
79
  """)
80
 
81
  chatbot = gr.Chatbot(
82
  value=[],
83
+ height=500,
84
+ bubble_full_width=False,
85
+ show_copy_button=True,
86
  )
87
 
88
+ with gr.Row():
89
+ msg = gr.Textbox(
90
+ placeholder="輸入訊息...",
91
+ container=False,
92
+ scale=7,
93
+ autofocus=True, # 自動聚焦到輸入框
94
+ )
95
+ clear = gr.ClearButton([msg, chatbot], variant="secondary", scale=1)
96
 
97
+ # 注意這裡輸出添加了msg,用於清除輸入框
98
+ msg.submit(respond, [msg, chatbot], [msg, chatbot], api_name="chat")
99
 
100
  demo.queue()
101