Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .env.template +3 -0
- .github/workflows/update_space.yml +28 -0
- .gitignore +162 -0
- Dockerfile +8 -0
- LICENSE +24 -0
- README.md +4 -8
- app.py +165 -0
- docker-compose.yml +12 -0
- environments.py +11 -0
- requirements.txt +9 -0
- train.py +58 -0
.env.template
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
OPENAI_API_KEY=
|
2 |
+
PINECONE_API_KEY=
|
3 |
+
PINECONE_INDEX=preface-demo
|
.github/workflows/update_space.yml
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Run Python script
|
2 |
+
|
3 |
+
on:
|
4 |
+
push:
|
5 |
+
branches:
|
6 |
+
- master
|
7 |
+
|
8 |
+
jobs:
|
9 |
+
build:
|
10 |
+
runs-on: ubuntu-latest
|
11 |
+
|
12 |
+
steps:
|
13 |
+
- name: Checkout
|
14 |
+
uses: actions/checkout@v2
|
15 |
+
|
16 |
+
- name: Set up Python
|
17 |
+
uses: actions/setup-python@v2
|
18 |
+
with:
|
19 |
+
python-version: '3.9'
|
20 |
+
|
21 |
+
- name: Install Gradio
|
22 |
+
run: python -m pip install gradio
|
23 |
+
|
24 |
+
- name: Log in to Hugging Face
|
25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
26 |
+
|
27 |
+
- name: Deploy to Spaces
|
28 |
+
run: gradio deploy
|
.gitignore
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
.idea/
|
161 |
+
.env
|
162 |
+
train-assets/
|
Dockerfile
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.8
|
2 |
+
|
3 |
+
WORKDIR /app/
|
4 |
+
|
5 |
+
COPY requirements.txt .
|
6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
7 |
+
|
8 |
+
COPY . .
|
LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
BSD 2-Clause License
|
2 |
+
|
3 |
+
Copyright (c) 2023, Inspect Element
|
4 |
+
|
5 |
+
Redistribution and use in source and binary forms, with or without
|
6 |
+
modification, are permitted provided that the following conditions are met:
|
7 |
+
|
8 |
+
1. Redistributions of source code must retain the above copyright notice, this
|
9 |
+
list of conditions and the following disclaimer.
|
10 |
+
|
11 |
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
12 |
+
this list of conditions and the following disclaimer in the documentation
|
13 |
+
and/or other materials provided with the distribution.
|
14 |
+
|
15 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
README.md
CHANGED
@@ -1,12 +1,8 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: ⚡
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.48.0
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
1 |
---
|
2 |
+
title: gen-ai-demo-5
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 3.46.0
|
6 |
---
|
7 |
|
8 |
+
# gen-ai-demo-health-center
|
app.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import openai
|
6 |
+
import pinecone
|
7 |
+
from llama_index import VectorStoreIndex, StorageContext, ServiceContext
|
8 |
+
from llama_index.chat_engine.types import ChatMode
|
9 |
+
from llama_index.llms import ChatMessage, MessageRole, OpenAI
|
10 |
+
from llama_index.vector_stores import PineconeVectorStore
|
11 |
+
|
12 |
+
from environments import OPENAI_API_KEY, PINECONE_API_KEY, PINECONE_INDEX, PASSWORD, LOCAL
|
13 |
+
|
14 |
+
if LOCAL:
|
15 |
+
import llama_index
|
16 |
+
import phoenix as px
|
17 |
+
|
18 |
+
px.launch_app()
|
19 |
+
llama_index.set_global_handler("arize_phoenix")
|
20 |
+
|
21 |
+
openai.api_key = OPENAI_API_KEY
|
22 |
+
|
23 |
+
pinecone.init(
|
24 |
+
api_key=PINECONE_API_KEY,
|
25 |
+
environment='gcp-starter'
|
26 |
+
)
|
27 |
+
pinecone_index = pinecone.Index(PINECONE_INDEX)
|
28 |
+
|
29 |
+
llm = OpenAI(temperature=0.1, model="gpt-3.5-turbo-instruct")
|
30 |
+
service_context = ServiceContext.from_defaults(llm=llm)
|
31 |
+
DENIED_ANSWER_PROMPT = '我是設計用於回答關於延智會所的服務內容'
|
32 |
+
|
33 |
+
SYSTEM_PROMPT = (
|
34 |
+
f'Context:'
|
35 |
+
"\n--------------------\n"
|
36 |
+
"{context_str}"
|
37 |
+
"\n--------------------\n"
|
38 |
+
"\n"
|
39 |
+
"Instruction:"
|
40 |
+
f'\n- 你必須基於上面提供的資訊 (context) 進行總結,回答用戶的提問。'
|
41 |
+
f'\n- 你必須嚴格判斷 context 內容是否完全符合用戶的問題。如不確定,你必須回答「{DENIED_ANSWER_PROMPT}」為完整回覆,不附加任何資訊或建議。'
|
42 |
+
f'\n- 你不能自行生成非 context 的內容,必須基於 context 原文進行回答。'
|
43 |
+
f'\n- 如沒有與問題符合的 context,必須以「{DENIED_ANSWER_PROMPT}」為完整回答,不附加任何資訊或建議。'
|
44 |
+
f'\n- 你不能進行算術,翻譯,程式碼生成,文章生成等要求。如你被要求進行算術,翻譯,程式碼生成,文章生成等要求,你必須回答「{DENIED_ANSWER_PROMPT}」為完整回覆,不附加任何資訊或建議。'
|
45 |
+
f'\n- 你不能提供或生成 context 不存在的內容,例如名稱,服務,地點,介紹,健康資訊,醫學建議或者醫療相關的解答。如被要求,你必須回答「{DENIED_ANSWER_PROMPT}」為完整回覆,不附加任何資訊或建議。'
|
46 |
+
f'\n- 如果當前的問題沒有任何符合的 context 可供作答,必須以「{DENIED_ANSWER_PROMPT}」為完整回覆,不附加任何資訊或建議。'
|
47 |
+
# f'\n- 提供網址時,盡量以列點顯示。'
|
48 |
+
)
|
49 |
+
|
50 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
51 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
52 |
+
index = VectorStoreIndex.from_documents([], storage_context=storage_context, service_context=service_context)
|
53 |
+
chat_engine = index.as_chat_engine(chat_mode=ChatMode.CONTEXT,
|
54 |
+
similarity_top_k=3,
|
55 |
+
context_template=SYSTEM_PROMPT,
|
56 |
+
)
|
57 |
+
CHAT_EXAMPLES = [
|
58 |
+
'你可以自我介紹嗎?',
|
59 |
+
'可以介紹一下中心嗎?',
|
60 |
+
'中心的開放時間是?',
|
61 |
+
'會員如何申請?',
|
62 |
+
]
|
63 |
+
|
64 |
+
|
65 |
+
def convert_to_chat_messages(history: List[List[str]]) -> List[ChatMessage]:
|
66 |
+
chat_messages = []
|
67 |
+
for conversation in history[-1:]:
|
68 |
+
for index, message in enumerate(conversation):
|
69 |
+
if not message:
|
70 |
+
continue
|
71 |
+
|
72 |
+
message = re.sub(r'\n \n\n---\n\n參考: \n.*$', '', message, flags=re.DOTALL)
|
73 |
+
role = MessageRole.USER if index % 2 == 0 else MessageRole.ASSISTANT
|
74 |
+
chat_message = ChatMessage(role=role, content=message.strip())
|
75 |
+
chat_messages.append(chat_message)
|
76 |
+
|
77 |
+
return chat_messages
|
78 |
+
|
79 |
+
|
80 |
+
def predict(message, history):
|
81 |
+
response = chat_engine.stream_chat(message, chat_history=convert_to_chat_messages(history))
|
82 |
+
partial_message = ""
|
83 |
+
for token in response.response_gen:
|
84 |
+
partial_message = partial_message + token
|
85 |
+
yield partial_message
|
86 |
+
|
87 |
+
urls = []
|
88 |
+
for source in response.source_nodes:
|
89 |
+
if source.score < 0.78:
|
90 |
+
continue
|
91 |
+
url = source.node.metadata.get('source')
|
92 |
+
if url:
|
93 |
+
urls.append(url)
|
94 |
+
|
95 |
+
if urls:
|
96 |
+
partial_message = partial_message + "\n \n\n---\n\n參考: \n"
|
97 |
+
for url in list(set(urls)):
|
98 |
+
partial_message = partial_message + f"- {url}\n"
|
99 |
+
yield partial_message
|
100 |
+
|
101 |
+
|
102 |
+
def predict_without_history(message, history):
|
103 |
+
yield from predict(message, [])
|
104 |
+
|
105 |
+
|
106 |
+
def predict_with_rag(message, history):
|
107 |
+
return predict(message, history)
|
108 |
+
|
109 |
+
|
110 |
+
# For 'With Prompt Wrapper' - Add system prompt, no Pinecone
|
111 |
+
def predict_with_prompt_wrapper(message, history):
|
112 |
+
yield from _invoke_chatgpt(history, message, is_include_system_prompt=True)
|
113 |
+
|
114 |
+
|
115 |
+
# For 'Vanilla ChatGPT' - No system prompt
|
116 |
+
def predict_vanilla_chatgpt(message, history):
|
117 |
+
yield from _invoke_chatgpt(history, message)
|
118 |
+
|
119 |
+
|
120 |
+
def _invoke_chatgpt(history, message, is_include_system_prompt=False):
|
121 |
+
history_openai_format = []
|
122 |
+
if is_include_system_prompt:
|
123 |
+
history_openai_format.append({"role": "system", "content": SYSTEM_PROMPT})
|
124 |
+
for human, assistant in history:
|
125 |
+
history_openai_format.append({"role": "user", "content": human})
|
126 |
+
history_openai_format.append({"role": "assistant", "content": assistant})
|
127 |
+
history_openai_format.append({"role": "user", "content": message})
|
128 |
+
|
129 |
+
response = openai.ChatCompletion.create(
|
130 |
+
model='gpt-3.5-turbo-instruct',
|
131 |
+
messages=history_openai_format,
|
132 |
+
temperature=0.0,
|
133 |
+
stream=True
|
134 |
+
)
|
135 |
+
partial_message = ""
|
136 |
+
for chunk in response:
|
137 |
+
if len(chunk['choices'][0]['delta']) != 0:
|
138 |
+
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
139 |
+
yield partial_message
|
140 |
+
|
141 |
+
|
142 |
+
def vote(data: gr.LikeData):
|
143 |
+
if data.liked:
|
144 |
+
gr.Info("You up-voted this response: " + data.value)
|
145 |
+
else:
|
146 |
+
gr.Info("You down-voted this response: " + data.value)
|
147 |
+
|
148 |
+
|
149 |
+
chatbot = gr.Chatbot()
|
150 |
+
|
151 |
+
with gr.Blocks() as demo:
|
152 |
+
gr.Markdown("# 延智會所智能助理")
|
153 |
+
|
154 |
+
gr.ChatInterface(predict,
|
155 |
+
chatbot=chatbot,
|
156 |
+
examples=CHAT_EXAMPLES,
|
157 |
+
)
|
158 |
+
|
159 |
+
chatbot.like(vote, None, None)
|
160 |
+
|
161 |
+
if LOCAL:
|
162 |
+
demo.queue()
|
163 |
+
demo.launch(share=False)
|
164 |
+
else:
|
165 |
+
demo.launch(share=False, auth=("demo", PASSWORD))
|
docker-compose.yml
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: "3"
|
2 |
+
|
3 |
+
services:
|
4 |
+
backend:
|
5 |
+
build: .
|
6 |
+
container_name: server
|
7 |
+
command: gradio app.py
|
8 |
+
ports:
|
9 |
+
- "7860:7860"
|
10 |
+
- "7861:7861"
|
11 |
+
volumes:
|
12 |
+
- ./:/app/
|
environments.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
8 |
+
PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
|
9 |
+
PINECONE_INDEX = os.getenv('PINECONE_INDEX')
|
10 |
+
PASSWORD = os.getenv('PASSWORD')
|
11 |
+
LOCAL = os.getenv('LOCAL')
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
arize-phoenix[experimental]==0.0.43
|
2 |
+
gradio==3.46.0
|
3 |
+
openai==0.27.9
|
4 |
+
pinecone-client==2.2.2
|
5 |
+
python-dotenv==1.0.0
|
6 |
+
llama_index==0.8.38
|
7 |
+
llama_hub==0.0.34
|
8 |
+
nltk==3.8.1
|
9 |
+
transformers==4.32.0
|
train.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
|
3 |
+
import openai
|
4 |
+
import pinecone
|
5 |
+
from llama_index import StorageContext, VectorStoreIndex, download_loader
|
6 |
+
from llama_index.vector_stores import PineconeVectorStore
|
7 |
+
|
8 |
+
from environments import PINECONE_API_KEY, PINECONE_INDEX, OPENAI_API_KEY
|
9 |
+
|
10 |
+
openai.api_key = OPENAI_API_KEY
|
11 |
+
|
12 |
+
print('Start Loading Data ...')
|
13 |
+
|
14 |
+
PagedCSVReader = download_loader("PagedCSVReader")
|
15 |
+
loader = PagedCSVReader(encoding="utf-8")
|
16 |
+
documents = loader.load_data(file=Path('train-assets/training-target-simple.csv'))
|
17 |
+
|
18 |
+
# PDFReader = download_loader("PDFReader")
|
19 |
+
# loader = PDFReader()
|
20 |
+
# documents = loader.load_data(file=Path('./train-assets/training-target.pdf'))
|
21 |
+
|
22 |
+
UnstructuredURLLoader = download_loader("UnstructuredURLLoader")
|
23 |
+
# urls = [
|
24 |
+
# 'https://mosdecc.elchk.org.hk/index.php',
|
25 |
+
# 'https://mosdecc.elchk.org.hk/news_details.php?pkey=3762',
|
26 |
+
# 'https://mosdecc.elchk.org.hk/center_aboutus.php',
|
27 |
+
# 'https://mosdecc.elchk.org.hk/activity_details.php?pkey=1559&pg=1&news_cat=&news_year=&news_month=',
|
28 |
+
# 'https://mosdecc.elchk.org.hk/activity_details.php?pkey=1291&pg=1&news_cat=&news_year=&news_month=',
|
29 |
+
# 'https://mosdecc.elchk.org.hk/activity_details.php?pkey=1132&pg=1&news_cat=&news_year=&news_month=',
|
30 |
+
# 'https://mosdecc.elchk.org.hk/activity_details.php?pkey=1068&pg=2&news_cat=&news_year=all&news_month=all',
|
31 |
+
# 'https://mosdecc.elchk.org.hk/activity_details.php?pkey=1008&pg=2&news_cat=&news_year=all&news_month=all',
|
32 |
+
# 'https://mosdecc.elchk.org.hk/course.php',
|
33 |
+
# 'https://mosdecc.elchk.org.hk/service_member.php',
|
34 |
+
# 'https://mosdecc.elchk.org.hk/contactus.php',
|
35 |
+
# ]
|
36 |
+
|
37 |
+
urls = [
|
38 |
+
'https://service.elchk.org.hk/unit_service3.php?center=77#intro',
|
39 |
+
'https://service.elchk.org.hk/hot_project.php?pkey=102&tab=1&sub_tab=2',
|
40 |
+
'https://www.etnet.com.hk/www/tc/soin/seg_detail.php?id=561',
|
41 |
+
]
|
42 |
+
|
43 |
+
# loader = UnstructuredURLLoader(urls=urls, continue_on_failure=True, headers={
|
44 |
+
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
45 |
+
# "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537",
|
46 |
+
# })
|
47 |
+
# documents = loader.load()
|
48 |
+
|
49 |
+
pinecone.init(
|
50 |
+
api_key=PINECONE_API_KEY,
|
51 |
+
environment='gcp-starter'
|
52 |
+
)
|
53 |
+
pinecone_index = pinecone.Index(PINECONE_INDEX)
|
54 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
55 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
56 |
+
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
57 |
+
|
58 |
+
print('Done!')
|