Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,18 @@ from transformers import pipeline
|
|
| 4 |
from sentence_transformers import SentenceTransformer, util
|
| 5 |
import PyPDF2
|
| 6 |
|
| 7 |
-
# Set up logging with
|
| 8 |
-
logging.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Load models
|
| 17 |
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
|
@@ -30,7 +34,7 @@ def extract_text_from_pdf(file_path):
|
|
| 30 |
def find_relevant_section(query, sections, section_embeddings):
|
| 31 |
stopwords = {"and", "the", "is", "for", "to", "a", "an", "of", "in", "on", "at", "with", "by", "it", "as", "so", "what"}
|
| 32 |
|
| 33 |
-
|
| 34 |
query_embedding = embedder.encode(query, convert_to_tensor=True)
|
| 35 |
similarities = util.cos_sim(query_embedding, section_embeddings)[0]
|
| 36 |
best_idx = similarities.argmax().item()
|
|
@@ -39,39 +43,43 @@ def find_relevant_section(query, sections, section_embeddings):
|
|
| 39 |
|
| 40 |
SIMILARITY_THRESHOLD = 0.4
|
| 41 |
if similarity_score >= SIMILARITY_THRESHOLD:
|
| 42 |
-
logger.info(f"Found relevant section using embeddings
|
|
|
|
| 43 |
return best_section
|
| 44 |
|
| 45 |
logger.info(f"Low similarity ({similarity_score}). Falling back to keyword search.")
|
| 46 |
-
|
| 47 |
-
# Keyword-based fallback search with stopword filtering
|
| 48 |
query_words = {word for word in query.lower().split() if word not in stopwords}
|
| 49 |
for section in sections:
|
| 50 |
section_words = {word for word in section.lower().split() if word not in stopwords}
|
| 51 |
common_words = query_words.intersection(section_words)
|
| 52 |
if len(common_words) >= 2:
|
| 53 |
-
logger.info(f"Keyword match found
|
|
|
|
| 54 |
return section
|
| 55 |
|
| 56 |
-
logger.info(
|
|
|
|
| 57 |
return "I don’t have enough information to answer that."
|
| 58 |
|
| 59 |
-
# Process the uploaded file
|
| 60 |
def process_file(file, state):
|
|
|
|
| 61 |
if file is None:
|
| 62 |
-
logger.info("No file uploaded
|
|
|
|
| 63 |
return [("Bot", "Please upload a file.")], state
|
| 64 |
|
| 65 |
file_path = file.name
|
| 66 |
if file_path.lower().endswith(".pdf"):
|
| 67 |
-
logger.info(f"
|
| 68 |
text = extract_text_from_pdf(file_path)
|
| 69 |
elif file_path.lower().endswith(".txt"):
|
| 70 |
-
logger.info(f"
|
| 71 |
with open(file_path, 'r', encoding='utf-8') as f:
|
| 72 |
text = f.read()
|
| 73 |
else:
|
| 74 |
logger.error(f"Unsupported file format: {file_path}")
|
|
|
|
| 75 |
return [("Bot", "Unsupported file format. Please upload a PDF or TXT file.")], state
|
| 76 |
|
| 77 |
sections = text.split('\n\n')
|
|
@@ -83,16 +91,19 @@ def process_file(file, state):
|
|
| 83 |
state['feedback_count'] = 0
|
| 84 |
state['mode'] = 'waiting_for_query'
|
| 85 |
state['chat_history'] = [("Bot", "File processed. You can now ask questions.")]
|
| 86 |
-
logger.info(f"
|
|
|
|
| 87 |
return state['chat_history'], state
|
| 88 |
|
| 89 |
# Handle user input (queries and feedback)
|
| 90 |
def handle_input(user_input, state):
|
| 91 |
if state['mode'] == 'waiting_for_upload':
|
|
|
|
| 92 |
state['chat_history'].append(("Bot", "Please upload a file first."))
|
| 93 |
-
|
| 94 |
elif state['mode'] == 'waiting_for_query':
|
| 95 |
query = user_input
|
|
|
|
| 96 |
state['current_query'] = query
|
| 97 |
state['feedback_count'] = 0
|
| 98 |
context = find_relevant_section(query, state['sections'], state['section_embeddings'])
|
|
@@ -105,34 +116,40 @@ def handle_input(user_input, state):
|
|
| 105 |
state['mode'] = 'waiting_for_feedback'
|
| 106 |
state['chat_history'].append(("User", query))
|
| 107 |
state['chat_history'].append(("Bot", f"Answer: {answer}\nPlease provide feedback: good, too vague, not helpful."))
|
| 108 |
-
logger.info(f"
|
|
|
|
| 109 |
elif state['mode'] == 'waiting_for_feedback':
|
| 110 |
feedback = user_input.lower()
|
|
|
|
| 111 |
state['chat_history'].append(("User", feedback))
|
| 112 |
-
logger.info(f"Feedback: {feedback}")
|
| 113 |
if feedback == "good" or state['feedback_count'] >= 2:
|
| 114 |
state['mode'] = 'waiting_for_query'
|
| 115 |
if feedback == "good":
|
| 116 |
state['chat_history'].append(("Bot", "Thank you for your feedback. You can ask another question."))
|
| 117 |
-
logger.info("Feedback
|
| 118 |
else:
|
| 119 |
state['chat_history'].append(("Bot", "Maximum feedback iterations reached. You can ask another question."))
|
| 120 |
-
logger.info("Max feedback iterations reached.
|
|
|
|
| 121 |
else:
|
| 122 |
query = state['current_query']
|
| 123 |
context = find_relevant_section(query, state['sections'], state['section_embeddings'])
|
| 124 |
if feedback == "too vague":
|
| 125 |
adjusted_answer = f"{state['last_answer']}\n\n(More details:\n{context[:500]}...)"
|
|
|
|
| 126 |
elif feedback == "not helpful":
|
| 127 |
adjusted_answer = qa_model(question=query + " Please provide more detailed information with examples.", context=context)['answer']
|
|
|
|
| 128 |
else:
|
| 129 |
state['chat_history'].append(("Bot", "Please provide valid feedback: good, too vague, not helpful."))
|
| 130 |
logger.info(f"Invalid feedback received: {feedback}")
|
|
|
|
| 131 |
return state['chat_history'], state
|
| 132 |
state['last_answer'] = adjusted_answer
|
| 133 |
state['feedback_count'] += 1
|
| 134 |
state['chat_history'].append(("Bot", f"Updated answer: {adjusted_answer}\nPlease provide feedback: good, too vague, not helpful."))
|
| 135 |
-
logger.info(f"
|
|
|
|
| 136 |
return state['chat_history'], state
|
| 137 |
|
| 138 |
# Initial state
|
|
@@ -154,12 +171,9 @@ with gr.Blocks() as demo:
|
|
| 154 |
chat = gr.Chatbot()
|
| 155 |
user_input = gr.Textbox(label="Your query or feedback")
|
| 156 |
submit_btn = gr.Button("Submit")
|
| 157 |
-
log_file = gr.File(label="Download Log File", value="support_bot_log.txt")
|
| 158 |
|
| 159 |
-
# Process file upload
|
| 160 |
file_upload.upload(process_file, inputs=[file_upload, state], outputs=[chat, state])
|
| 161 |
-
|
| 162 |
-
# Handle user input and clear the textbox
|
| 163 |
submit_btn.click(handle_input, inputs=[user_input, state], outputs=[chat, state]).then(lambda: "", None, user_input)
|
| 164 |
|
| 165 |
demo.launch(share=True)
|
|
|
|
| 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')
|
| 9 |
+
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 |
+
handler = logging.FileHandler('support_bot_log.txt', mode='a')
|
| 15 |
+
handler.setLevel(logging.INFO)
|
| 16 |
+
formatter = logging.Formatter('%(asctime)s - %(message)s')
|
| 17 |
+
handler.setFormatter(formatter)
|
| 18 |
+
logger.addHandler(handler)
|
| 19 |
|
| 20 |
# Load models
|
| 21 |
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
|
|
|
| 34 |
def find_relevant_section(query, sections, section_embeddings):
|
| 35 |
stopwords = {"and", "the", "is", "for", "to", "a", "an", "of", "in", "on", "at", "with", "by", "it", "as", "so", "what"}
|
| 36 |
|
| 37 |
+
logger.info(f"Searching for relevant section for query: {query}")
|
| 38 |
query_embedding = embedder.encode(query, convert_to_tensor=True)
|
| 39 |
similarities = util.cos_sim(query_embedding, section_embeddings)[0]
|
| 40 |
best_idx = similarities.argmax().item()
|
|
|
|
| 43 |
|
| 44 |
SIMILARITY_THRESHOLD = 0.4
|
| 45 |
if similarity_score >= SIMILARITY_THRESHOLD:
|
| 46 |
+
logger.info(f"Found relevant section using embeddings (score: {similarity_score})")
|
| 47 |
+
handler.flush() # Ensure log is written immediately
|
| 48 |
return best_section
|
| 49 |
|
| 50 |
logger.info(f"Low similarity ({similarity_score}). Falling back to keyword search.")
|
|
|
|
|
|
|
| 51 |
query_words = {word for word in query.lower().split() if word not in stopwords}
|
| 52 |
for section in sections:
|
| 53 |
section_words = {word for word in section.lower().split() if word not in stopwords}
|
| 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 |
+
handler.flush()
|
| 58 |
return section
|
| 59 |
|
| 60 |
+
logger.info("No good match found. Returning default response.")
|
| 61 |
+
handler.flush()
|
| 62 |
return "I don’t have enough information to answer that."
|
| 63 |
|
| 64 |
+
# Process the uploaded file
|
| 65 |
def process_file(file, state):
|
| 66 |
+
logger.info("Received file upload request")
|
| 67 |
if file is None:
|
| 68 |
+
logger.info("No file uploaded")
|
| 69 |
+
handler.flush()
|
| 70 |
return [("Bot", "Please upload a file.")], state
|
| 71 |
|
| 72 |
file_path = file.name
|
| 73 |
if file_path.lower().endswith(".pdf"):
|
| 74 |
+
logger.info(f"Processing PDF file: {file_path}")
|
| 75 |
text = extract_text_from_pdf(file_path)
|
| 76 |
elif file_path.lower().endswith(".txt"):
|
| 77 |
+
logger.info(f"Processing TXT file: {file_path}")
|
| 78 |
with open(file_path, 'r', encoding='utf-8') as f:
|
| 79 |
text = f.read()
|
| 80 |
else:
|
| 81 |
logger.error(f"Unsupported file format: {file_path}")
|
| 82 |
+
handler.flush()
|
| 83 |
return [("Bot", "Unsupported file format. Please upload a PDF or TXT file.")], state
|
| 84 |
|
| 85 |
sections = text.split('\n\n')
|
|
|
|
| 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: {file_path}")
|
| 95 |
+
handler.flush()
|
| 96 |
return state['chat_history'], state
|
| 97 |
|
| 98 |
# Handle user input (queries and feedback)
|
| 99 |
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 |
+
handler.flush()
|
| 104 |
elif state['mode'] == 'waiting_for_query':
|
| 105 |
query = user_input
|
| 106 |
+
logger.info(f"User query: {query}")
|
| 107 |
state['current_query'] = query
|
| 108 |
state['feedback_count'] = 0
|
| 109 |
context = find_relevant_section(query, state['sections'], state['section_embeddings'])
|
|
|
|
| 116 |
state['mode'] = 'waiting_for_feedback'
|
| 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 |
+
handler.flush()
|
| 121 |
elif state['mode'] == 'waiting_for_feedback':
|
| 122 |
feedback = user_input.lower()
|
| 123 |
+
logger.info(f"User feedback: {feedback}")
|
| 124 |
state['chat_history'].append(("User", feedback))
|
|
|
|
| 125 |
if feedback == "good" or state['feedback_count'] >= 2:
|
| 126 |
state['mode'] = 'waiting_for_query'
|
| 127 |
if feedback == "good":
|
| 128 |
state['chat_history'].append(("Bot", "Thank you for your feedback. You can ask another question."))
|
| 129 |
+
logger.info("Feedback 'good' received. Ready for next query.")
|
| 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 |
+
handler.flush()
|
| 134 |
else:
|
| 135 |
query = state['current_query']
|
| 136 |
context = find_relevant_section(query, state['sections'], state['section_embeddings'])
|
| 137 |
if feedback == "too vague":
|
| 138 |
adjusted_answer = f"{state['last_answer']}\n\n(More details:\n{context[:500]}...)"
|
| 139 |
+
logger.info("Feedback 'too vague'. Providing context.")
|
| 140 |
elif feedback == "not helpful":
|
| 141 |
adjusted_answer = qa_model(question=query + " Please provide more detailed information with examples.", context=context)['answer']
|
| 142 |
+
logger.info("Feedback 'not helpful'. Re-searching with modified query.")
|
| 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 |
+
handler.flush()
|
| 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 |
+
handler.flush()
|
| 153 |
return state['chat_history'], state
|
| 154 |
|
| 155 |
# Initial state
|
|
|
|
| 171 |
chat = gr.Chatbot()
|
| 172 |
user_input = gr.Textbox(label="Your query or feedback")
|
| 173 |
submit_btn = gr.Button("Submit")
|
| 174 |
+
log_file = gr.File(label="Download Log File", value="support_bot_log.txt")
|
| 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)
|