Spaces:
Sleeping
Sleeping
Marathon23
commited on
Upload 2 files
Browse files- app (2).py +74 -0
- requirements (2).txt +3 -0
app (2).py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
import os
|
5 |
+
import requests
|
6 |
+
import json
|
7 |
+
|
8 |
+
# 從 Hugging Face secrets 中讀取 OpenAI API 金鑰
|
9 |
+
api_key = os.getenv('OPENAI_API_KEY')
|
10 |
+
if not api_key:
|
11 |
+
raise ValueError("請設置 'OPENAI_API_KEY' 環境變數")
|
12 |
+
|
13 |
+
# OpenAI API key
|
14 |
+
openai_api_key = api_key
|
15 |
+
|
16 |
+
# 將 Gradio 的歷史紀錄轉換為 OpenAI 格式
|
17 |
+
def transform_history(history):
|
18 |
+
new_history = []
|
19 |
+
for chat in history:
|
20 |
+
new_history.append({"role": "user", "content": chat[0]})
|
21 |
+
new_history.append({"role": "assistant", "content": chat[1]})
|
22 |
+
return new_history
|
23 |
+
|
24 |
+
# 回應生成函數,使用 requests 來呼叫 OpenAI API
|
25 |
+
def response(message, history):
|
26 |
+
global conversation_history
|
27 |
+
|
28 |
+
# 將 Gradio 的歷史紀錄轉換為 OpenAI 的格式
|
29 |
+
conversation_history = transform_history(history)
|
30 |
+
|
31 |
+
url = "https://api.openai.com/v1/chat/completions"
|
32 |
+
headers = {
|
33 |
+
"Content-Type": "application/json",
|
34 |
+
"Authorization": f"Bearer {openai_api_key}"
|
35 |
+
}
|
36 |
+
|
37 |
+
# 設置初始的 prompt_instruction
|
38 |
+
prompt_instruction = """
|
39 |
+
你是廖老師的專業小助教,名字叫做 '小確' ,要以專業、熱情、善解人意且非常有禮貌,親切的的口氣,與用戶互動並解答問題:
|
40 |
+
"""
|
41 |
+
prompt_to_gpt = prompt_instruction + message
|
42 |
+
|
43 |
+
# 新增至 conversation_history
|
44 |
+
conversation_history.append({"role": "system", "content": prompt_to_gpt})
|
45 |
+
|
46 |
+
# 設置請求的數據
|
47 |
+
data = {
|
48 |
+
"model": "gpt-4o", # 確認使用的模型是 gpt-4 或 gpt-3.5-turbo
|
49 |
+
"messages": conversation_history,
|
50 |
+
"max_tokens": 200 # 控制生成的最大令牌數
|
51 |
+
}
|
52 |
+
|
53 |
+
# 發送請求到 OpenAI API
|
54 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
55 |
+
|
56 |
+
# 處理回應
|
57 |
+
response_json = response.json()
|
58 |
+
|
59 |
+
# 提取模型的回應並加入歷史紀錄
|
60 |
+
if 'choices' in response_json and len(response_json['choices']) > 0:
|
61 |
+
model_response = response_json['choices'][0]['message']['content']
|
62 |
+
conversation_history.append({"role": "assistant", "content": model_response})
|
63 |
+
|
64 |
+
# 逐字回傳生成的文字,實現打字機效果
|
65 |
+
for i in range(len(model_response)):
|
66 |
+
time.sleep(0.05) # 每個字符間隔 0.05 秒
|
67 |
+
yield model_response[: i+1]
|
68 |
+
else:
|
69 |
+
yield "Error: No response from the model."
|
70 |
+
|
71 |
+
# 建立 Gradio 聊天界面
|
72 |
+
gr.ChatInterface(response,
|
73 |
+
title='OpenAI Chat',
|
74 |
+
textbox=gr.Textbox(placeholder="Question to OpenAI")).launch(share=True)
|
requirements (2).txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
#requirements.txt
|
2 |
+
gradio
|
3 |
+
openai
|