DeepLearning101
commited on
Commit
•
537576e
1
Parent(s):
6ca9070
Update app.py
Browse files
app.py
CHANGED
@@ -6,21 +6,21 @@ import json
|
|
6 |
from datasets import Dataset, DatasetDict, load_dataset, load_from_disk
|
7 |
from huggingface_hub import HfApi, HfFolder
|
8 |
|
9 |
-
#
|
10 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
|
11 |
LLM_API = os.environ.get("LLM_API")
|
12 |
LLM_URL = os.environ.get("LLM_URL")
|
13 |
USER_ID = "HuggingFace Space"
|
14 |
DATASET_NAME = os.environ.get("DATASET_NAME")
|
15 |
|
16 |
-
#
|
17 |
if HF_API_TOKEN is None:
|
18 |
-
raise ValueError("HF_API_TOKEN
|
19 |
|
20 |
-
#
|
21 |
HfFolder.save_token(HF_API_TOKEN)
|
22 |
|
23 |
-
#
|
24 |
try:
|
25 |
dataset = load_dataset(DATASET_NAME)
|
26 |
except:
|
@@ -96,14 +96,18 @@ def save_feedback(user_input, response, feedback_type, improvement):
|
|
96 |
dataset.push_to_hub(DATASET_NAME)
|
97 |
|
98 |
def handle_feedback(response, feedback_type, improvement):
|
99 |
-
|
100 |
-
|
|
|
|
|
101 |
|
102 |
def handle_user_input(user_input):
|
103 |
print(f"User input: {user_input}")
|
|
|
|
|
104 |
return run_sync(user_input)
|
105 |
|
106 |
-
#
|
107 |
def show_feedback():
|
108 |
try:
|
109 |
feedbacks = dataset["feedback"].to_pandas().to_dict(orient="records")
|
@@ -154,4 +158,10 @@ with iface:
|
|
154 |
|
155 |
show_feedback_button.click(fn=show_feedback, outputs=feedback_display)
|
156 |
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from datasets import Dataset, DatasetDict, load_dataset, load_from_disk
|
7 |
from huggingface_hub import HfApi, HfFolder
|
8 |
|
9 |
+
# 從環境變量中獲取 Hugging Face API 令牌和其他配置
|
10 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
|
11 |
LLM_API = os.environ.get("LLM_API")
|
12 |
LLM_URL = os.environ.get("LLM_URL")
|
13 |
USER_ID = "HuggingFace Space"
|
14 |
DATASET_NAME = os.environ.get("DATASET_NAME")
|
15 |
|
16 |
+
# 確保令牌不為空
|
17 |
if HF_API_TOKEN is None:
|
18 |
+
raise ValueError("HF_API_TOKEN 環境變量未設置。請在 Hugging Face Space 的設置中添加該環境變量。")
|
19 |
|
20 |
+
# 設置 Hugging Face API 令牌
|
21 |
HfFolder.save_token(HF_API_TOKEN)
|
22 |
|
23 |
+
# 加載或創建數據集
|
24 |
try:
|
25 |
dataset = load_dataset(DATASET_NAME)
|
26 |
except:
|
|
|
96 |
dataset.push_to_hub(DATASET_NAME)
|
97 |
|
98 |
def handle_feedback(response, feedback_type, improvement):
|
99 |
+
# 獲取最新的用戶輸入(假設用戶輸入保存在全局變量中)
|
100 |
+
global last_user_input
|
101 |
+
save_feedback(last_user_input, response, feedback_type, improvement)
|
102 |
+
return "感謝您的反饋!"
|
103 |
|
104 |
def handle_user_input(user_input):
|
105 |
print(f"User input: {user_input}")
|
106 |
+
global last_user_input
|
107 |
+
last_user_input = user_input # 保存最新的用戶輸入
|
108 |
return run_sync(user_input)
|
109 |
|
110 |
+
# 讀取並顯示反饋內容的函數
|
111 |
def show_feedback():
|
112 |
try:
|
113 |
feedbacks = dataset["feedback"].to_pandas().to_dict(orient="records")
|
|
|
158 |
|
159 |
show_feedback_button.click(fn=show_feedback, outputs=feedback_display)
|
160 |
|
161 |
+
# 添加示例
|
162 |
+
examples = [
|
163 |
+
["AlCoCrFeNi HEA coating 可用怎樣的實驗方法做到 ?"],
|
164 |
+
["請問high entropy nitride coatings的形成,主要可透過那些元素來熱這個材料形成熱穩定?"]
|
165 |
+
]
|
166 |
+
|
167 |
+
iface.launch(examples=examples)
|