Spaces:
Sleeping
Sleeping
Update: v1.6
Browse files
app.py
CHANGED
|
@@ -1,60 +1,34 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import logging
|
| 3 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 4 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
from llama_index.legacy.callbacks import CallbackManager
|
| 6 |
from llama_index.llms.openai_like import OpenAILike
|
| 7 |
import os
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# 设置日志
|
| 11 |
-
class StreamlitHandler(logging.Handler):
|
| 12 |
-
def emit(self, record):
|
| 13 |
-
st.write(f"📝 {self.format(record)}")
|
| 14 |
-
|
| 15 |
-
# 配置日志输出到控制台和 Streamlit
|
| 16 |
-
logging.basicConfig(
|
| 17 |
-
level=logging.INFO,
|
| 18 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 19 |
-
handlers=[
|
| 20 |
-
logging.StreamHandler(sys.stdout), # 输出到控制台
|
| 21 |
-
StreamlitHandler() # 输出到 Streamlit 界面
|
| 22 |
-
]
|
| 23 |
-
)
|
| 24 |
-
logger = logging.getLogger(__name__)
|
| 25 |
-
|
| 26 |
-
# 创建日志显示区域
|
| 27 |
-
log_container = st.empty()
|
| 28 |
-
|
| 29 |
-
# 页面配置
|
| 30 |
-
st.set_page_config(
|
| 31 |
-
page_title="AI Assistant Demo",
|
| 32 |
-
page_icon="😄",
|
| 33 |
-
layout="wide" # 使用宽布局
|
| 34 |
-
)
|
| 35 |
|
| 36 |
# 显示加载状态
|
| 37 |
status_placeholder = st.empty()
|
| 38 |
|
| 39 |
def init_models():
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
status_placeholder.text("正在初始化模型...")
|
| 43 |
|
| 44 |
# 初始化 API 设置
|
| 45 |
api_key = os.getenv("API_KEY")
|
| 46 |
if not api_key:
|
| 47 |
-
|
| 48 |
raise ValueError("API_KEY environment variable is not set")
|
| 49 |
|
| 50 |
-
|
| 51 |
api_base_url = "https://api.siliconflow.cn/v1"
|
| 52 |
model = "internlm/internlm2_5-7b-chat"
|
| 53 |
|
| 54 |
-
|
| 55 |
callback_manager = CallbackManager()
|
| 56 |
|
| 57 |
-
|
| 58 |
llm = OpenAILike(
|
| 59 |
model=model,
|
| 60 |
api_base=api_base_url,
|
|
@@ -63,47 +37,36 @@ def init_models():
|
|
| 63 |
callback_manager=callback_manager
|
| 64 |
)
|
| 65 |
Settings.llm = llm
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
embed_model = HuggingFaceEmbedding(
|
| 70 |
model_name="/home/user/model/paraphrase-multilingual-MiniLM-L12-v2"
|
| 71 |
)
|
| 72 |
Settings.embed_model = embed_model
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
documents = SimpleDirectoryReader("/home/user/data").load_data()
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
index = VectorStoreIndex.from_documents(documents)
|
| 81 |
|
| 82 |
-
|
| 83 |
query_engine = index.as_query_engine()
|
| 84 |
|
| 85 |
-
|
| 86 |
status_placeholder.empty()
|
| 87 |
return query_engine
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
error_msg = f"Error during initialization: {str(e)}"
|
| 91 |
-
|
| 92 |
st.error(error_msg)
|
| 93 |
raise
|
| 94 |
|
| 95 |
-
|
| 96 |
-
col1, col2 = st.columns([2, 1])
|
| 97 |
-
|
| 98 |
-
with col1:
|
| 99 |
-
# 初始化标题和说明
|
| 100 |
-
st.title("AI Assistant Demo")
|
| 101 |
-
st.markdown("---")
|
| 102 |
-
|
| 103 |
-
with col2:
|
| 104 |
-
# 显示系统状态
|
| 105 |
-
st.subheader("System Status")
|
| 106 |
-
st.markdown("---")
|
| 107 |
|
| 108 |
# 检查是否需要初始化模型
|
| 109 |
if 'query_engine' not in st.session_state:
|
|
@@ -113,13 +76,13 @@ if 'query_engine' not in st.session_state:
|
|
| 113 |
|
| 114 |
def generate_response(question):
|
| 115 |
try:
|
| 116 |
-
|
| 117 |
response = st.session_state['query_engine'].query(question)
|
| 118 |
-
|
| 119 |
return response
|
| 120 |
except Exception as e:
|
| 121 |
error_msg = f"Error generating response: {str(e)}"
|
| 122 |
-
|
| 123 |
st.error(error_msg)
|
| 124 |
return None
|
| 125 |
|
|
@@ -135,14 +98,14 @@ for message in st.session_state.messages:
|
|
| 135 |
# 清除聊天历史的功能
|
| 136 |
def clear_chat_history():
|
| 137 |
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助���的吗?"}]
|
| 138 |
-
|
| 139 |
|
| 140 |
# 侧边栏按钮
|
| 141 |
st.sidebar.button('清除聊天历史', on_click=clear_chat_history)
|
| 142 |
|
| 143 |
# 用户输入处理
|
| 144 |
if prompt := st.chat_input():
|
| 145 |
-
|
| 146 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 147 |
with st.chat_message("user"):
|
| 148 |
st.write(prompt)
|
|
@@ -156,4 +119,4 @@ if prompt := st.chat_input():
|
|
| 156 |
st.markdown(response)
|
| 157 |
message = {"role": "assistant", "content": response.response}
|
| 158 |
st.session_state.messages.append(message)
|
| 159 |
-
|
|
|
|
| 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 |
import os
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="AI Assistant Demo", page_icon="😄")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
| 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:
|
|
|
|
| 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 |
|
|
|
|
| 98 |
# 清除聊天历史的功能
|
| 99 |
def clear_chat_history():
|
| 100 |
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助���的吗?"}]
|
| 101 |
+
print("Chat history cleared")
|
| 102 |
|
| 103 |
# 侧边栏按钮
|
| 104 |
st.sidebar.button('清除聊天历史', on_click=clear_chat_history)
|
| 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)
|
|
|
|
| 119 |
st.markdown(response)
|
| 120 |
message = {"role": "assistant", "content": response.response}
|
| 121 |
st.session_state.messages.append(message)
|
| 122 |
+
print("Response added to chat history")
|