Spaces:
Sleeping
Sleeping
Kit-Hung commited on
Commit ·
26fc5d6
1
Parent(s): 9824533
add: app.py requirements.txt, update: README.md
Browse files- README.md +44 -0
- app.py +83 -0
- requirements.txt +140 -0
README.md
CHANGED
|
@@ -11,3 +11,47 @@ license: apache-2.0
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
## Installation of dependencies
|
| 17 |
+
### Sentence Transformer
|
| 18 |
+
```Bash
|
| 19 |
+
touch download_hf.py
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
download_hf.py
|
| 23 |
+
```python
|
| 24 |
+
import os
|
| 25 |
+
|
| 26 |
+
# 设置环境变量
|
| 27 |
+
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
|
| 28 |
+
|
| 29 |
+
# 下载模型
|
| 30 |
+
os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
download
|
| 34 |
+
```shell
|
| 35 |
+
python download_hf.py
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
### paraphrase-multilingual-MiniLM-L12-v2
|
| 40 |
+
```shell
|
| 41 |
+
mkdir /root/model
|
| 42 |
+
git lfs install
|
| 43 |
+
git clone https://www.modelscope.cn/Ceceliachenen/paraphrase-multilingual-MiniLM-L12-v2.git
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
### NLTK
|
| 48 |
+
```shell
|
| 49 |
+
cd /root
|
| 50 |
+
git clone https://gitee.com/yzy0612/nltk_data.git --branch gh-pages
|
| 51 |
+
cd nltk_data
|
| 52 |
+
mv packages/* ./
|
| 53 |
+
cd tokenizers
|
| 54 |
+
unzip punkt.zip
|
| 55 |
+
cd ../taggers
|
| 56 |
+
unzip averaged_perceptron_tagger.zip
|
| 57 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 3 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 4 |
+
from llama_index.legacy.callbacks import CallbackManager
|
| 5 |
+
from llama_index.llms.openai_like import OpenAILike
|
| 6 |
+
|
| 7 |
+
# Create an instance of CallbackManager
|
| 8 |
+
callback_manager = CallbackManager()
|
| 9 |
+
|
| 10 |
+
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
| 11 |
+
model = "internlm2.5-latest"
|
| 12 |
+
api_key = "api_key"
|
| 13 |
+
|
| 14 |
+
# api_base_url = "https://api.siliconflow.cn/v1"
|
| 15 |
+
# model = "internlm/internlm2_5-7b-chat"
|
| 16 |
+
# api_key = "api_key"
|
| 17 |
+
|
| 18 |
+
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
| 23 |
+
st.title("llama_index_demo")
|
| 24 |
+
|
| 25 |
+
# 初始化模型
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def init_models():
|
| 28 |
+
embed_model = HuggingFaceEmbedding(
|
| 29 |
+
model_name="/root/model/paraphrase-multilingual-MiniLM-L12-v2"
|
| 30 |
+
)
|
| 31 |
+
Settings.embed_model = embed_model
|
| 32 |
+
|
| 33 |
+
#用初始化llm
|
| 34 |
+
Settings.llm = llm
|
| 35 |
+
|
| 36 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
| 37 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 38 |
+
query_engine = index.as_query_engine()
|
| 39 |
+
|
| 40 |
+
return query_engine
|
| 41 |
+
|
| 42 |
+
# 检查是否需要初始化模型
|
| 43 |
+
if 'query_engine' not in st.session_state:
|
| 44 |
+
st.session_state['query_engine'] = init_models()
|
| 45 |
+
|
| 46 |
+
def greet2(question):
|
| 47 |
+
response = st.session_state['query_engine'].query(question)
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Store LLM generated responses
|
| 52 |
+
if "messages" not in st.session_state.keys():
|
| 53 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 54 |
+
|
| 55 |
+
# Display or clear chat messages
|
| 56 |
+
for message in st.session_state.messages:
|
| 57 |
+
with st.chat_message(message["role"]):
|
| 58 |
+
st.write(message["content"])
|
| 59 |
+
|
| 60 |
+
def clear_chat_history():
|
| 61 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 62 |
+
|
| 63 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 64 |
+
|
| 65 |
+
# Function for generating LLaMA2 response
|
| 66 |
+
def generate_llama_index_response(prompt_input):
|
| 67 |
+
return greet2(prompt_input)
|
| 68 |
+
|
| 69 |
+
# User-provided prompt
|
| 70 |
+
if prompt := st.chat_input():
|
| 71 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 72 |
+
with st.chat_message("user"):
|
| 73 |
+
st.write(prompt)
|
| 74 |
+
|
| 75 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
| 76 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 77 |
+
with st.chat_message("assistant"):
|
| 78 |
+
with st.spinner("Thinking..."):
|
| 79 |
+
response = generate_llama_index_response(prompt)
|
| 80 |
+
placeholder = st.empty()
|
| 81 |
+
placeholder.markdown(response)
|
| 82 |
+
message = {"role": "assistant", "content": response}
|
| 83 |
+
st.session_state.messages.append(message)
|
requirements.txt
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate==1.1.1
|
| 2 |
+
aiohappyeyeballs==2.4.3
|
| 3 |
+
aiohttp==3.10.10
|
| 4 |
+
aiosignal==1.3.1
|
| 5 |
+
altair==5.4.1
|
| 6 |
+
annotated-types==0.7.0
|
| 7 |
+
anyio==4.6.2.post1
|
| 8 |
+
async-timeout==4.0.3
|
| 9 |
+
attrs==24.2.0
|
| 10 |
+
beautifulsoup4==4.12.3
|
| 11 |
+
blinker==1.9.0
|
| 12 |
+
cachetools==5.5.0
|
| 13 |
+
certifi==2024.8.30
|
| 14 |
+
charset-normalizer==3.4.0
|
| 15 |
+
click==8.1.7
|
| 16 |
+
dataclasses-json==0.6.7
|
| 17 |
+
Deprecated==1.2.14
|
| 18 |
+
dirtyjson==1.0.8
|
| 19 |
+
distro==1.9.0
|
| 20 |
+
einops==0.7.0
|
| 21 |
+
exceptiongroup==1.2.2
|
| 22 |
+
filelock==3.16.1
|
| 23 |
+
filetype==1.2.0
|
| 24 |
+
frozenlist==1.5.0
|
| 25 |
+
fsspec==2024.10.0
|
| 26 |
+
gitdb==4.0.11
|
| 27 |
+
GitPython==3.1.43
|
| 28 |
+
greenlet==3.1.1
|
| 29 |
+
h11==0.14.0
|
| 30 |
+
httpcore==1.0.6
|
| 31 |
+
httpx==0.27.2
|
| 32 |
+
huggingface-hub==0.23.1
|
| 33 |
+
idna==3.10
|
| 34 |
+
InstructorEmbedding==1.0.1
|
| 35 |
+
Jinja2==3.1.4
|
| 36 |
+
jiter==0.7.0
|
| 37 |
+
joblib==1.4.2
|
| 38 |
+
jsonschema==4.23.0
|
| 39 |
+
jsonschema-specifications==2024.10.1
|
| 40 |
+
llama-cloud==0.1.4
|
| 41 |
+
llama-index==0.10.38
|
| 42 |
+
llama-index-agent-openai==0.2.9
|
| 43 |
+
llama-index-cli==0.1.13
|
| 44 |
+
llama-index-core==0.10.68.post1
|
| 45 |
+
llama-index-embeddings-huggingface==0.3.1
|
| 46 |
+
llama-index-embeddings-instructor==0.2.1
|
| 47 |
+
llama-index-embeddings-openai==0.1.11
|
| 48 |
+
llama-index-indices-managed-llama-cloud==0.1.6
|
| 49 |
+
llama-index-legacy==0.9.48.post4
|
| 50 |
+
llama-index-llms-huggingface==0.2.0
|
| 51 |
+
llama-index-llms-openai==0.1.31
|
| 52 |
+
llama-index-llms-openai-like==0.2.0
|
| 53 |
+
llama-index-llms-replicate==0.3.0
|
| 54 |
+
llama-index-multi-modal-llms-openai==0.1.9
|
| 55 |
+
llama-index-program-openai==0.1.7
|
| 56 |
+
llama-index-question-gen-openai==0.1.3
|
| 57 |
+
llama-index-readers-file==0.1.33
|
| 58 |
+
llama-index-readers-llama-parse==0.1.6
|
| 59 |
+
llama-parse==0.4.9
|
| 60 |
+
llamaindex-py-client==0.1.19
|
| 61 |
+
markdown-it-py==3.0.0
|
| 62 |
+
MarkupSafe==3.0.2
|
| 63 |
+
marshmallow==3.23.1
|
| 64 |
+
mdurl==0.1.2
|
| 65 |
+
minijinja==2.5.0
|
| 66 |
+
mpmath==1.3.0
|
| 67 |
+
multidict==6.1.0
|
| 68 |
+
mypy-extensions==1.0.0
|
| 69 |
+
narwhals==1.13.3
|
| 70 |
+
nest-asyncio==1.6.0
|
| 71 |
+
networkx==3.4.2
|
| 72 |
+
nltk==3.9.1
|
| 73 |
+
numpy==1.26.4
|
| 74 |
+
nvidia-cublas-cu12==12.1.3.1
|
| 75 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
| 76 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
| 77 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
| 78 |
+
nvidia-cudnn-cu12==9.1.0.70
|
| 79 |
+
nvidia-cufft-cu12==11.0.2.54
|
| 80 |
+
nvidia-curand-cu12==10.3.2.106
|
| 81 |
+
nvidia-cusolver-cu12==11.4.5.107
|
| 82 |
+
nvidia-cusparse-cu12==12.1.0.106
|
| 83 |
+
nvidia-nccl-cu12==2.21.5
|
| 84 |
+
nvidia-nvjitlink-cu12==12.4.127
|
| 85 |
+
nvidia-nvtx-cu12==12.1.105
|
| 86 |
+
openai==1.54.3
|
| 87 |
+
openmind-hub==0.9.0
|
| 88 |
+
packaging==24.2
|
| 89 |
+
pandas==2.2.3
|
| 90 |
+
pillow==10.4.0
|
| 91 |
+
propcache==0.2.0
|
| 92 |
+
protobuf==5.26.1
|
| 93 |
+
psutil==6.1.0
|
| 94 |
+
pyarrow==18.0.0
|
| 95 |
+
pydantic==2.9.2
|
| 96 |
+
pydantic_core==2.23.4
|
| 97 |
+
pydeck==0.9.1
|
| 98 |
+
Pygments==2.18.0
|
| 99 |
+
pypdf==4.3.1
|
| 100 |
+
python-dateutil==2.9.0.post0
|
| 101 |
+
pytz==2024.2
|
| 102 |
+
PyYAML==6.0.2
|
| 103 |
+
referencing==0.35.1
|
| 104 |
+
regex==2024.11.6
|
| 105 |
+
requests==2.32.2
|
| 106 |
+
rich==13.9.4
|
| 107 |
+
rpds-py==0.21.0
|
| 108 |
+
safetensors==0.4.5
|
| 109 |
+
scikit-learn==1.5.2
|
| 110 |
+
scipy==1.14.1
|
| 111 |
+
sentence-transformers==2.7.0
|
| 112 |
+
sentencepiece==0.2.0
|
| 113 |
+
six==1.16.0
|
| 114 |
+
smmap==5.0.1
|
| 115 |
+
sniffio==1.3.1
|
| 116 |
+
soupsieve==2.6
|
| 117 |
+
SQLAlchemy==2.0.36
|
| 118 |
+
streamlit==1.39.0
|
| 119 |
+
striprtf==0.0.26
|
| 120 |
+
sympy==1.13.1
|
| 121 |
+
tenacity==8.5.0
|
| 122 |
+
text-generation==0.7.0
|
| 123 |
+
threadpoolctl==3.5.0
|
| 124 |
+
tiktoken==0.8.0
|
| 125 |
+
tokenizers==0.19.1
|
| 126 |
+
toml==0.10.2
|
| 127 |
+
torch==2.5.0+cu121
|
| 128 |
+
torchaudio==2.5.0+cu121
|
| 129 |
+
torchvision==0.20.0+cu121
|
| 130 |
+
tornado==6.4.1
|
| 131 |
+
tqdm==4.67.0
|
| 132 |
+
transformers==4.41.1
|
| 133 |
+
triton==3.1.0
|
| 134 |
+
typing-inspect==0.9.0
|
| 135 |
+
typing_extensions==4.12.2
|
| 136 |
+
tzdata==2024.2
|
| 137 |
+
urllib3==2.2.3
|
| 138 |
+
watchdog==5.0.3
|
| 139 |
+
wrapt==1.16.0
|
| 140 |
+
yarl==1.17.1
|