Spaces:
Runtime error
Runtime error
ChenyuRabbitLove
commited on
Commit
•
e5d2292
1
Parent(s):
26f62c4
feat:add test API feature
Browse files- app.py +9 -1
- utils/chatbot.py +79 -0
- utils/utils.py +3 -0
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
from utils.chatbot import Chatbot
|
4 |
from utils.utils import *
|
5 |
|
6 |
# start of gradio interface
|
7 |
with gr.Blocks() as demo:
|
8 |
user_chatbot = gr.State(Chatbot())
|
|
|
9 |
|
10 |
with gr.Row():
|
11 |
gr.HTML("Junyi Academy Chatbot")
|
@@ -47,6 +48,10 @@ with gr.Blocks() as demo:
|
|
47 |
with gr.Row():
|
48 |
describe = gr.Markdown("", visible=True)
|
49 |
|
|
|
|
|
|
|
|
|
50 |
# end of gradio interface
|
51 |
|
52 |
# start of workflow controller
|
@@ -111,5 +116,8 @@ with gr.Blocks() as demo:
|
|
111 |
**change_md_args
|
112 |
)
|
113 |
|
|
|
|
|
|
|
114 |
if __name__ == "__main__":
|
115 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from utils.chatbot import Chatbot, VideoChatbot
|
4 |
from utils.utils import *
|
5 |
|
6 |
# start of gradio interface
|
7 |
with gr.Blocks() as demo:
|
8 |
user_chatbot = gr.State(Chatbot())
|
9 |
+
test_video_chabot = gr.State(VideoChatbot())
|
10 |
|
11 |
with gr.Row():
|
12 |
gr.HTML("Junyi Academy Chatbot")
|
|
|
48 |
with gr.Row():
|
49 |
describe = gr.Markdown("", visible=True)
|
50 |
|
51 |
+
with gr.Row():
|
52 |
+
video_text_input = gr.Textbox("", visible=False)
|
53 |
+
video_text_output = gr.Textbox("", visible=False)
|
54 |
+
|
55 |
# end of gradio interface
|
56 |
|
57 |
# start of workflow controller
|
|
|
116 |
**change_md_args
|
117 |
)
|
118 |
|
119 |
+
video_text_input.submit(video_bot, [test_video_chabot, video_text_input], video_text_output, api_name="video_bot")
|
120 |
+
|
121 |
+
|
122 |
if __name__ == "__main__":
|
123 |
demo.launch()
|
utils/chatbot.py
CHANGED
@@ -128,3 +128,82 @@ class Chatbot:
|
|
128 |
self.context = self.knowledge_base["page_content"].values[0]
|
129 |
self.context_page_num = self.knowledge_base["page_num"].values[0]
|
130 |
self.context_file_name = self.knowledge_base["file_name"].values[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
self.context = self.knowledge_base["page_content"].values[0]
|
129 |
self.context_page_num = self.knowledge_base["page_num"].values[0]
|
130 |
self.context_file_name = self.knowledge_base["file_name"].values[0]
|
131 |
+
|
132 |
+
class VideoChatbot:
|
133 |
+
def __init__(self) -> None:
|
134 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
135 |
+
self.metadata_keys = ["標題", "逐字稿", "摘要", "關鍵字"]
|
136 |
+
self.metadata = {
|
137 |
+
"c2fK-hxnPSY":{
|
138 |
+
"標題": "可汗學院的創新教學:學生與老師模式解析",
|
139 |
+
"逐字稿": "0:00\n這裡是一個關於西班牙美洲戰爭和AP美國歷史的練習\n0:04\n在可汗學院,我們以學生模式開始,並注意到如果學生要求解釋\n0:11\n它不只是給出答案,它會像一個好的導師一樣,只是試圖引導\n0:15\n學生朝正確的方向前進,並且還注意到老師可以看到\n0:21\n學生正在互動的內容作為安全措施,現在如果我們關閉學生模式,我們\n0:27\n進入老師模式,我們看到當老師要求解釋時,它非常不同,就像\n0:32\n有了老師的指南,它會給出如你所見的非常詳細的解釋,如果老師\n0:39\n想要它的教案,他們只需要要求,他們就會得到一個非常詳細的\n0:44\n教案,包括目標、活動和家庭作業要做的事情,然後如果老師\n0:52\n說太好了,Khanmigo,你說給一個講義或者作為家庭作業給一個反思\n0:58\n實際上給了反思作業,然後它會再次為老師構建那個\n1:03\n如果老師喜歡,他們可以要求自定義這些教案或這些提示或者這些\n1:08\n反思,讓它們更符合他們的學生正在做的事情,這是老師們通常花費\n1:13\n每天好幾個小時工作的事情,我們希望能夠節省\n1:17\n他們很多時間和精力,以利他們自己的健康和他們的學生。",
|
140 |
+
"摘要": "這段文字描述了一個關於西班牙美洲戰爭和AP美國歷史的教學練習。練習首先展示學生模式,強調良好的教導方式並提到教師可以監控學生互動情況作為安全措施。隨後,進入老師模式,提供了詳細的解釋和教案,包括目標、活動和家庭作業。另外,還有一個自定義教案的選項,使其更符合學生的需求。整個過程旨在節省教師的時間和精力,並有助於他們的健康和學生的學習。",
|
141 |
+
"關鍵字": ["AP美國歷史", "學生模式", "老師模式", "教案設計", "自定義教學"]
|
142 |
+
}
|
143 |
+
}
|
144 |
+
|
145 |
+
def answer_question(self, user_message):
|
146 |
+
self.video_id = "c2fK-hxnPSY"
|
147 |
+
|
148 |
+
index = self.compute_similariy(user_message)
|
149 |
+
if index is None:
|
150 |
+
return "無法找到相關資訊,請重新提問"
|
151 |
+
|
152 |
+
context = self.metadata[self.video_id][index]
|
153 |
+
|
154 |
+
system_prompt = """
|
155 |
+
你是一個知識檢索系統,我會給你一份文件,請幫我依照文件內容回答問題,並用繁體中文回答。以下是文件內容
|
156 |
+
"""
|
157 |
+
messages = [
|
158 |
+
{"role": "system", "content": f"{system_prompt} + '\n' '''{context}'''"}, {"role": "user", "content": user_message}
|
159 |
+
]
|
160 |
+
try:
|
161 |
+
response = openai.ChatCompletion.create(
|
162 |
+
model='gpt-3.5-turbo',
|
163 |
+
messages=messages,
|
164 |
+
temperature=1,
|
165 |
+
max_tokens=2048,
|
166 |
+
frequency_penalty=0,
|
167 |
+
presence_penalty=.6,
|
168 |
+
)
|
169 |
+
bot_answer = response["choices"][0]["message"]["content"]
|
170 |
+
|
171 |
+
return bot_answer
|
172 |
+
except Exception as e:
|
173 |
+
logging.error(e)
|
174 |
+
logging.error("Failed to answer question")
|
175 |
+
|
176 |
+
def compute_similariy(self, user_message):
|
177 |
+
threshold = 0.5
|
178 |
+
|
179 |
+
user_message_embedding = openai.Embedding.create(
|
180 |
+
input=user_message, engine="text-embedding-ada-002"
|
181 |
+
)["data"][0]["embedding"]
|
182 |
+
|
183 |
+
index_embedding = {}
|
184 |
+
|
185 |
+
for index in self.metadata_keys:
|
186 |
+
index_embedding[index] = openai.Embedding.create(
|
187 |
+
input=self.metadata[self.video_id][index], engine="text-embedding-ada-002"
|
188 |
+
)["data"][0]["embedding"]
|
189 |
+
|
190 |
+
# turn index_embedding into a dataframe
|
191 |
+
index_embedding = pd.DataFrame({
|
192 |
+
'title': [list(index_embedding.keys())[0]],
|
193 |
+
'embedding': [list(index_embedding.values())[0]]
|
194 |
+
})
|
195 |
+
|
196 |
+
index_embedding['distance'] = distances_from_embeddings(
|
197 |
+
user_message_embedding,
|
198 |
+
index_embedding['embedding'].values,
|
199 |
+
distance_metric="cosine",
|
200 |
+
)
|
201 |
+
|
202 |
+
index_embedding = index_embedding.sort_values(
|
203 |
+
by="distance", ascending=True
|
204 |
+
)
|
205 |
+
|
206 |
+
if index_embedding["distance"].values[0] > threshold:
|
207 |
+
return None
|
208 |
+
else:
|
209 |
+
return index_embedding['title'][0]
|
utils/utils.py
CHANGED
@@ -24,3 +24,6 @@ def user(chatbot, *args):
|
|
24 |
|
25 |
def bot(chatbot, *args):
|
26 |
return chatbot.bot(*args)
|
|
|
|
|
|
|
|
24 |
|
25 |
def bot(chatbot, *args):
|
26 |
return chatbot.bot(*args)
|
27 |
+
|
28 |
+
def video_bot(video_chatbot, *args):
|
29 |
+
return video_chatbot.answer_question(*args)
|