Mattral commited on
Commit
9578d72
·
verified ·
1 Parent(s): 2e44d20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -47
app.py CHANGED
@@ -1,6 +1,11 @@
1
  import streamlit as st
2
  import requests
3
  import logging
 
 
 
 
 
4
 
5
  # Configure logging
6
  logging.basicConfig(level=logging.INFO)
@@ -61,6 +66,36 @@ def query(payload, api_url):
61
  logger.error(f"Failed to decode JSON response: {response.text}")
62
  return None
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # Chat interface
65
  st.title("🤖 DeepSeek Chatbot")
66
  st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
@@ -70,59 +105,75 @@ for message in st.session_state.messages:
70
  with st.chat_message(message["role"]):
71
  st.markdown(message["content"])
72
 
73
- # Handle input
74
- if prompt := st.chat_input("Type your message..."):
75
- st.session_state.messages.append({"role": "user", "content": prompt})
76
-
77
- with st.chat_message("user"):
78
- st.markdown(prompt)
79
-
80
- try:
81
- with st.spinner("Generating response..."):
82
- # Prepare the payload for the API
83
- # Combine system message and user input into a single prompt
84
- full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
85
- payload = {
86
- "inputs": full_prompt,
87
- "parameters": {
88
- "max_new_tokens": max_tokens,
89
- "temperature": temperature,
90
- "top_p": top_p,
91
- "return_full_text": False
 
 
 
 
 
 
92
  }
93
- }
94
 
95
- # Dynamically construct the API URL based on the selected model
96
- api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
97
- logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
98
 
99
- # Query the Hugging Face API using the selected model
100
- output = query(payload, api_url)
101
 
102
- # Handle API response
103
- if output is not None and isinstance(output, list) and len(output) > 0:
104
- if 'generated_text' in output[0]:
105
- # Extract the assistant's response
106
- assistant_response = output[0]['generated_text'].strip()
107
 
108
- # Check for and remove duplicate responses
109
- responses = assistant_response.split("\n</think>\n")
110
- unique_response = responses[0].strip()
111
 
112
- logger.info(f"Generated response: {unique_response}")
113
 
114
- # Append response to chat only once
115
- with st.chat_message("assistant"):
116
- st.markdown(unique_response)
117
 
118
- st.session_state.messages.append({"role": "assistant", "content": unique_response})
 
 
 
119
  else:
120
- logger.error(f"Unexpected API response structure: {output}")
121
- st.error("Error: Unexpected response from the model. Please try again.")
122
- else:
123
- logger.error(f"Empty or invalid API response: {output}")
124
- st.error("Error: Unable to generate a response. Please check the model and try again.")
125
-
126
- except Exception as e:
127
- logger.error(f"Application Error: {str(e)}", exc_info=True)
128
- st.error(f"Application Error: {str(e)}")
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import requests
3
  import logging
4
+ from langchain.document_loaders import PDFPlumberLoader
5
+ from langchain.text_splitters import RecursiveCharacterTextSplitter
6
+ from langchain.prompts import ChatPromptTemplate
7
+ from langchain.llms import HuggingFacePipeline
8
+ from transformers import pipeline
9
 
10
  # Configure logging
11
  logging.basicConfig(level=logging.INFO)
 
66
  logger.error(f"Failed to decode JSON response: {response.text}")
67
  return None
68
 
69
+ # Function to load and process PDF
70
+ def process_pdf(uploaded_file):
71
+ loader = PDFPlumberLoader(uploaded_file)
72
+ documents = loader.load()
73
+
74
+ # Split the documents into chunks
75
+ text_splitter = RecursiveCharacterTextSplitter(
76
+ chunk_size=1000,
77
+ chunk_overlap=200,
78
+ add_start_index=True
79
+ )
80
+ return text_splitter.split_documents(documents)
81
+
82
+ # Function to generate response using LangChain
83
+ def generate_response_with_langchain(question, context):
84
+ prompt_template = """
85
+ You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
86
+ Question: {question}
87
+ Context: {context}
88
+ Answer:
89
+ """
90
+
91
+ prompt = ChatPromptTemplate.from_template(prompt_template)
92
+ model = HuggingFacePipeline(pipeline("text-generation", model=selected_model))
93
+
94
+ # Use LangChain to generate an answer
95
+ chain = prompt | model
96
+ response = chain.invoke({"question": question, "context": context})
97
+ return response
98
+
99
  # Chat interface
100
  st.title("🤖 DeepSeek Chatbot")
101
  st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
 
105
  with st.chat_message(message["role"]):
106
  st.markdown(message["content"])
107
 
108
+ # Handle input and PDF processing
109
+ uploaded_file = st.file_uploader("Upload PDF", type="pdf", accept_multiple_files=False)
110
+ if uploaded_file:
111
+ documents = process_pdf(uploaded_file)
112
+ context = "\n\n".join([doc.page_content for doc in documents])
113
+
114
+ # Ask the user a question
115
+ if prompt := st.chat_input("Type your message..."):
116
+ st.session_state.messages.append({"role": "user", "content": prompt})
117
+
118
+ with st.chat_message("user"):
119
+ st.markdown(prompt)
120
+
121
+ try:
122
+ with st.spinner("Generating response..."):
123
+ # Combine system message and user input into a single prompt
124
+ full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
125
+ payload = {
126
+ "inputs": full_prompt,
127
+ "parameters": {
128
+ "max_new_tokens": max_tokens,
129
+ "temperature": temperature,
130
+ "top_p": top_p,
131
+ "return_full_text": False
132
+ }
133
  }
 
134
 
135
+ # Dynamically construct the API URL based on the selected model
136
+ api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
137
+ logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
138
 
139
+ # Query the Hugging Face API using the selected model
140
+ output = query(payload, api_url)
141
 
142
+ # Handle API response
143
+ if output is not None and isinstance(output, list) and len(output) > 0:
144
+ if 'generated_text' in output[0]:
145
+ assistant_response = output[0]['generated_text'].strip()
 
146
 
147
+ # Check for and remove duplicate responses
148
+ responses = assistant_response.split("\n</think>\n")
149
+ unique_response = responses[0].strip()
150
 
151
+ logger.info(f"Generated response: {unique_response}")
152
 
153
+ # Append response to chat only once
154
+ with st.chat_message("assistant"):
155
+ st.markdown(unique_response)
156
 
157
+ st.session_state.messages.append({"role": "assistant", "content": unique_response})
158
+ else:
159
+ logger.error(f"Unexpected API response structure: {output}")
160
+ st.error("Error: Unexpected response from the model. Please try again.")
161
  else:
162
+ logger.error(f"Empty or invalid API response: {output}")
163
+ st.error("Error: Unable to generate a response. Please check the model and try again.")
164
+
165
+ except Exception as e:
166
+ logger.error(f"Application Error: {str(e)}", exc_info=True)
167
+ st.error(f"Application Error: {str(e)}")
168
+
169
+ # Allow user to ask a question based on extracted PDF content
170
+ if prompt := st.chat_input("Ask a question about the PDF content"):
171
+ if documents:
172
+ context = "\n\n".join([doc.page_content for doc in documents]) # Get context from documents
173
+ answer = generate_response_with_langchain(prompt, context)
174
+
175
+ # Show the answer from LangChain model
176
+ with st.chat_message("assistant"):
177
+ st.markdown(answer)
178
+
179
+ st.session_state.messages.append({"role": "assistant", "content": answer})