Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,41 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import streamlit as st
|
| 4 |
|
|
@@ -6,4 +43,4 @@ import streamlit as st
|
|
| 6 |
|
| 7 |
if __name__ == "__main__":
|
| 8 |
port = int(os.environ.get("PORT", 7860)) # Default Hugging Face Spaces port
|
| 9 |
-
st.run(port=port, host="0.0.0.0")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pdf_extractor import get_pdf_text
|
| 3 |
+
from knowledge_base import create_faiss_index, search_faiss
|
| 4 |
+
from chatbot import generate_response
|
| 5 |
+
|
| 6 |
+
st.title("π€ Arabic PDF Chatbot")
|
| 7 |
+
|
| 8 |
+
# Step 1: Upload a PDF
|
| 9 |
+
uploaded_file = st.file_uploader("π Upload a PDF", type=["pdf"])
|
| 10 |
+
if uploaded_file:
|
| 11 |
+
with open("temp.pdf", "wb") as f:
|
| 12 |
+
f.write(uploaded_file.getbuffer())
|
| 13 |
+
|
| 14 |
+
st.success("β
PDF uploaded successfully!")
|
| 15 |
+
|
| 16 |
+
# Step 2: Extract text from PDF
|
| 17 |
+
st.info("π Extracting text from PDF...")
|
| 18 |
+
pdf_text = get_pdf_text("temp.pdf") # Load the PDF and extract text
|
| 19 |
+
texts = pdf_text.split("\n") # Split text into paragraphs
|
| 20 |
+
faiss_index, stored_texts = create_faiss_index(texts) # Store in FAISS
|
| 21 |
+
|
| 22 |
+
st.success("β
Knowledge base created!")
|
| 23 |
+
|
| 24 |
+
# Step 3: Chat Interface
|
| 25 |
+
st.header("π¬ Chat with Your PDF")
|
| 26 |
+
user_query = st.text_input("Ask a question:")
|
| 27 |
+
if user_query:
|
| 28 |
+
st.info("π Searching for relevant information...")
|
| 29 |
+
relevant_texts = search_faiss(faiss_index, stored_texts, user_query, top_k=3)
|
| 30 |
+
|
| 31 |
+
st.success("β
Found relevant content!")
|
| 32 |
+
context = "\n".join(relevant_texts)
|
| 33 |
+
|
| 34 |
+
st.info("π€ Generating answer...")
|
| 35 |
+
answer = generate_response(context, user_query)
|
| 36 |
+
|
| 37 |
+
st.write("**Chatbot Answer:**", answer)
|
| 38 |
+
|
| 39 |
import os
|
| 40 |
import streamlit as st
|
| 41 |
|
|
|
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
port = int(os.environ.get("PORT", 7860)) # Default Hugging Face Spaces port
|
| 46 |
+
st.run(port=port, host="0.0.0.0")
|