Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- Dockerfile +18 -0
- README.md +26 -10
- app.py +112 -0
- chainlit.md +14 -0
- combined_data.json +0 -0
- pyproject.toml +20 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
|
| 6 |
+
ENV HOME=/home/user \
|
| 7 |
+
PATH=/home/user/.local/bin:$PATH \
|
| 8 |
+
UVICORN_WS_PROTOCOL=websockets
|
| 9 |
+
|
| 10 |
+
WORKDIR $HOME/app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . $HOME/app
|
| 13 |
+
|
| 14 |
+
RUN uv sync
|
| 15 |
+
|
| 16 |
+
EXPOSE 7860
|
| 17 |
+
|
| 18 |
+
CMD ["uv", "run", "chainlit", "run", "app.py", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,26 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: docker
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title : Pilates App Fine_Tuned
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
short_description: Tool to provide users reformer exercises
|
| 11 |
+
startup_duration_timeout: 1h
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# Pilates Reformer RAG App Fine_Tuned
|
| 15 |
+
|
| 16 |
+
This Chainlit app answers questions using Pilates reformer videos and textbooks. All data is preloaded from `combined_data.json`.
|
| 17 |
+
|
| 18 |
+
## Run Locally
|
| 19 |
+
|
| 20 |
+
```bash
|
| 21 |
+
uv run chainlit run app.py
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Or Deploy to Hugging Face Space with Docker
|
| 25 |
+
Just upload this directory and you're done.
|
| 26 |
+
|
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
| 11 |
+
from stability_sdk import client
|
| 12 |
+
import io
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
# === Load and prepare data ===
|
| 16 |
+
with open("combined_data.json", "r") as f:
|
| 17 |
+
raw_data = json.load(f)
|
| 18 |
+
|
| 19 |
+
all_docs = [
|
| 20 |
+
Document(page_content=entry["content"], metadata=entry["metadata"])
|
| 21 |
+
for entry in raw_data
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# === Split documents into chunks ===
|
| 25 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=50)
|
| 26 |
+
chunked_docs = splitter.split_documents(all_docs)
|
| 27 |
+
|
| 28 |
+
# === Use your fine-tuned Hugging Face embeddings ===
|
| 29 |
+
embedding_model = HuggingFaceEmbeddings(
|
| 30 |
+
model_name="AneetaXavier/reformer-pilates-embed-ft-49fc1835-9968-433d-9c45-1538ea91dcc9"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# === Set up FAISS vector store ===
|
| 34 |
+
vectorstore = FAISS.from_documents(chunked_docs, embedding_model)
|
| 35 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
| 36 |
+
|
| 37 |
+
# === Load LLM ===
|
| 38 |
+
llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0)
|
| 39 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 40 |
+
|
| 41 |
+
# === Initialize Stability AI client ===
|
| 42 |
+
stability_api = client.StabilityInference(
|
| 43 |
+
key=os.getenv('STABILITY_KEY'),
|
| 44 |
+
verbose=True,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# === Chainlit start event ===
|
| 48 |
+
@cl.on_chat_start
|
| 49 |
+
async def start():
|
| 50 |
+
await cl.Message(content =
|
| 51 |
+
"""π Welcome to your Reformer Pilates AI!
|
| 52 |
+
|
| 53 |
+
Here's what you can do:
|
| 54 |
+
β’ Ask questions about Reformer Pilates
|
| 55 |
+
β’ Get individualized workouts based on your level, goals, and equipment
|
| 56 |
+
β’ Get instant exercise modifications based on injuries or limitations
|
| 57 |
+
|
| 58 |
+
Let's get started! π""").send()
|
| 59 |
+
cl.user_session.set("qa_chain", qa_chain)
|
| 60 |
+
|
| 61 |
+
# === Chainlit message handler ===
|
| 62 |
+
@cl.on_message
|
| 63 |
+
async def handle_message(message: cl.Message):
|
| 64 |
+
if message.content.lower().startswith('create an image'):
|
| 65 |
+
try:
|
| 66 |
+
# Extract the prompt from the message
|
| 67 |
+
prompt = message.content[15:].strip()
|
| 68 |
+
|
| 69 |
+
# Generate the image
|
| 70 |
+
answers = stability_api.generate(
|
| 71 |
+
prompt=prompt,
|
| 72 |
+
seed=123,
|
| 73 |
+
steps=30,
|
| 74 |
+
cfg_scale=7.0,
|
| 75 |
+
width=512,
|
| 76 |
+
height=512,
|
| 77 |
+
samples=1,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Process the generated image
|
| 81 |
+
for resp in answers:
|
| 82 |
+
for artifact in resp.artifacts:
|
| 83 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 84 |
+
# Convert the image data to a PIL Image
|
| 85 |
+
img = Image.open(io.BytesIO(artifact.binary))
|
| 86 |
+
|
| 87 |
+
# Save the image temporarily
|
| 88 |
+
img_path = "temp_generated_image.png"
|
| 89 |
+
img.save(img_path)
|
| 90 |
+
|
| 91 |
+
# Send the image to the chat
|
| 92 |
+
await cl.Message(
|
| 93 |
+
content="Here's your generated image:",
|
| 94 |
+
elements=[cl.Image(path=img_path, name="Generated Image")]
|
| 95 |
+
).send()
|
| 96 |
+
|
| 97 |
+
# Clean up the temporary file
|
| 98 |
+
os.remove(img_path)
|
| 99 |
+
return
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
await cl.Message(content=f"β οΈ Error generating image: {str(e)}").send()
|
| 103 |
+
return
|
| 104 |
+
|
| 105 |
+
# Handle regular QA queries
|
| 106 |
+
chain = cl.user_session.get("qa_chain")
|
| 107 |
+
if chain:
|
| 108 |
+
try:
|
| 109 |
+
response = chain.run(message.content)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
response = f"β οΈ Error: {str(e)}"
|
| 112 |
+
await cl.Message(response).send()
|
chainlit.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Welcome to Chainlit! ππ€
|
| 2 |
+
|
| 3 |
+
Hi there, Developer! π We're excited to have you on board. Chainlit is a powerful tool designed to help you prototype, debug and share applications built on top of LLMs.
|
| 4 |
+
|
| 5 |
+
## Useful Links π
|
| 6 |
+
|
| 7 |
+
- **Documentation:** Get started with our comprehensive [Chainlit Documentation](https://docs.chainlit.io) π
|
| 8 |
+
- **Discord Community:** Join our friendly [Chainlit Discord](https://discord.gg/k73SQ3FyUh) to ask questions, share your projects, and connect with other developers! π¬
|
| 9 |
+
|
| 10 |
+
We can't wait to see what you create with Chainlit! Happy coding! π»π
|
| 11 |
+
|
| 12 |
+
## Welcome screen
|
| 13 |
+
|
| 14 |
+
To modify the welcome screen, edit the `chainlit.md` file at the root of your project. If you do not want a welcome screen, just leave this file empty.
|
combined_data.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pyproject.toml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "pilates_fine_tuned"
|
| 3 |
+
requires-python = ">=3.13"
|
| 4 |
+
version = "0.1.0"
|
| 5 |
+
description = "A fine-tuned pilates project."
|
| 6 |
+
dependencies = [
|
| 7 |
+
"langchain-huggingface>=0.0.6",
|
| 8 |
+
"chainlit>=2.5.5",
|
| 9 |
+
"faiss-cpu>=1.11.0",
|
| 10 |
+
"langchain>=0.3.25",
|
| 11 |
+
"langchain-community>=0.3.24",
|
| 12 |
+
"langchain-openai>=0.3.16",
|
| 13 |
+
"langchain-core>=0.0.1", # Added langchain-core
|
| 14 |
+
"pymupdf>=1.25.5",
|
| 15 |
+
"pytube>=15.0.0",
|
| 16 |
+
"unstructured>=0.17.2",
|
| 17 |
+
"youtube-transcript-api>=1.0.3",
|
| 18 |
+
"websockets==11.0.3"
|
| 19 |
+
]
|
| 20 |
+
|