Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,8 @@ from helpers import (
|
|
10 |
list_docx_files, get_splits, get_json_splits_only, get_answer
|
11 |
)
|
12 |
|
|
|
|
|
13 |
os.environ["USER_AGENT"] = "gradio-hf-space"
|
14 |
|
15 |
if "GOOGLE_API_KEY" not in os.environ:
|
@@ -20,6 +22,70 @@ key = "AIzaSyDJ4vIKuIBIPNHATLxnoHlagXWbsAz-vRs"
|
|
20 |
|
21 |
# Cấu hình API key cho Google GenAI
|
22 |
genai.configure(api_key=key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
institutions = ['Tất cả'] + ['Trường Công Nghệ']
|
24 |
categories = ['Tất cả'] + ['Đề án', 'Chương trình đào tạo']
|
25 |
|
@@ -39,4 +105,4 @@ with gr.Blocks() as demo:
|
|
39 |
)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
-
demo.launch(
|
|
|
10 |
list_docx_files, get_splits, get_json_splits_only, get_answer
|
11 |
)
|
12 |
|
13 |
+
from file_loader import get_vectorstore
|
14 |
+
|
15 |
os.environ["USER_AGENT"] = "gradio-hf-space"
|
16 |
|
17 |
if "GOOGLE_API_KEY" not in os.environ:
|
|
|
22 |
|
23 |
# Cấu hình API key cho Google GenAI
|
24 |
genai.configure(api_key=key)
|
25 |
+
|
26 |
+
vectorstore = get_vectorstore()
|
27 |
+
# Define the augment_prompt function
|
28 |
+
def augment_prompt(query: str, k: int = 10):
|
29 |
+
queries = []
|
30 |
+
queries.append(query)
|
31 |
+
|
32 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": k})
|
33 |
+
results = retriever.invoke(query)
|
34 |
+
|
35 |
+
if results:
|
36 |
+
source_knowledge = "\n\n".join([doc.page_content for doc in results])
|
37 |
+
return f"""Using the contexts below, answer the query.
|
38 |
+
Contexts:
|
39 |
+
{source_knowledge}
|
40 |
+
"""
|
41 |
+
else:
|
42 |
+
return f"No relevant context found.\n."
|
43 |
+
|
44 |
+
def get_answer(query, queries_list=None):
|
45 |
+
if queries_list is None:
|
46 |
+
queries_list = []
|
47 |
+
|
48 |
+
messages = [
|
49 |
+
{"role": "user", "parts": [{"text": "IMPORTANT: You are a super energetic, helpful, polite, Vietnamese-speaking assistant. If you can not see the answer in contexts, try to search it up online by yourself but remember to give the source."}]},
|
50 |
+
{"role": "user", "parts": [{"text": augment_prompt(query)}]}
|
51 |
+
]
|
52 |
+
# bonus = '''
|
53 |
+
# Bạn tham kháo thêm các nguồn thông tin tại:
|
54 |
+
# Trang thông tin điện tử: https://neu.edu.vn ; https://daotao.neu.edu.vn
|
55 |
+
# Trang mạng xã hội có thông tin tuyển sinh: https://www.facebook.com/ktqdNEU ; https://www.facebook.com/tvtsneu ;
|
56 |
+
# Email tuyển sinh: [email protected]
|
57 |
+
# Số điện thoại tuyển sinh: 0888.128.558
|
58 |
+
# '''
|
59 |
+
|
60 |
+
queries_list.append(query)
|
61 |
+
queries = {"role": "user", "parts": [{"text": prompt_order(queries_list)}]}
|
62 |
+
messages_with_queries = messages.copy()
|
63 |
+
messages_with_queries.append(queries)
|
64 |
+
# messages_with_queries.insert(0, queries)
|
65 |
+
|
66 |
+
# Configure API key
|
67 |
+
genai.configure(api_key=key)
|
68 |
+
|
69 |
+
# Initialize the Gemini model
|
70 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
71 |
+
|
72 |
+
response = model.generate_content(contents=messages_with_queries, stream=True)
|
73 |
+
response_text = ""
|
74 |
+
|
75 |
+
for chunk in response:
|
76 |
+
response_text += chunk.text
|
77 |
+
yield response_text
|
78 |
+
|
79 |
+
messages.append({"role": "model", "parts": [{"text": response_text}]})
|
80 |
+
|
81 |
+
# user_feedback = yield "\nNhập phản hồi của bạn (hoặc nhập 'q' để thoát): "
|
82 |
+
# if user_feedback.lower() == "q":
|
83 |
+
# break
|
84 |
+
|
85 |
+
# messages.append({"role": "user", "parts": [{"text": query}]})
|
86 |
+
|
87 |
+
log_message(messages)
|
88 |
+
|
89 |
institutions = ['Tất cả'] + ['Trường Công Nghệ']
|
90 |
categories = ['Tất cả'] + ['Đề án', 'Chương trình đào tạo']
|
91 |
|
|
|
105 |
)
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
+
demo.launch()
|