Ralqasimi commited on
Commit
4f7d52a
Β·
verified Β·
1 Parent(s): 2cbf54a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -2
app.py CHANGED
@@ -1,4 +1,41 @@
1
- {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyODFlThkKHgwc/6sqL8iPou"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":4,"metadata":{"id":"GtTlAPZ7PKSc","colab":{"base_uri":"https://localhost:8080/","height":397},"executionInfo":{"status":"error","timestamp":1738945860520,"user_tz":-180,"elapsed":29,"user":{"displayName":"Rawan Alqassimi","userId":"02524741001548264321"}},"outputId":"2526ad44-8969-4045-ff97-767b70f2452d"},"outputs":[{"output_type":"error","ename":"ModuleNotFoundError","evalue":"No module named 'streamlit'","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)","\u001b[0;32m<ipython-input-4-d702174ecc8f>\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mstreamlit\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mst\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpdf_extractor\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mget_pdf_text\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mknowledge_base\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcreate_faiss_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msearch_faiss\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mchatbot\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mgenerate_response\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'streamlit'","","\u001b[0;31m---------------------------------------------------------------------------\u001b[0;32m\nNOTE: If your import is failing due to a missing package, you can\nmanually install dependencies using either !pip or !apt.\n\nTo view examples of installing some common dependencies, click the\n\"Open Examples\" button below.\n\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n"],"errorDetails":{"actions":[{"action":"open_url","actionText":"Open Examples","url":"/notebooks/snippets/importing_libraries.ipynb"}]}}],"source":["import streamlit as st\n","from pdf_extractor import get_pdf_text\n","from knowledge_base import create_faiss_index, search_faiss\n","from chatbot import generate_response\n","\n","st.title(\"πŸ€– Arabic PDF Chatbot\")\n","\n","# Step 1: Upload a PDF\n","uploaded_file = st.file_uploader(\"πŸ“‚ Upload a PDF\", type=[\"pdf\"])\n","if uploaded_file:\n"," with open(\"temp.pdf\", \"wb\") as f:\n"," f.write(uploaded_file.getbuffer())\n","\n"," st.success(\"βœ… PDF uploaded successfully!\")\n","\n"," # Step 2: Extract text from PDF\n"," st.info(\"πŸ”„ Extracting text from PDF...\")\n"," pdf_text = get_pdf_text(\"temp.pdf\") # Load the PDF and extract text\n"," texts = pdf_text.split(\"\\n\") # Split text into paragraphs\n"," faiss_index, stored_texts = create_faiss_index(texts) # Store in FAISS\n","\n"," st.success(\"βœ… Knowledge base created!\")\n","\n"," # Step 3: Chat Interface\n"," st.header(\"πŸ’¬ Chat with Your PDF\")\n"," user_query = st.text_input(\"Ask a question:\")\n"," if user_query:\n"," st.info(\"πŸ” Searching for relevant information...\")\n"," relevant_texts = search_faiss(faiss_index, stored_texts, user_query, top_k=3)\n","\n"," st.success(\"βœ… Found relevant content!\")\n"," context = \"\\n\".join(relevant_texts)\n","\n"," st.info(\"πŸ€– Generating answer...\")\n"," answer = generate_response(context, user_query)\n","\n"," st.write(\"**Chatbot Answer:**\", answer)"]},{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"DrdogNvbPgr4","executionInfo":{"status":"ok","timestamp":1738945847342,"user_tz":-180,"elapsed":9029,"user":{"displayName":"Rawan Alqassimi","userId":"02524741001548264321"}},"outputId":"6218e51e-f486-49e1-f322-f31e6bc18f1b"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\n"]}]}]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")