Spaces:
Paused
Paused
Fix ASGI app error and stabilize Streamlit with top-level api_app
Browse files
README.md
CHANGED
|
@@ -11,12 +11,12 @@ pinned: false
|
|
| 11 |
|
| 12 |
# RAG-Based Question Answering System
|
| 13 |
|
| 14 |
-
This system uses **
|
| 15 |
|
| 16 |
## 🚀 How it works
|
| 17 |
|
| 18 |
-
1. **Upload**:
|
| 19 |
-
2. **Retrieve**: FAISS finds the most relevant chunks.
|
| 20 |
3. **Generate**: Gemini 1.5 Flash synthesizes an answer using **ZeroGPU**.
|
| 21 |
|
| 22 |
## 🛠️ API Access
|
|
@@ -26,4 +26,6 @@ The FastAPI endpoints are available at:
|
|
| 26 |
- `POST /upload`: Upload documents.
|
| 27 |
- `POST /ask`: Query the documents.
|
| 28 |
|
|
|
|
|
|
|
| 29 |
Make sure to set `GOOGLE_API_KEY` in your Space Secrets.
|
|
|
|
| 11 |
|
| 12 |
# RAG-Based Question Answering System
|
| 13 |
|
| 14 |
+
This system uses **Streamlit** for its user interface and **FastAPI** for its background API, running on **ZeroGPU**.
|
| 15 |
|
| 16 |
## 🚀 How it works
|
| 17 |
|
| 18 |
+
1. **Upload**: Use the sidebar to upload PDF or TXT files.
|
| 19 |
+
2. **Retrieve**: FAISS finds the most relevant chunks from your documents.
|
| 20 |
3. **Generate**: Gemini 1.5 Flash synthesizes an answer using **ZeroGPU**.
|
| 21 |
|
| 22 |
## 🛠️ API Access
|
|
|
|
| 26 |
- `POST /upload`: Upload documents.
|
| 27 |
- `POST /ask`: Query the documents.
|
| 28 |
|
| 29 |
+
_Note: The API runs on a background thread within the Streamlit process._
|
| 30 |
+
|
| 31 |
Make sure to set `GOOGLE_API_KEY` in your Space Secrets.
|
app.py
CHANGED
|
@@ -16,156 +16,120 @@ from app.services.document_processor import DocumentProcessor
|
|
| 16 |
from app.services.vector_store import vector_store
|
| 17 |
from app.services.llm_service import llm_service
|
| 18 |
|
| 19 |
-
# ---
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
uvicorn.run(api_app, host="0.0.0.0", port=8000)
|
| 52 |
|
| 53 |
-
# Start API
|
| 54 |
-
if "
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
st.session_state.api_thread = True
|
| 58 |
|
| 59 |
-
#
|
| 60 |
st.set_page_config(
|
| 61 |
-
page_title="Knowledge Nexus
|
| 62 |
page_icon="📚",
|
| 63 |
layout="wide"
|
| 64 |
)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
st.markdown("""
|
| 68 |
-
|
| 69 |
-
.main {
|
| 70 |
-
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
background-color: #4f46e5;
|
| 77 |
-
color: white;
|
| 78 |
-
border: none;
|
| 79 |
-
}
|
| 80 |
-
.stButton>button:hover {
|
| 81 |
-
background-color: #4338ca;
|
| 82 |
-
color: white;
|
| 83 |
-
}
|
| 84 |
-
.upload-box {
|
| 85 |
-
border: 2px dashed #cbd5e1;
|
| 86 |
-
padding: 20px;
|
| 87 |
-
border-radius: 15px;
|
| 88 |
-
text-align: center;
|
| 89 |
-
}
|
| 90 |
-
.status-text {
|
| 91 |
-
color: #64748b;
|
| 92 |
-
font-size: 0.9em;
|
| 93 |
-
}
|
| 94 |
-
/* Hide Streamlit Branding */
|
| 95 |
-
#MainMenu {visibility: hidden;}
|
| 96 |
-
footer {visibility: hidden;}
|
| 97 |
-
header {visibility: hidden;}
|
| 98 |
-
</style>
|
| 99 |
-
""", unsafe_allow_html=True)
|
| 100 |
-
|
| 101 |
-
# Zero-GPU Decorated Function
|
| 102 |
@spaces.GPU
|
| 103 |
def get_llm_response(query: str, chunks: List):
|
| 104 |
return llm_service.generate_answer(query, chunks)
|
| 105 |
|
| 106 |
-
# Sidebar
|
| 107 |
with st.sidebar:
|
| 108 |
st.title("Knowledge Nexus")
|
| 109 |
st.markdown("---")
|
| 110 |
-
st.header("
|
| 111 |
-
st.write("
|
| 112 |
|
| 113 |
-
uploaded_file = st.file_uploader("
|
| 114 |
|
| 115 |
-
if uploaded_file
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
except Exception as e:
|
| 132 |
-
st.error(f"Error processing: {str(e)}")
|
| 133 |
-
finally:
|
| 134 |
-
if os.path.exists(temp_path):
|
| 135 |
-
os.remove(temp_path)
|
| 136 |
|
| 137 |
st.markdown("---")
|
| 138 |
-
st.
|
| 139 |
-
st.
|
|
|
|
| 140 |
|
| 141 |
-
#
|
| 142 |
st.title("Document Interaction")
|
| 143 |
-
st.write("Ask questions based on your uploaded knowledge base.")
|
| 144 |
|
| 145 |
if "messages" not in st.session_state:
|
| 146 |
st.session_state.messages = []
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
st.markdown(message["content"])
|
| 152 |
|
| 153 |
-
|
| 154 |
-
if prompt := st.chat_input("Enter your question..."):
|
| 155 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 156 |
with st.chat_message("user"):
|
| 157 |
st.markdown(prompt)
|
| 158 |
|
| 159 |
with st.chat_message("assistant"):
|
| 160 |
-
with st.spinner("Searching
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
response = "I could not find any relevant information in the linked documents."
|
| 165 |
else:
|
| 166 |
-
|
| 167 |
-
sources = list(set([c['source'] for c in
|
| 168 |
-
response = f"{
|
| 169 |
-
|
| 170 |
st.markdown(response)
|
| 171 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
| 16 |
from app.services.vector_store import vector_store
|
| 17 |
from app.services.llm_service import llm_service
|
| 18 |
|
| 19 |
+
# --- FASTAPI APP DEFINITION (Exposed at top level) ---
|
| 20 |
+
api_app = FastAPI(title="RAG API")
|
| 21 |
+
api_app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 22 |
+
|
| 23 |
+
@api_app.post("/upload")
|
| 24 |
+
async def api_upload(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
|
| 25 |
+
file_id = str(uuid.uuid4())
|
| 26 |
+
temp_path = os.path.join(settings.UPLOADS_DIR, f"{file_id}_{file.filename}")
|
| 27 |
+
os.makedirs(settings.UPLOADS_DIR, exist_ok=True)
|
| 28 |
+
with open(temp_path, "wb") as buffer:
|
| 29 |
+
shutil.copyfileobj(file.file, buffer)
|
| 30 |
+
def process_task(path, name):
|
| 31 |
+
text = DocumentProcessor.extract_text(path)
|
| 32 |
+
chunks = DocumentProcessor.chunk_text(text)
|
| 33 |
+
vector_store.add_documents(chunks, name)
|
| 34 |
+
if os.path.exists(path): os.remove(path)
|
| 35 |
+
background_tasks.add_task(process_task, temp_path, file.filename)
|
| 36 |
+
return {"message": "Success", "filename": file.filename}
|
| 37 |
+
|
| 38 |
+
@api_app.post("/ask")
|
| 39 |
+
async def api_ask(request: Request):
|
| 40 |
+
data = await request.json()
|
| 41 |
+
question = data.get("question")
|
| 42 |
+
relevant_chunks = vector_store.search(question)
|
| 43 |
+
if not relevant_chunks: return {"answer": "No info", "sources": []}
|
| 44 |
+
answer = llm_service.generate_answer(question, relevant_chunks)
|
| 45 |
+
sources = list(set([c['source'] for c in relevant_chunks]))
|
| 46 |
+
return {"answer": answer, "sources": sources}
|
| 47 |
+
|
| 48 |
+
# --- BACKGROUND API RUNNER ---
|
| 49 |
+
def run_api_server():
|
| 50 |
+
# Run on a different port than Streamlit (7860)
|
| 51 |
uvicorn.run(api_app, host="0.0.0.0", port=8000)
|
| 52 |
|
| 53 |
+
# Start API thread
|
| 54 |
+
if "api_started" not in st.session_state:
|
| 55 |
+
st.session_state.api_started = True
|
| 56 |
+
threading.Thread(target=run_api_server, daemon=True).start()
|
|
|
|
| 57 |
|
| 58 |
+
# --- STREAMLIT UI ---
|
| 59 |
st.set_page_config(
|
| 60 |
+
page_title="Knowledge Nexus",
|
| 61 |
page_icon="📚",
|
| 62 |
layout="wide"
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Premium Text-Only CSS
|
| 66 |
st.markdown("""
|
| 67 |
+
<style>
|
| 68 |
+
.main { background-color: #f8fafc; }
|
| 69 |
+
.stChatFloatingInputContainer { bottom: 20px; }
|
| 70 |
+
footer { visibility: hidden; }
|
| 71 |
+
header { visibility: hidden; }
|
| 72 |
+
</style>
|
| 73 |
+
""", unsafe_allow_html=True)
|
| 74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@spaces.GPU
|
| 76 |
def get_llm_response(query: str, chunks: List):
|
| 77 |
return llm_service.generate_answer(query, chunks)
|
| 78 |
|
| 79 |
+
# Sidebar
|
| 80 |
with st.sidebar:
|
| 81 |
st.title("Knowledge Nexus")
|
| 82 |
st.markdown("---")
|
| 83 |
+
st.header("Document Upload")
|
| 84 |
+
st.write("Upload PDF/TXT to link them to the AI.")
|
| 85 |
|
| 86 |
+
uploaded_file = st.file_uploader("Upload", type=["pdf", "txt"], label_visibility="collapsed")
|
| 87 |
|
| 88 |
+
if uploaded_file and st.button("Index Document"):
|
| 89 |
+
with st.spinner("Processing..."):
|
| 90 |
+
file_id = str(uuid.uuid4())
|
| 91 |
+
temp_path = os.path.join(settings.UPLOADS_DIR, f"{file_id}_{uploaded_file.name}")
|
| 92 |
+
os.makedirs(settings.UPLOADS_DIR, exist_ok=True)
|
| 93 |
+
with open(temp_path, "wb") as f:
|
| 94 |
+
f.write(uploaded_file.getbuffer())
|
| 95 |
+
try:
|
| 96 |
+
text = DocumentProcessor.extract_text(temp_path)
|
| 97 |
+
chunks = DocumentProcessor.chunk_text(text)
|
| 98 |
+
vector_store.add_documents(chunks, uploaded_file.name)
|
| 99 |
+
st.success(f"Linked: {uploaded_file.name}")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
st.error(f"Error: {str(e)}")
|
| 102 |
+
finally:
|
| 103 |
+
if os.path.exists(temp_path): os.remove(temp_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
st.markdown("---")
|
| 106 |
+
st.write("Model: Gemini 1.5 Flash")
|
| 107 |
+
st.write("Retrieval: FAISS")
|
| 108 |
+
st.write("Compute: ZeroGPU")
|
| 109 |
|
| 110 |
+
# Chat UI
|
| 111 |
st.title("Document Interaction")
|
|
|
|
| 112 |
|
| 113 |
if "messages" not in st.session_state:
|
| 114 |
st.session_state.messages = []
|
| 115 |
|
| 116 |
+
for m in st.session_state.messages:
|
| 117 |
+
with st.chat_message(m["role"]):
|
| 118 |
+
st.markdown(m["content"])
|
|
|
|
| 119 |
|
| 120 |
+
if prompt := st.chat_input("Ask a question about your documents..."):
|
|
|
|
| 121 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 122 |
with st.chat_message("user"):
|
| 123 |
st.markdown(prompt)
|
| 124 |
|
| 125 |
with st.chat_message("assistant"):
|
| 126 |
+
with st.spinner("Searching..."):
|
| 127 |
+
chunks = vector_store.search(prompt)
|
| 128 |
+
if not chunks:
|
| 129 |
+
response = "No relevant information found."
|
|
|
|
| 130 |
else:
|
| 131 |
+
ans = get_llm_response(prompt, chunks)
|
| 132 |
+
sources = list(set([c['source'] for c in chunks]))
|
| 133 |
+
response = f"{ans}\n\nSources: {', '.join(sources)}"
|
|
|
|
| 134 |
st.markdown(response)
|
| 135 |
st.session_state.messages.append({"role": "assistant", "content": response})
|