Spaces:
Sleeping
Sleeping
Upload app_hf_space.py
Browse files- app_hf_space.py +49 -0
app_hf_space.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app_hf_space.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 4 |
+
from langchain_community.vectorstores.faiss import FAISS
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
from langchain_huggingface.llms import HuggingFacePipeline
|
| 7 |
+
|
| 8 |
+
# --- Configuration ---
|
| 9 |
+
# Use Hugging Face inference API via the `pipeline` from langchain-huggingface
|
| 10 |
+
EMBED_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
|
| 11 |
+
LLM_MODEL_ID = "google/flan-t5-small"
|
| 12 |
+
INDEX_DIR = "faiss_index"
|
| 13 |
+
|
| 14 |
+
# 1. Initialize embeddings via HF Inference API
|
| 15 |
+
embeddings = HuggingFaceEmbeddings(
|
| 16 |
+
model_name=EMBED_MODEL_ID,
|
| 17 |
+
cache_dir=".hf_cache"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# 2. Load FAISS index
|
| 21 |
+
store = FAISS.load_local(
|
| 22 |
+
INDEX_DIR,
|
| 23 |
+
embeddings
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# 3. Initialize HF LLM via pipeline (inference API)
|
| 27 |
+
llm = HuggingFacePipeline.from_model_id(
|
| 28 |
+
model_id=LLM_MODEL_ID,
|
| 29 |
+
task="text2text-generation"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# 4. Build RetrievalQA chain
|
| 33 |
+
aqa_chain = RetrievalQA.from_chain_type(
|
| 34 |
+
llm=llm,
|
| 35 |
+
chain_type="stuff",
|
| 36 |
+
retriever=store.as_retriever()
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# 5. Streamlit UI
|
| 40 |
+
st.title("🦜🔗 RAG App via HF Spaces")
|
| 41 |
+
query = st.text_input("Ask a question about your docs:")
|
| 42 |
+
|
| 43 |
+
if query:
|
| 44 |
+
with st.spinner("Generating answer via HF Space..."):
|
| 45 |
+
answer = aqa_chain.run(query)
|
| 46 |
+
st.markdown(f"**Answer:** {answer}")
|
| 47 |
+
|
| 48 |
+
# NOTE: Deploy this to Hugging Face Spaces for fully-managed hosting.
|
| 49 |
+
# Just push this file to your repo on HF and enable Streamlit space.
|