Spaces:
Sleeping
Sleeping
Update: v1.7
Browse files- Dockerfile +1 -1
- app.py +56 -95
Dockerfile
CHANGED
@@ -63,4 +63,4 @@ USER user
|
|
63 |
EXPOSE 8501
|
64 |
|
65 |
# Run streamlit with proper path
|
66 |
-
CMD ["
|
|
|
63 |
EXPOSE 8501
|
64 |
|
65 |
# Run streamlit with proper path
|
66 |
+
CMD ["streamlit", "run", "app.py"]
|
app.py
CHANGED
@@ -3,120 +3,81 @@ 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 |
-
import
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
# 显示加载状态
|
11 |
-
status_placeholder = st.empty()
|
12 |
|
13 |
-
def init_models():
|
14 |
-
try:
|
15 |
-
print("Starting model initialization...")
|
16 |
-
status_placeholder.text("正在初始化模型...")
|
17 |
-
|
18 |
-
# 初始化 API 设置
|
19 |
-
api_key = os.getenv("API_KEY")
|
20 |
-
if not api_key:
|
21 |
-
print("Error: API_KEY environment variable is not set")
|
22 |
-
raise ValueError("API_KEY environment variable is not set")
|
23 |
-
|
24 |
-
print("API key loaded successfully")
|
25 |
-
api_base_url = "https://api.siliconflow.cn/v1"
|
26 |
-
model = "internlm/internlm2_5-7b-chat"
|
27 |
-
|
28 |
-
print("Initializing callback manager...")
|
29 |
-
callback_manager = CallbackManager()
|
30 |
-
|
31 |
-
print("Initializing LLM...")
|
32 |
-
llm = OpenAILike(
|
33 |
-
model=model,
|
34 |
-
api_base=api_base_url,
|
35 |
-
api_key=api_key,
|
36 |
-
is_chat_model=True,
|
37 |
-
callback_manager=callback_manager
|
38 |
-
)
|
39 |
-
Settings.llm = llm
|
40 |
-
print("LLM initialized successfully")
|
41 |
-
|
42 |
-
print("Initializing embedding model...")
|
43 |
-
embed_model = HuggingFaceEmbedding(
|
44 |
-
model_name="/home/user/model/paraphrase-multilingual-MiniLM-L12-v2"
|
45 |
-
)
|
46 |
-
Settings.embed_model = embed_model
|
47 |
-
print("Embedding model initialized successfully")
|
48 |
-
|
49 |
-
print("Loading documents...")
|
50 |
-
documents = SimpleDirectoryReader("/home/user/data").load_data()
|
51 |
-
print(f"Loaded {len(documents)} documents")
|
52 |
-
|
53 |
-
print("Creating vector store index...")
|
54 |
-
index = VectorStoreIndex.from_documents(documents)
|
55 |
-
|
56 |
-
print("Creating query engine...")
|
57 |
-
query_engine = index.as_query_engine()
|
58 |
-
|
59 |
-
print("Model initialization completed successfully!")
|
60 |
-
status_placeholder.empty()
|
61 |
-
return query_engine
|
62 |
-
|
63 |
-
except Exception as e:
|
64 |
-
error_msg = f"Error during initialization: {str(e)}"
|
65 |
-
print(error_msg)
|
66 |
-
st.error(error_msg)
|
67 |
-
raise
|
68 |
|
|
|
69 |
st.title("AI Assistant Demo")
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# 检查是否需要初始化模型
|
72 |
if 'query_engine' not in st.session_state:
|
73 |
-
|
74 |
-
st.session_state['query_engine'] = init_models()
|
75 |
-
st.success("模型初始化完成!")
|
76 |
-
|
77 |
-
def generate_response(question):
|
78 |
-
try:
|
79 |
-
print(f"Generating response for question: {question}")
|
80 |
-
response = st.session_state['query_engine'].query(question)
|
81 |
-
print("Response generated successfully")
|
82 |
-
return response
|
83 |
-
except Exception as e:
|
84 |
-
error_msg = f"Error generating response: {str(e)}"
|
85 |
-
print(error_msg)
|
86 |
-
st.error(error_msg)
|
87 |
-
return None
|
88 |
-
|
89 |
-
# 初始化消息历史
|
90 |
-
if "messages" not in st.session_state:
|
91 |
-
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
for message in st.session_state.messages:
|
95 |
with st.chat_message(message["role"]):
|
96 |
st.write(message["content"])
|
97 |
|
98 |
-
# 清除聊天历史的功能
|
99 |
def clear_chat_history():
|
100 |
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
101 |
-
print("Chat history cleared")
|
102 |
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
105 |
|
106 |
-
#
|
107 |
if prompt := st.chat_input():
|
108 |
-
print(f"Received user input: {prompt}")
|
109 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
110 |
with st.chat_message("user"):
|
111 |
st.write(prompt)
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
print("Response added to chat history")
|
|
|
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 |
+
from download import prepare_data
|
7 |
|
8 |
+
# prepare datas
|
9 |
+
prepare_data()
|
10 |
+
|
11 |
+
# Create an instance of CallbackManager
|
12 |
+
callback_manager = CallbackManager()
|
13 |
+
|
14 |
+
api_base_url = "https://api.siliconflow.cn/v1"
|
15 |
+
model = "internlm/internlm2_5-7b-chat"
|
16 |
+
api_key = st.secrets["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="ai_assistant_demo", page_icon="😄")
|
23 |
st.title("AI Assistant Demo")
|
24 |
|
25 |
+
# 初始化模型
|
26 |
+
@st.cache_resource
|
27 |
+
def init_models():
|
28 |
+
embed_model = HuggingFaceEmbedding(
|
29 |
+
model_name="/home/user/model/paraphrase-multilingual-MiniLM-L12-v2"
|
30 |
+
)
|
31 |
+
Settings.embed_model = embed_model
|
32 |
+
|
33 |
+
#用初始化llm
|
34 |
+
Settings.llm = llm
|
35 |
+
|
36 |
+
documents = SimpleDirectoryReader("/home/user/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.response}
|
83 |
+
st.session_state.messages.append(message)
|
|