Spaces:
No application file
No application file
BinZhang
commited on
Commit
·
c5763ee
1
Parent(s):
63b00b8
first push
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.llms.huggingface import HuggingFaceLLM
|
5 |
+
|
6 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
7 |
+
st.title("llama_index_demo")
|
8 |
+
|
9 |
+
# 初始化模型
|
10 |
+
@st.cache_resource
|
11 |
+
def init_models():
|
12 |
+
embed_model = HuggingFaceEmbedding(
|
13 |
+
model_name="/root/model/sentence-transformer"
|
14 |
+
)
|
15 |
+
Settings.embed_model = embed_model
|
16 |
+
|
17 |
+
llm = HuggingFaceLLM(
|
18 |
+
model_name="/root/model/internlm2-chat-1_8b",
|
19 |
+
tokenizer_name="/root/model/internlm2-chat-1_8b",
|
20 |
+
model_kwargs={"trust_remote_code": True},
|
21 |
+
tokenizer_kwargs={"trust_remote_code": True}
|
22 |
+
)
|
23 |
+
Settings.llm = llm
|
24 |
+
|
25 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
26 |
+
index = VectorStoreIndex.from_documents(documents)
|
27 |
+
query_engine = index.as_query_engine()
|
28 |
+
|
29 |
+
return query_engine
|
30 |
+
|
31 |
+
# 检查是否需要初始化模型
|
32 |
+
if 'query_engine' not in st.session_state:
|
33 |
+
st.session_state['query_engine'] = init_models()
|
34 |
+
|
35 |
+
def greet2(question):
|
36 |
+
response = st.session_state['query_engine'].query(question)
|
37 |
+
return response
|
38 |
+
|
39 |
+
|
40 |
+
# Store LLM generated responses
|
41 |
+
if "messages" not in st.session_state.keys():
|
42 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
43 |
+
|
44 |
+
# Display or clear chat messages
|
45 |
+
for message in st.session_state.messages:
|
46 |
+
with st.chat_message(message["role"]):
|
47 |
+
st.write(message["content"])
|
48 |
+
|
49 |
+
def clear_chat_history():
|
50 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
51 |
+
|
52 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
53 |
+
|
54 |
+
# Function for generating LLaMA2 response
|
55 |
+
def generate_llama_index_response(prompt_input):
|
56 |
+
return greet2(prompt_input)
|
57 |
+
|
58 |
+
# User-provided prompt
|
59 |
+
if prompt := st.chat_input():
|
60 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
61 |
+
with st.chat_message("user"):
|
62 |
+
st.write(prompt)
|
63 |
+
|
64 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
65 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
66 |
+
with st.chat_message("assistant"):
|
67 |
+
with st.spinner("Thinking..."):
|
68 |
+
response = generate_llama_index_response(prompt)
|
69 |
+
placeholder = st.empty()
|
70 |
+
placeholder.markdown(response)
|
71 |
+
message = {"role": "assistant", "content": response}
|
72 |
+
st.session_state.messages.append(message)
|