Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,14 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import json
|
4 |
import requests
|
5 |
-
from typing import List,
|
6 |
|
7 |
# 環境變數設置
|
8 |
XAI_API_KEY = os.getenv("XAI_API_KEY")
|
9 |
if not XAI_API_KEY:
|
10 |
raise ValueError("Please set XAI_API_KEY environment variable")
|
11 |
|
12 |
-
def call_grok_api(messages: List[
|
13 |
"""調用Grok API"""
|
14 |
headers = {
|
15 |
"Content-Type": "application/json",
|
@@ -20,7 +20,7 @@ def call_grok_api(messages: List[Dict]) -> str:
|
|
20 |
"messages": messages,
|
21 |
"model": "grok-beta",
|
22 |
"stream": False,
|
23 |
-
"temperature": 0.7
|
24 |
}
|
25 |
|
26 |
try:
|
@@ -34,8 +34,8 @@ def call_grok_api(messages: List[Dict]) -> str:
|
|
34 |
except requests.exceptions.RequestException as e:
|
35 |
return f"Error calling Grok API: {str(e)}"
|
36 |
|
37 |
-
def format_history(history: List[
|
38 |
-
"""
|
39 |
formatted_messages = [
|
40 |
{
|
41 |
"role": "system",
|
@@ -43,27 +43,30 @@ def format_history(history: List[Dict]) -> List[Dict]:
|
|
43 |
}
|
44 |
]
|
45 |
|
46 |
-
for
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
return formatted_messages
|
53 |
|
54 |
-
def respond(message: str, history: List[
|
55 |
"""處理用戶輸入並生成回應"""
|
56 |
-
|
57 |
-
|
58 |
-
# 格式化消息歷史
|
59 |
-
formatted_history = format_history(history)
|
60 |
|
61 |
# 獲取Grok的回應
|
62 |
-
response = call_grok_api(
|
63 |
-
|
64 |
-
# 添加助手回應到歷史記錄
|
65 |
-
history.append({"role": "assistant", "content": response})
|
66 |
|
|
|
|
|
67 |
return history
|
68 |
|
69 |
# 創建Gradio界面
|
@@ -75,8 +78,6 @@ with gr.Blocks() as demo:
|
|
75 |
|
76 |
chatbot = gr.Chatbot(
|
77 |
value=[],
|
78 |
-
bubble_full_width=False,
|
79 |
-
avatar_images=(None, "🤖"),
|
80 |
height=500
|
81 |
)
|
82 |
|
|
|
2 |
import os
|
3 |
import json
|
4 |
import requests
|
5 |
+
from typing import List, Tuple
|
6 |
|
7 |
# 環境變數設置
|
8 |
XAI_API_KEY = os.getenv("XAI_API_KEY")
|
9 |
if not XAI_API_KEY:
|
10 |
raise ValueError("Please set XAI_API_KEY environment variable")
|
11 |
|
12 |
+
def call_grok_api(messages: List[dict]) -> str:
|
13 |
"""調用Grok API"""
|
14 |
headers = {
|
15 |
"Content-Type": "application/json",
|
|
|
20 |
"messages": messages,
|
21 |
"model": "grok-beta",
|
22 |
"stream": False,
|
23 |
+
"temperature": 0.7
|
24 |
}
|
25 |
|
26 |
try:
|
|
|
34 |
except requests.exceptions.RequestException as e:
|
35 |
return f"Error calling Grok API: {str(e)}"
|
36 |
|
37 |
+
def format_history(history: List[Tuple[str, str]]) -> List[dict]:
|
38 |
+
"""將聊天歷史格式化為Grok API需要的格式"""
|
39 |
formatted_messages = [
|
40 |
{
|
41 |
"role": "system",
|
|
|
43 |
}
|
44 |
]
|
45 |
|
46 |
+
for user_msg, assistant_msg in history:
|
47 |
+
if user_msg:
|
48 |
+
formatted_messages.append({
|
49 |
+
"role": "user",
|
50 |
+
"content": user_msg
|
51 |
+
})
|
52 |
+
if assistant_msg:
|
53 |
+
formatted_messages.append({
|
54 |
+
"role": "assistant",
|
55 |
+
"content": assistant_msg
|
56 |
+
})
|
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)])
|
|
|
|
|
64 |
|
65 |
# 獲取Grok的回應
|
66 |
+
response = call_grok_api(history_for_api)
|
|
|
|
|
|
|
67 |
|
68 |
+
# 返回更新後的歷史記錄
|
69 |
+
history.append((message, response))
|
70 |
return history
|
71 |
|
72 |
# 創建Gradio界面
|
|
|
78 |
|
79 |
chatbot = gr.Chatbot(
|
80 |
value=[],
|
|
|
|
|
81 |
height=500
|
82 |
)
|
83 |
|