Huzaifa424 commited on
Commit
f853ea3
·
verified ·
1 Parent(s): dd2ab1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import streamlit as st
3
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
4
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
@@ -59,9 +58,7 @@ def handle_query(query):
59
  If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context
60
  of the document
61
 
62
-
63
 
64
-
65
  Context:
66
  {context_str}
67
  Question:
@@ -85,14 +82,14 @@ def handle_query(query):
85
  st.set_page_config(page_title="PDF Information and Inference", page_icon=":book:", layout="wide")
86
 
87
  st.title("📚 PDF Information and Inference")
88
- st.markdown("Welcome to the Retrieval-Augmented Generation tool! Upload a PDF and ask questions about its content.")
89
 
90
  # Sidebar for PDF upload and processing
91
  with st.sidebar:
92
  st.title("Menu")
93
  st.subheader("Upload and Process PDF")
94
  uploaded_file = st.file_uploader("Upload your PDF file here:", type="pdf")
95
- if st.button("Submit & Process"):
96
  if uploaded_file is not None:
97
  with st.spinner("Processing..."):
98
  filepath = os.path.join(DATA_DIR, "saved_pdf.pdf")
@@ -106,10 +103,21 @@ with st.sidebar:
106
  st.sidebar.error("Please upload a PDF file first.")
107
 
108
  # Main section for user interaction
109
- st.header("Chat with the PDF")
110
- user_prompt = st.text_input("Ask me anything about the content of the PDF:")
 
 
 
 
 
 
111
 
112
  if user_prompt:
113
  with st.spinner("Fetching answer..."):
114
- response = handle_query(user_prompt)
115
- st.write(response)
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
3
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
 
58
  If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context
59
  of the document
60
 
 
61
 
 
62
  Context:
63
  {context_str}
64
  Question:
 
82
  st.set_page_config(page_title="PDF Information and Inference", page_icon=":book:", layout="wide")
83
 
84
  st.title("📚 PDF Information and Inference")
85
+ st.markdown("Welcome to the Retrieval-Augmented Generation tool! You can either upload a PDF or chat with the language model directly.")
86
 
87
  # Sidebar for PDF upload and processing
88
  with st.sidebar:
89
  st.title("Menu")
90
  st.subheader("Upload and Process PDF")
91
  uploaded_file = st.file_uploader("Upload your PDF file here:", type="pdf")
92
+ if st.button("Submit & Process PDF"):
93
  if uploaded_file is not None:
94
  with st.spinner("Processing..."):
95
  filepath = os.path.join(DATA_DIR, "saved_pdf.pdf")
 
103
  st.sidebar.error("Please upload a PDF file first.")
104
 
105
  # Main section for user interaction
106
+ st.header("Chat with the PDF or the LLM")
107
+
108
+ if uploaded_file:
109
+ st.write("You can now ask questions about the uploaded PDF.")
110
+ else:
111
+ st.write("No PDF uploaded. You can still chat with the LLM.")
112
+
113
+ user_prompt = st.text_input("Ask me anything:")
114
 
115
  if user_prompt:
116
  with st.spinner("Fetching answer..."):
117
+ if uploaded_file:
118
+ # Handle query with PDF context
119
+ response = handle_query(user_prompt)
120
+ else:
121
+ # Handle query without PDF, just using the LLM
122
+ response = Settings.llm.generate([user_prompt])
123
+ st.write(response[0]["generated_text"] if isinstance(response, list) else response)