KitHung commited on
Commit
a7a2ed7
·
1 Parent(s): fe7599d

Update: v1

Browse files
Files changed (3) hide show
  1. Dockerfile +50 -7
  2. app.py +77 -5
  3. requirements.txt +10 -2
Dockerfile CHANGED
@@ -1,16 +1,59 @@
1
  # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
  # you will also find guides on how best to write your Dockerfile
3
 
4
- FROM python:3.9
5
 
 
 
 
 
6
  RUN useradd -m -u 1000 user
7
- USER user
 
 
 
 
 
 
8
  ENV PATH="/home/user/.local/bin:$PATH"
9
 
10
- WORKDIR /app
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- COPY --chown=user ./requirements.txt requirements.txt
13
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- COPY --chown=user . /app
16
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
  # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
  # you will also find guides on how best to write your Dockerfile
3
 
4
+ FROM python:3.10
5
 
6
+ # Install git and git-lfs
7
+ RUN apt-get update && apt-get install -y git git-lfs && rm -rf /var/lib/apt/lists/*
8
+
9
+ # Create user and set working directory
10
  RUN useradd -m -u 1000 user
11
+ WORKDIR /home/user/app
12
+
13
+ # Create necessary directories
14
+ RUN mkdir -p /home/user/model /home/user/data
15
+
16
+ # Set environment variables
17
+ ENV HF_ENDPOINT=https://hf-mirror.com
18
  ENV PATH="/home/user/.local/bin:$PATH"
19
 
20
+ # Install huggingface-cli first for downloading models
21
+ RUN pip install --no-cache-dir huggingface-cli
22
+
23
+ # Download sentence-transformer model
24
+ RUN huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /home/user/model/sentence-transformer
25
+
26
+ # Download paraphrase-multilingual-MiniLM-L12-v2
27
+ RUN cd /home/user/model && \
28
+ git clone https://www.modelscope.cn/Ceceliachenen/paraphrase-multilingual-MiniLM-L12-v2.git && \
29
+ cd /home/user/model/paraphrase-multilingual-MiniLM-L12-v2 && \
30
+ git lfs install && \
31
+ git lfs pull
32
 
33
+ # Download NLTK data
34
+ RUN cd /home/user && \
35
+ git clone https://gitee.com/yzy0612/nltk_data.git --branch gh-pages && \
36
+ cd /home/user/nltk_data && \
37
+ mv /home/user/nltk_data/packages/* /home/user/nltk_data && \
38
+ cd /home/user/nltk_data/tokenizers && \
39
+ unzip punkt.zip && \
40
+ cd /home/user/nltk_data/taggers && \
41
+ unzip averaged_perceptron_tagger.zip
42
+
43
+ # Download demo data
44
+ RUN cd /home/user/data && \
45
+ git clone https://github.com/Kit-Hung/demos && \
46
+ cp /home/user/data/demos/k8s/1-deploy/3-k8s_install/2-kubeadm/kubeadm.md /home/user/data
47
+
48
+ # Install Python dependencies
49
+ COPY --chown=user requirements.txt .
50
+ RUN pip install --no-cache-dir -r requirements.txt
51
+
52
+ # Copy application files
53
+ COPY --chown=user . .
54
+
55
+ # Switch to non-root user
56
+ USER user
57
 
58
+ # Run streamlit
59
+ CMD ["streamlit", "run", "app.py"]
app.py CHANGED
@@ -1,7 +1,79 @@
1
- from fastapi import FastAPI
 
 
 
 
2
 
3
- app = FastAPI()
 
4
 
5
- @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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://api.siliconflow.cn/v1"
11
+ model = "internlm/internlm2_5-7b-chat"
12
+ api_key = st.secrets["API_KEY"]
13
+
14
+ llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True, callback_manager=callback_manager)
15
+
16
+
17
+
18
+ st.set_page_config(page_title="ai_assistant_demo", page_icon="😄")
19
+ st.title("AI Assistant Demo")
20
+
21
+ # 初始化模型
22
+ @st.cache_resource
23
+ def init_models():
24
+ embed_model = HuggingFaceEmbedding(
25
+ model_name="/home/user/model/sentence-transformer"
26
+ )
27
+ Settings.embed_model = embed_model
28
+
29
+ #用初始化llm
30
+ Settings.llm = llm
31
+
32
+ documents = SimpleDirectoryReader("/home/user/data").load_data()
33
+ index = VectorStoreIndex.from_documents(documents)
34
+ query_engine = index.as_query_engine()
35
+
36
+ return query_engine
37
+
38
+ # 检查是否需要初始化模型
39
+ if 'query_engine' not in st.session_state:
40
+ st.session_state['query_engine'] = init_models()
41
+
42
+ def greet2(question):
43
+ response = st.session_state['query_engine'].query(question)
44
+ return response
45
+
46
+
47
+ # Store LLM generated responses
48
+ if "messages" not in st.session_state.keys():
49
+ st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
50
+
51
+ # Display or clear chat messages
52
+ for message in st.session_state.messages:
53
+ with st.chat_message(message["role"]):
54
+ st.write(message["content"])
55
+
56
+ def clear_chat_history():
57
+ st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
58
+
59
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
60
+
61
+ # Function for generating LLaMA2 response
62
+ def generate_llama_index_response(prompt_input):
63
+ return greet2(prompt_input)
64
+
65
+ # User-provided prompt
66
+ if prompt := st.chat_input():
67
+ st.session_state.messages.append({"role": "user", "content": prompt})
68
+ with st.chat_message("user"):
69
+ st.write(prompt)
70
+
71
+ # Gegenerate_llama_index_response last message is not from assistant
72
+ if st.session_state.messages[-1]["role"] != "assistant":
73
+ with st.chat_message("assistant"):
74
+ with st.spinner("Thinking..."):
75
+ response = generate_llama_index_response(prompt)
76
+ placeholder = st.empty()
77
+ placeholder.markdown(response)
78
+ message = {"role": "assistant", "content": response.response}
79
+ st.session_state.messages.append(message)
requirements.txt CHANGED
@@ -1,2 +1,10 @@
1
- fastapi
2
- uvicorn[standard]
 
 
 
 
 
 
 
 
 
1
+ einops==0.7.0
2
+ protobuf==5.26.1
3
+ llama-index==0.11.20
4
+ llama-index-llms-replicate==0.3.0
5
+ llama-index-llms-openai-like==0.2.0
6
+ llama-index-embeddings-huggingface==0.3.1
7
+ llama-index-embeddings-instructor==0.2.1
8
+ torch==2.5.0
9
+ torchvision==0.20.0
10
+ torchaudio==2.5.0