amn-sdqi commited on
Commit
ff490f3
·
1 Parent(s): c933fc0

beta chat feature

Browse files
Files changed (2) hide show
  1. app.py +41 -28
  2. app_bkp.py +37 -0
app.py CHANGED
@@ -1,46 +1,59 @@
1
- # import os
2
- # os.environ["USE_TF"] = "0" # Force PyTorch, avoids Keras issues
3
-
4
  import streamlit as st
5
  from transformers import pipeline
6
- import fitz # PyMuPDF
7
-
8
 
9
- # Load the model once
10
- # @st.cache_resource
11
- # def load_pipeline():
12
- # return pipeline(
13
- # "question-answering", model="distilbert-base-uncased-distilled-squad"
14
- # )
15
 
16
- question_answerer = pipeline(
17
- "question-answering", model="distilbert-base-cased-distilled-squad"
18
- )
19
 
20
- # Extract text from uploaded PDF
21
- def extract_text_from_pdf(uploaded_file):
22
  text = ""
23
- with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
24
  for page in doc:
25
  text += page.get_text()
26
  return text
27
 
28
 
29
- st.title("PDF Chatbot with Transformers")
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- st.subheader("Upload a PDF Document")
32
- pdf_file = st.file_uploader("Upload your PDF file", type="pdf")
33
 
34
- if pdf_file is not None:
35
- context = extract_text_from_pdf(pdf_file)
 
36
 
37
- st.subheader("Ask a Question")
38
- question = st.text_input("Your question:", "What is this document about?")
 
 
 
 
 
39
 
40
  if question:
41
- result = question_answerer(question=question, context=context)
42
- st.write("**Answer:**", result["answer"])
43
- else:
44
- st.info("Please upload a PDF to begin.")
45
 
 
 
46
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import fitz
 
4
 
5
+ qa = pipeline("question-answring", model="distilbert-base-cased-distilled-squad")
 
 
 
 
 
6
 
 
 
 
7
 
8
+ # extract text from uploaded document
9
+ def extract_PDF(file):
10
  text = ""
11
+ with fitz.open(stream=file.read(), filetype="pdf") as doc:
12
  for page in doc:
13
  text += page.get_text()
14
  return text
15
 
16
 
17
+ # ------------------------------------------------------------------------------
18
+
19
+ # -----------------------------------Streamlit UI--------------------------------
20
+
21
+ st.title("Chatbot with Huggingface")
22
+
23
+ st.subheader("Upload file")
24
+ pdf_file = st.file_uploader("Upload", type="pdf")
25
+
26
+ # Initialize Session state for convo history
27
+
28
+ if "chat_history" not in st.session_state:
29
+ st.session_state.chat_history = []
30
 
31
+ if "context" not in st.session_state:
32
+ st.session_state.context = None
33
 
34
+ # extract text and store in the session
35
+ if pdf_file is not None and st.session.context is None:
36
+ st.session_state.context = extract_PDF(pdf_file)
37
 
38
+
39
+ # Chat section
40
+
41
+ if st.session_state.context:
42
+ st.subheader("Chat with the PDF")
43
+
44
+ question = st.text_input("You", key="user_input")
45
 
46
  if question:
47
+ result = qa(question=question, context=st.session_state.context)
 
 
 
48
 
49
+ # save convo
50
+ st.session_state.chat_history.apend({"user": question, "bot": result["answer"]})
51
 
52
+ # Display chat
53
+
54
+ for chat in st.session_state.chat_history:
55
+ st.markdown(f"**You:** {chat['user']}")
56
+ st.markdown(f"**Bot:** {chat['bot']}")
57
+
58
+ else:
59
+ st.info("Please upload PDF to begin")
app_bkp.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import fitz # PyMuPDF
4
+
5
+
6
+
7
+ question_answerer = pipeline(
8
+ "question-answering", model="distilbert-base-cased-distilled-squad"
9
+ )
10
+
11
+ # Extract text from uploaded PDF
12
+ def extract_text_from_pdf(uploaded_file):
13
+ text = ""
14
+ with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
15
+ for page in doc:
16
+ text += page.get_text()
17
+ return text
18
+
19
+
20
+ st.title("PDF Chatbot with Transformers")
21
+
22
+ st.subheader("Upload a PDF Document")
23
+ pdf_file = st.file_uploader("Upload your PDF file", type="pdf")
24
+
25
+ if pdf_file is not None:
26
+ context = extract_text_from_pdf(pdf_file)
27
+
28
+ st.subheader("Ask a Question")
29
+ question = st.text_input("Your question:", "What is this document about?")
30
+
31
+ if question:
32
+ result = question_answerer(question=question, context=context)
33
+ st.write("**Answer:**", result["answer"])
34
+ else:
35
+ st.info("Please upload a PDF to begin.")
36
+
37
+