Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 3 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 4 |
+
from llama_index.core.node_parser import SentenceSplitter
|
| 5 |
+
from llama_index.core.ingestion import IngestionPipeline
|
| 6 |
+
import chromadb
|
| 7 |
+
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 8 |
+
from llama_index.llms.ollama import Ollama
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Ustawienia strony
|
| 12 |
+
st.title("Aplikacja z LlamaIndex")
|
| 13 |
+
|
| 14 |
+
# Za艂aduj dokumenty
|
| 15 |
+
documents = SimpleDirectoryReader('./data/').load_data()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
db = chromadb.PersistentClient(path="./ustawy")
|
| 20 |
+
chroma_collection = db.get_or_create_collection("pomoc_ukrainie")
|
| 21 |
+
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 22 |
+
|
| 23 |
+
# Utw贸rz pipeline do przetwarzania dokument贸w
|
| 24 |
+
pipeline = IngestionPipeline(
|
| 25 |
+
transformations=[
|
| 26 |
+
SentenceSplitter(),
|
| 27 |
+
HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
|
| 28 |
+
],
|
| 29 |
+
vector_store=vector_store
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Przetw贸rz dokumenty
|
| 33 |
+
nodes = pipeline.run(documents)
|
| 34 |
+
|
| 35 |
+
# Utw贸rz indeks
|
| 36 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 37 |
+
index = VectorStoreIndex.from_vector_store(vector_store, embed_model=embed_model)
|
| 38 |
+
|
| 39 |
+
# Utw贸rz silnik zapyta艅
|
| 40 |
+
llm = Ollama(model="qwen2:7b")
|
| 41 |
+
query_engine = index.as_query_engine(llm=llm)
|
| 42 |
+
|
| 43 |
+
# Wy艣wietl histori臋 wiadomo艣ci
|
| 44 |
+
if "messages" not in st.session_state:
|
| 45 |
+
st.session_state.messages = []
|
| 46 |
+
|
| 47 |
+
# Wej艣cie u偶ytkownika
|
| 48 |
+
if input := st.text_input("Zadaj pytanie"):
|
| 49 |
+
st.session_state.messages.append({"role": "user", "content": input})
|
| 50 |
+
|
| 51 |
+
# Wygeneruj odpowied藕
|
| 52 |
+
try:
|
| 53 |
+
response = query_engine.query(input)
|
| 54 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"B艂膮d: {e}")
|
| 57 |
+
|
| 58 |
+
# Wy艣wietl wiadomo艣ci
|
| 59 |
+
for message in st.session_state.messages:
|
| 60 |
+
if message["role"] == "user":
|
| 61 |
+
st.write(f"U偶ytkownik: {message['content']}")
|
| 62 |
+
else:
|
| 63 |
+
st.write(f"Asystent: {message['content']}")
|