File size: 4,552 Bytes
0dc3cfb |
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"]}]}]} |