Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from typing import List, Dict
|
| 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",
|
| 16 |
+
"Authorization": f"Bearer {XAI_API_KEY}"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
data = {
|
| 20 |
+
"messages": messages,
|
| 21 |
+
"model": "grok-beta",
|
| 22 |
+
"stream": False,
|
| 23 |
+
"temperature": 0.7 # 可以調整此值來控制回答的創造性
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(
|
| 28 |
+
"https://api.x.ai/v1/chat/completions",
|
| 29 |
+
headers=headers,
|
| 30 |
+
json=data
|
| 31 |
+
)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 34 |
+
except requests.exceptions.RequestException as e:
|
| 35 |
+
return f"Error calling Grok API: {str(e)}"
|
| 36 |
+
|
| 37 |
+
def format_history(history: List[Dict]) -> List[Dict]:
|
| 38 |
+
"""將Gradio的歷史記錄格式化為Grok API需要的格式"""
|
| 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 |
+
|
| 46 |
+
for msg in history:
|
| 47 |
+
formatted_messages.append({
|
| 48 |
+
"role": "user" if msg["role"] == "user" else "assistant",
|
| 49 |
+
"content": msg["content"]
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
return formatted_messages
|
| 53 |
+
|
| 54 |
+
def respond(message: str, history: List[Dict]) -> Dict:
|
| 55 |
+
"""處理用戶輸入並生成回應"""
|
| 56 |
+
history.append({"role": "user", "content": message})
|
| 57 |
+
|
| 58 |
+
# 格式化消息歷史
|
| 59 |
+
formatted_history = format_history(history)
|
| 60 |
+
|
| 61 |
+
# 獲取Grok的回應
|
| 62 |
+
response = call_grok_api(formatted_history)
|
| 63 |
+
|
| 64 |
+
# 添加助手回應到歷史記錄
|
| 65 |
+
history.append({"role": "assistant", "content": response})
|
| 66 |
+
|
| 67 |
+
return history
|
| 68 |
+
|
| 69 |
+
# 創建Gradio界面
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("""
|
| 72 |
+
# Grok Chatbot
|
| 73 |
+
與Elon Musk的Grok AI聊天!這個聊天機器人使用了xAI的Grok API。
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
chatbot = gr.Chatbot(
|
| 77 |
+
value=[],
|
| 78 |
+
bubble_full_width=False,
|
| 79 |
+
avatar_images=(None, "🤖"),
|
| 80 |
+
height=500
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
msg = gr.Textbox(
|
| 84 |
+
placeholder="輸入訊息...",
|
| 85 |
+
container=False,
|
| 86 |
+
scale=7
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
clear = gr.ClearButton([msg, chatbot], variant="secondary", scale=1)
|
| 90 |
+
|
| 91 |
+
msg.submit(respond, [msg, chatbot], [chatbot], api_name="chat")
|
| 92 |
+
|
| 93 |
+
demo.queue()
|
| 94 |
+
|
| 95 |
+
# 啟動應用
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|