Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,60 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
from langchain_core.documents import Document
|
| 4 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
-
from langchain_community.vectorstores import FAISS
|
| 6 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
-
from langchain_openai import ChatOpenAI
|
| 8 |
-
from langchain.chains import RetrievalQA
|
| 9 |
-
import chainlit as cl
|
| 10 |
-
|
| 11 |
-
# === Load and prepare data ===
|
| 12 |
-
with open("combined_data.json", "r") as f:
|
| 13 |
-
raw_data = json.load(f)
|
| 14 |
-
|
| 15 |
-
all_docs = [
|
| 16 |
-
Document(page_content=entry["content"], metadata=entry["metadata"])
|
| 17 |
-
for entry in raw_data
|
| 18 |
-
]
|
| 19 |
-
|
| 20 |
-
# === Split documents into chunks ===
|
| 21 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=50)
|
| 22 |
-
chunked_docs = splitter.split_documents(all_docs)
|
| 23 |
-
|
| 24 |
-
# === Use your fine-tuned Hugging Face embeddings ===
|
| 25 |
-
embedding_model = HuggingFaceEmbeddings(
|
| 26 |
-
model_name="
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# === Set up FAISS vector store ===
|
| 30 |
-
vectorstore = FAISS.from_documents(chunked_docs, embedding_model)
|
| 31 |
-
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
| 32 |
-
|
| 33 |
-
# === Load LLM ===
|
| 34 |
-
llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0)
|
| 35 |
-
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 36 |
-
|
| 37 |
-
# === Chainlit start event ===
|
| 38 |
-
@cl.on_chat_start
|
| 39 |
-
async def start():
|
| 40 |
-
await cl.Message(content =
|
| 41 |
-
"""👋 Welcome to your Reformer Pilates AI!
|
| 42 |
-
|
| 43 |
-
Here’s what you can do:
|
| 44 |
-
• Ask questions about Reformer Pilates
|
| 45 |
-
• Get individualized workouts based on your level, goals, and equipment
|
| 46 |
-
• Get instant exercise modifications based on injuries or limitations
|
| 47 |
-
|
| 48 |
-
Let’s get started! 🚀""").send()
|
| 49 |
-
cl.user_session.set("qa_chain", qa_chain)
|
| 50 |
-
|
| 51 |
-
# === Chainlit message handler ===
|
| 52 |
-
@cl.on_message
|
| 53 |
-
async def handle_message(message: cl.Message):
|
| 54 |
-
chain = cl.user_session.get("qa_chain")
|
| 55 |
-
if chain:
|
| 56 |
-
try:
|
| 57 |
-
response = chain.run(message.content)
|
| 58 |
-
except Exception as e:
|
| 59 |
-
response = f"⚠️ Error: {str(e)}"
|
| 60 |
-
await cl.Message(response).send()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from langchain_core.documents import Document
|
| 4 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_openai import ChatOpenAI
|
| 8 |
+
from langchain.chains import RetrievalQA
|
| 9 |
+
import chainlit as cl
|
| 10 |
+
|
| 11 |
+
# === Load and prepare data ===
|
| 12 |
+
with open("combined_data.json", "r") as f:
|
| 13 |
+
raw_data = json.load(f)
|
| 14 |
+
|
| 15 |
+
all_docs = [
|
| 16 |
+
Document(page_content=entry["content"], metadata=entry["metadata"])
|
| 17 |
+
for entry in raw_data
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
# === Split documents into chunks ===
|
| 21 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=50)
|
| 22 |
+
chunked_docs = splitter.split_documents(all_docs)
|
| 23 |
+
|
| 24 |
+
# === Use your fine-tuned Hugging Face embeddings ===
|
| 25 |
+
embedding_model = HuggingFaceEmbeddings(
|
| 26 |
+
model_name="bsmith3715/legal-ft-demo_final"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# === Set up FAISS vector store ===
|
| 30 |
+
vectorstore = FAISS.from_documents(chunked_docs, embedding_model)
|
| 31 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
| 32 |
+
|
| 33 |
+
# === Load LLM ===
|
| 34 |
+
llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0)
|
| 35 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 36 |
+
|
| 37 |
+
# === Chainlit start event ===
|
| 38 |
+
@cl.on_chat_start
|
| 39 |
+
async def start():
|
| 40 |
+
await cl.Message(content =
|
| 41 |
+
"""👋 Welcome to your Reformer Pilates AI!
|
| 42 |
+
|
| 43 |
+
Here’s what you can do:
|
| 44 |
+
• Ask questions about Reformer Pilates
|
| 45 |
+
• Get individualized workouts based on your level, goals, and equipment
|
| 46 |
+
• Get instant exercise modifications based on injuries or limitations
|
| 47 |
+
|
| 48 |
+
Let’s get started! 🚀""").send()
|
| 49 |
+
cl.user_session.set("qa_chain", qa_chain)
|
| 50 |
+
|
| 51 |
+
# === Chainlit message handler ===
|
| 52 |
+
@cl.on_message
|
| 53 |
+
async def handle_message(message: cl.Message):
|
| 54 |
+
chain = cl.user_session.get("qa_chain")
|
| 55 |
+
if chain:
|
| 56 |
+
try:
|
| 57 |
+
response = chain.run(message.content)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
response = f"⚠️ Error: {str(e)}"
|
| 60 |
+
await cl.Message(response).send()
|