Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
from transformers import pipeline
|
| 4 |
from sentence_transformers import SentenceTransformer, util
|
| 5 |
import PyPDF2
|
|
|
|
| 6 |
|
| 7 |
# Set up logging with a dedicated file handler
|
| 8 |
logger = logging.getLogger('SupportBot')
|
|
@@ -10,24 +11,36 @@ logger.setLevel(logging.INFO)
|
|
| 10 |
# Remove any existing handlers to avoid conflicts
|
| 11 |
if logger.handlers:
|
| 12 |
logger.handlers.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Create a file handler with append mode
|
| 14 |
-
|
| 15 |
-
|
| 16 |
formatter = logging.Formatter('%(asctime)s - %(message)s')
|
| 17 |
-
|
| 18 |
-
logger.addHandler(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Load models
|
| 21 |
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 22 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 23 |
|
| 24 |
-
# Helper function to extract text from PDF
|
| 25 |
def extract_text_from_pdf(file_path):
|
| 26 |
text = ""
|
| 27 |
with open(file_path, "rb") as file:
|
| 28 |
pdf_reader = PyPDF2.PdfReader(file)
|
| 29 |
for page in pdf_reader.pages:
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
return text
|
| 32 |
|
| 33 |
# Find the most relevant section in the document
|
|
@@ -44,7 +57,7 @@ def find_relevant_section(query, sections, section_embeddings):
|
|
| 44 |
SIMILARITY_THRESHOLD = 0.4
|
| 45 |
if similarity_score >= SIMILARITY_THRESHOLD:
|
| 46 |
logger.info(f"Found relevant section using embeddings (score: {similarity_score})")
|
| 47 |
-
|
| 48 |
return best_section
|
| 49 |
|
| 50 |
logger.info(f"Low similarity ({similarity_score}). Falling back to keyword search.")
|
|
@@ -54,11 +67,11 @@ def find_relevant_section(query, sections, section_embeddings):
|
|
| 54 |
common_words = query_words.intersection(section_words)
|
| 55 |
if len(common_words) >= 2:
|
| 56 |
logger.info(f"Keyword match found with common words: {common_words}")
|
| 57 |
-
|
| 58 |
return section
|
| 59 |
|
| 60 |
logger.info("No good match found. Returning default response.")
|
| 61 |
-
|
| 62 |
return "I don’t have enough information to answer that."
|
| 63 |
|
| 64 |
# Process the uploaded file
|
|
@@ -66,20 +79,25 @@ def process_file(file, state):
|
|
| 66 |
logger.info("Received file upload request")
|
| 67 |
if file is None:
|
| 68 |
logger.info("No file uploaded")
|
| 69 |
-
|
| 70 |
return [("Bot", "Please upload a file.")], state
|
| 71 |
|
|
|
|
| 72 |
file_path = file.name
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
text = f.read()
|
| 80 |
else:
|
| 81 |
-
logger.error(f"Unsupported file format: {
|
| 82 |
-
|
| 83 |
return [("Bot", "Unsupported file format. Please upload a PDF or TXT file.")], state
|
| 84 |
|
| 85 |
sections = text.split('\n\n')
|
|
@@ -91,8 +109,8 @@ def process_file(file, state):
|
|
| 91 |
state['feedback_count'] = 0
|
| 92 |
state['mode'] = 'waiting_for_query'
|
| 93 |
state['chat_history'] = [("Bot", "File processed. You can now ask questions.")]
|
| 94 |
-
logger.info(f"File processed successfully: {
|
| 95 |
-
|
| 96 |
return state['chat_history'], state
|
| 97 |
|
| 98 |
# Handle user input (queries and feedback)
|
|
@@ -100,7 +118,7 @@ def handle_input(user_input, state):
|
|
| 100 |
if state['mode'] == 'waiting_for_upload':
|
| 101 |
logger.info("User input received before file upload")
|
| 102 |
state['chat_history'].append(("Bot", "Please upload a file first."))
|
| 103 |
-
|
| 104 |
elif state['mode'] == 'waiting_for_query':
|
| 105 |
query = user_input
|
| 106 |
logger.info(f"User query: {query}")
|
|
@@ -117,7 +135,7 @@ def handle_input(user_input, state):
|
|
| 117 |
state['chat_history'].append(("User", query))
|
| 118 |
state['chat_history'].append(("Bot", f"Answer: {answer}\nPlease provide feedback: good, too vague, not helpful."))
|
| 119 |
logger.info(f"Generated answer: {answer}")
|
| 120 |
-
|
| 121 |
elif state['mode'] == 'waiting_for_feedback':
|
| 122 |
feedback = user_input.lower()
|
| 123 |
logger.info(f"User feedback: {feedback}")
|
|
@@ -130,7 +148,7 @@ def handle_input(user_input, state):
|
|
| 130 |
else:
|
| 131 |
state['chat_history'].append(("Bot", "Maximum feedback iterations reached. You can ask another question."))
|
| 132 |
logger.info("Max feedback iterations (2) reached. Ready for next query.")
|
| 133 |
-
|
| 134 |
else:
|
| 135 |
query = state['current_query']
|
| 136 |
context = find_relevant_section(query, state['sections'], state['section_embeddings'])
|
|
@@ -143,13 +161,13 @@ def handle_input(user_input, state):
|
|
| 143 |
else:
|
| 144 |
state['chat_history'].append(("Bot", "Please provide valid feedback: good, too vague, not helpful."))
|
| 145 |
logger.info(f"Invalid feedback received: {feedback}")
|
| 146 |
-
|
| 147 |
return state['chat_history'], state
|
| 148 |
state['last_answer'] = adjusted_answer
|
| 149 |
state['feedback_count'] += 1
|
| 150 |
state['chat_history'].append(("Bot", f"Updated answer: {adjusted_answer}\nPlease provide feedback: good, too vague, not helpful."))
|
| 151 |
logger.info(f"Updated answer: {adjusted_answer}")
|
| 152 |
-
|
| 153 |
return state['chat_history'], state
|
| 154 |
|
| 155 |
# Initial state
|
|
@@ -171,9 +189,10 @@ with gr.Blocks() as demo:
|
|
| 171 |
chat = gr.Chatbot()
|
| 172 |
user_input = gr.Textbox(label="Your query or feedback")
|
| 173 |
submit_btn = gr.Button("Submit")
|
| 174 |
-
|
|
|
|
| 175 |
|
| 176 |
file_upload.upload(process_file, inputs=[file_upload, state], outputs=[chat, state])
|
| 177 |
submit_btn.click(handle_input, inputs=[user_input, state], outputs=[chat, state]).then(lambda: "", None, user_input)
|
| 178 |
|
| 179 |
-
demo.launch(share=True)
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
from sentence_transformers import SentenceTransformer, util
|
| 5 |
import PyPDF2
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# Set up logging with a dedicated file handler
|
| 9 |
logger = logging.getLogger('SupportBot')
|
|
|
|
| 11 |
# Remove any existing handlers to avoid conflicts
|
| 12 |
if logger.handlers:
|
| 13 |
logger.handlers.clear()
|
| 14 |
+
|
| 15 |
+
# Define log file path in a writable directory (/tmp)
|
| 16 |
+
log_file_path = '/tmp/support_bot_log.txt'
|
| 17 |
+
|
| 18 |
# Create a file handler with append mode
|
| 19 |
+
file_handler = logging.FileHandler(log_file_path, mode='a')
|
| 20 |
+
file_handler.setLevel(logging.INFO)
|
| 21 |
formatter = logging.Formatter('%(asctime)s - %(message)s')
|
| 22 |
+
file_handler.setFormatter(formatter)
|
| 23 |
+
logger.addHandler(file_handler)
|
| 24 |
+
|
| 25 |
+
# Add a stream handler to output logs to the console as well
|
| 26 |
+
stream_handler = logging.StreamHandler()
|
| 27 |
+
stream_handler.setLevel(logging.INFO)
|
| 28 |
+
stream_handler.setFormatter(formatter)
|
| 29 |
+
logger.addHandler(stream_handler)
|
| 30 |
|
| 31 |
# Load models
|
| 32 |
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 33 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 34 |
|
| 35 |
+
# Helper function to extract text from a PDF
|
| 36 |
def extract_text_from_pdf(file_path):
|
| 37 |
text = ""
|
| 38 |
with open(file_path, "rb") as file:
|
| 39 |
pdf_reader = PyPDF2.PdfReader(file)
|
| 40 |
for page in pdf_reader.pages:
|
| 41 |
+
extracted_text = page.extract_text()
|
| 42 |
+
if extracted_text:
|
| 43 |
+
text += extracted_text + "\n"
|
| 44 |
return text
|
| 45 |
|
| 46 |
# Find the most relevant section in the document
|
|
|
|
| 57 |
SIMILARITY_THRESHOLD = 0.4
|
| 58 |
if similarity_score >= SIMILARITY_THRESHOLD:
|
| 59 |
logger.info(f"Found relevant section using embeddings (score: {similarity_score})")
|
| 60 |
+
file_handler.flush() # Ensure log is written immediately
|
| 61 |
return best_section
|
| 62 |
|
| 63 |
logger.info(f"Low similarity ({similarity_score}). Falling back to keyword search.")
|
|
|
|
| 67 |
common_words = query_words.intersection(section_words)
|
| 68 |
if len(common_words) >= 2:
|
| 69 |
logger.info(f"Keyword match found with common words: {common_words}")
|
| 70 |
+
file_handler.flush()
|
| 71 |
return section
|
| 72 |
|
| 73 |
logger.info("No good match found. Returning default response.")
|
| 74 |
+
file_handler.flush()
|
| 75 |
return "I don’t have enough information to answer that."
|
| 76 |
|
| 77 |
# Process the uploaded file
|
|
|
|
| 79 |
logger.info("Received file upload request")
|
| 80 |
if file is None:
|
| 81 |
logger.info("No file uploaded")
|
| 82 |
+
file_handler.flush()
|
| 83 |
return [("Bot", "Please upload a file.")], state
|
| 84 |
|
| 85 |
+
# Save the uploaded file to a temporary location
|
| 86 |
file_path = file.name
|
| 87 |
+
temp_file_path = os.path.join("/tmp", os.path.basename(file_path))
|
| 88 |
+
with open(temp_file_path, "wb") as f:
|
| 89 |
+
f.write(file.read())
|
| 90 |
+
|
| 91 |
+
if temp_file_path.lower().endswith(".pdf"):
|
| 92 |
+
logger.info(f"Processing PDF file: {temp_file_path}")
|
| 93 |
+
text = extract_text_from_pdf(temp_file_path)
|
| 94 |
+
elif temp_file_path.lower().endswith(".txt"):
|
| 95 |
+
logger.info(f"Processing TXT file: {temp_file_path}")
|
| 96 |
+
with open(temp_file_path, 'r', encoding='utf-8') as f:
|
| 97 |
text = f.read()
|
| 98 |
else:
|
| 99 |
+
logger.error(f"Unsupported file format: {temp_file_path}")
|
| 100 |
+
file_handler.flush()
|
| 101 |
return [("Bot", "Unsupported file format. Please upload a PDF or TXT file.")], state
|
| 102 |
|
| 103 |
sections = text.split('\n\n')
|
|
|
|
| 109 |
state['feedback_count'] = 0
|
| 110 |
state['mode'] = 'waiting_for_query'
|
| 111 |
state['chat_history'] = [("Bot", "File processed. You can now ask questions.")]
|
| 112 |
+
logger.info(f"File processed successfully: {temp_file_path}")
|
| 113 |
+
file_handler.flush()
|
| 114 |
return state['chat_history'], state
|
| 115 |
|
| 116 |
# Handle user input (queries and feedback)
|
|
|
|
| 118 |
if state['mode'] == 'waiting_for_upload':
|
| 119 |
logger.info("User input received before file upload")
|
| 120 |
state['chat_history'].append(("Bot", "Please upload a file first."))
|
| 121 |
+
file_handler.flush()
|
| 122 |
elif state['mode'] == 'waiting_for_query':
|
| 123 |
query = user_input
|
| 124 |
logger.info(f"User query: {query}")
|
|
|
|
| 135 |
state['chat_history'].append(("User", query))
|
| 136 |
state['chat_history'].append(("Bot", f"Answer: {answer}\nPlease provide feedback: good, too vague, not helpful."))
|
| 137 |
logger.info(f"Generated answer: {answer}")
|
| 138 |
+
file_handler.flush()
|
| 139 |
elif state['mode'] == 'waiting_for_feedback':
|
| 140 |
feedback = user_input.lower()
|
| 141 |
logger.info(f"User feedback: {feedback}")
|
|
|
|
| 148 |
else:
|
| 149 |
state['chat_history'].append(("Bot", "Maximum feedback iterations reached. You can ask another question."))
|
| 150 |
logger.info("Max feedback iterations (2) reached. Ready for next query.")
|
| 151 |
+
file_handler.flush()
|
| 152 |
else:
|
| 153 |
query = state['current_query']
|
| 154 |
context = find_relevant_section(query, state['sections'], state['section_embeddings'])
|
|
|
|
| 161 |
else:
|
| 162 |
state['chat_history'].append(("Bot", "Please provide valid feedback: good, too vague, not helpful."))
|
| 163 |
logger.info(f"Invalid feedback received: {feedback}")
|
| 164 |
+
file_handler.flush()
|
| 165 |
return state['chat_history'], state
|
| 166 |
state['last_answer'] = adjusted_answer
|
| 167 |
state['feedback_count'] += 1
|
| 168 |
state['chat_history'].append(("Bot", f"Updated answer: {adjusted_answer}\nPlease provide feedback: good, too vague, not helpful."))
|
| 169 |
logger.info(f"Updated answer: {adjusted_answer}")
|
| 170 |
+
file_handler.flush()
|
| 171 |
return state['chat_history'], state
|
| 172 |
|
| 173 |
# Initial state
|
|
|
|
| 189 |
chat = gr.Chatbot()
|
| 190 |
user_input = gr.Textbox(label="Your query or feedback")
|
| 191 |
submit_btn = gr.Button("Submit")
|
| 192 |
+
# Point the log file download to the writable log file path
|
| 193 |
+
log_file = gr.File(label="Download Log File", value=log_file_path)
|
| 194 |
|
| 195 |
file_upload.upload(process_file, inputs=[file_upload, state], outputs=[chat, state])
|
| 196 |
submit_btn.click(handle_input, inputs=[user_input, state], outputs=[chat, state]).then(lambda: "", None, user_input)
|
| 197 |
|
| 198 |
+
demo.launch(share=True)
|