Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,164 @@
|
|
| 1 |
-
|
| 2 |
import numpy as np
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import faiss
|
| 5 |
import re
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
|
| 9 |
-
"""
|
| 10 |
-
Preprocess the text into structured question-answer pairs
|
| 11 |
-
"""
|
| 12 |
-
# Split text into sections by questions
|
| 13 |
-
sections = []
|
| 14 |
-
current_section = []
|
| 15 |
-
|
| 16 |
-
for line in text.split('\n'):
|
| 17 |
-
line = line.strip()
|
| 18 |
-
if line.startswith('Question'):
|
| 19 |
-
if current_section:
|
| 20 |
-
sections.append(' '.join(current_section))
|
| 21 |
-
current_section = [line]
|
| 22 |
-
elif line:
|
| 23 |
-
current_section.append(line)
|
| 24 |
-
|
| 25 |
-
if current_section:
|
| 26 |
-
sections.append(' '.join(current_section))
|
| 27 |
-
|
| 28 |
-
# Create a structured format
|
| 29 |
-
structured_sections = []
|
| 30 |
-
for section in sections:
|
| 31 |
-
# Remove page numbers and other irrelevant text
|
| 32 |
-
section = re.sub(r'\d+\s*$', '', section)
|
| 33 |
-
section = re.sub(r'TRAPS:|BEST ANSWER:|PASSABLE ANSWER:', ' ', section)
|
| 34 |
-
structured_sections.append(section.strip())
|
| 35 |
-
|
| 36 |
-
return structured_sections
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Search for the most similar chunks
|
| 47 |
-
k = 1 # Get only the best match
|
| 48 |
-
similarities, indices = index.search(question_embedding, k)
|
| 49 |
-
|
| 50 |
-
best_idx = indices[0][0]
|
| 51 |
-
similarity_score = similarities[0][0] # Cosine similarity score
|
| 52 |
-
|
| 53 |
-
if similarity_score >= similarity_threshold:
|
| 54 |
-
matched_text = text_chunks[best_idx]
|
| 55 |
-
# Extract just the question number for reference
|
| 56 |
-
question_num = re.search(r'Question \d+:', matched_text)
|
| 57 |
-
question_num = question_num.group(0) if question_num else "Matching section"
|
| 58 |
-
|
| 59 |
-
return {
|
| 60 |
-
'question': question_num,
|
| 61 |
-
'full_text': matched_text,
|
| 62 |
-
'confidence': float(similarity_score),
|
| 63 |
-
'found_answer': True
|
| 64 |
-
}
|
| 65 |
-
else:
|
| 66 |
-
return {
|
| 67 |
-
'question': None,
|
| 68 |
-
'full_text': "I couldn't find a sufficiently relevant answer to your question in the provided document.",
|
| 69 |
-
'confidence': float(similarity_score),
|
| 70 |
-
'found_answer': False
|
| 71 |
-
}
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if text:
|
| 83 |
-
pdf_text += text + "\n"
|
| 84 |
-
|
| 85 |
-
# Process text and create embeddings
|
| 86 |
-
text_chunks = preprocess_text(pdf_text)
|
| 87 |
-
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 88 |
-
embeddings = model.encode(text_chunks)
|
| 89 |
-
|
| 90 |
-
# Create index
|
| 91 |
-
dimension = embeddings.shape[1]
|
| 92 |
-
faiss.normalize_L2(embeddings)
|
| 93 |
-
index = faiss.IndexFlatIP(dimension)
|
| 94 |
-
index.add(embeddings)
|
| 95 |
-
|
| 96 |
-
return {
|
| 97 |
-
'model': model,
|
| 98 |
-
'index': index,
|
| 99 |
-
'text_chunks': text_chunks,
|
| 100 |
-
'status': f"System initialized with {len(text_chunks)} text chunks from your PDF!"
|
| 101 |
-
}
|
| 102 |
-
except Exception as e:
|
| 103 |
-
return {
|
| 104 |
-
'model': None,
|
| 105 |
-
'index': None,
|
| 106 |
-
'text_chunks': None,
|
| 107 |
-
'status': f"Error: {str(e)}"
|
| 108 |
-
}
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
qa_system = result
|
| 118 |
-
return result['status']
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
# Create the Gradio interface
|
| 138 |
-
with gr.Blocks(title="Interview Q&A Assistant") as demo:
|
| 139 |
-
|
| 140 |
-
gr.
|
|
|
|
|
|
|
|
|
|
| 141 |
|
|
|
|
| 142 |
with gr.Row():
|
| 143 |
-
with gr.Column():
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
with gr.Row():
|
| 149 |
-
with gr.Column():
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
with gr.Row():
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
# Set up events
|
| 157 |
upload_button.click(upload_file, inputs=pdf_upload, outputs=status_text)
|
| 158 |
submit_button.click(answer_question, inputs=question_input, outputs=answer_output)
|
| 159 |
|
| 160 |
# Launch the app
|
| 161 |
if __name__ == "__main__":
|
| 162 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
import faiss
|
| 4 |
import re
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# [Previous functions remain exactly the same - preprocess_text, query_qa_system, initialize_qa_system, etc.]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Custom CSS for professional styling
|
| 10 |
+
custom_css = """
|
| 11 |
+
.gradio-container {
|
| 12 |
+
max-width: 1200px !important;
|
| 13 |
+
margin: auto !important;
|
| 14 |
+
padding: 20px !important;
|
| 15 |
+
background-color: #f8f9fa !important;
|
| 16 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
.main-header {
|
| 19 |
+
text-align: center;
|
| 20 |
+
margin-bottom: 2rem;
|
| 21 |
+
padding: 2rem;
|
| 22 |
+
background: linear-gradient(135deg, #1a365d 0%, #2c5282 100%);
|
| 23 |
+
color: white;
|
| 24 |
+
border-radius: 10px;
|
| 25 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 26 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
.main-header h1 {
|
| 29 |
+
font-size: 2.5rem;
|
| 30 |
+
margin-bottom: 1rem;
|
| 31 |
+
font-weight: 600;
|
| 32 |
+
}
|
| 33 |
|
| 34 |
+
.main-header p {
|
| 35 |
+
font-size: 1.1rem;
|
| 36 |
+
opacity: 0.9;
|
| 37 |
+
}
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
.upload-section {
|
| 40 |
+
background: white;
|
| 41 |
+
padding: 2rem;
|
| 42 |
+
border-radius: 10px;
|
| 43 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
| 44 |
+
margin-bottom: 2rem;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
.qa-section {
|
| 48 |
+
background: white;
|
| 49 |
+
padding: 2rem;
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
| 52 |
+
}
|
| 53 |
|
| 54 |
+
.status-box {
|
| 55 |
+
margin-top: 1rem;
|
| 56 |
+
padding: 1rem;
|
| 57 |
+
border-radius: 8px;
|
| 58 |
+
background: #f0f9ff;
|
| 59 |
+
border: 1px solid #bae6fd;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
.custom-button {
|
| 63 |
+
background: #2563eb !important;
|
| 64 |
+
color: white !important;
|
| 65 |
+
border-radius: 8px !important;
|
| 66 |
+
padding: 0.75rem 1.5rem !important;
|
| 67 |
+
font-weight: 500 !important;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.custom-button:hover {
|
| 71 |
+
background: #1d4ed8 !important;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.answer-box {
|
| 75 |
+
background: #f8fafc !important;
|
| 76 |
+
border: 1px solid #e2e8f0 !important;
|
| 77 |
+
border-radius: 8px !important;
|
| 78 |
+
font-family: 'Source Code Pro', monospace !important;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.section-title {
|
| 82 |
+
color: #1e293b;
|
| 83 |
+
font-size: 1.25rem;
|
| 84 |
+
font-weight: 600;
|
| 85 |
+
margin-bottom: 1rem;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/* Responsive design */
|
| 89 |
+
@media (max-width: 768px) {
|
| 90 |
+
.gradio-container {
|
| 91 |
+
padding: 10px !important;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
.main-header {
|
| 95 |
+
padding: 1.5rem;
|
| 96 |
+
}
|
| 97 |
|
| 98 |
+
.main-header h1 {
|
| 99 |
+
font-size: 2rem;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
"""
|
| 103 |
|
| 104 |
+
# Create the enhanced Gradio interface
|
| 105 |
+
with gr.Blocks(title="Interview Q&A Assistant", css=custom_css) as demo:
|
| 106 |
+
# Header Section
|
| 107 |
+
with gr.Row(elem_classes=["main-header"]):
|
| 108 |
+
with gr.Column():
|
| 109 |
+
gr.Markdown("# Interview Q&A Assistant")
|
| 110 |
+
gr.Markdown("Your AI-powered interview preparation companion. Upload your interview questions PDF and get instant, relevant answers to your queries.")
|
| 111 |
|
| 112 |
+
# Upload Section
|
| 113 |
with gr.Row():
|
| 114 |
+
with gr.Column(elem_classes=["upload-section"]):
|
| 115 |
+
gr.Markdown("### 📁 Document Upload", elem_classes=["section-title"])
|
| 116 |
+
with gr.Row():
|
| 117 |
+
pdf_upload = gr.File(
|
| 118 |
+
label="Upload your interview questions PDF",
|
| 119 |
+
file_types=[".pdf"],
|
| 120 |
+
elem_classes=["file-upload"]
|
| 121 |
+
)
|
| 122 |
+
with gr.Row():
|
| 123 |
+
upload_button = gr.Button("Initialize Q&A System", elem_classes=["custom-button"])
|
| 124 |
+
with gr.Row():
|
| 125 |
+
status_text = gr.Textbox(
|
| 126 |
+
label="System Status",
|
| 127 |
+
value="Upload a PDF to begin",
|
| 128 |
+
elem_classes=["status-box"]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Q&A Section
|
| 132 |
with gr.Row():
|
| 133 |
+
with gr.Column(elem_classes=["qa-section"]):
|
| 134 |
+
gr.Markdown("### 💡 Ask Questions", elem_classes=["section-title"])
|
| 135 |
+
with gr.Row():
|
| 136 |
+
question_input = gr.Textbox(
|
| 137 |
+
label="What would you like to know about the interview?",
|
| 138 |
+
placeholder="e.g., What are the common behavioral questions?",
|
| 139 |
+
lines=2
|
| 140 |
+
)
|
| 141 |
+
with gr.Row():
|
| 142 |
+
submit_button = gr.Button("Get Answer", elem_classes=["custom-button"])
|
| 143 |
+
with gr.Row():
|
| 144 |
+
answer_output = gr.Textbox(
|
| 145 |
+
label="Answer",
|
| 146 |
+
lines=10,
|
| 147 |
+
elem_classes=["answer-box"]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Information Section
|
| 151 |
with gr.Row():
|
| 152 |
+
gr.Markdown("""
|
| 153 |
+
<div style="text-align: center; padding: 2rem; color: #64748b; font-size: 0.9rem;">
|
| 154 |
+
Made with ❤️ for interview preparation success
|
| 155 |
+
</div>
|
| 156 |
+
""")
|
| 157 |
|
| 158 |
+
# Set up events (keeping the same functionality)
|
| 159 |
upload_button.click(upload_file, inputs=pdf_upload, outputs=status_text)
|
| 160 |
submit_button.click(answer_question, inputs=question_input, outputs=answer_output)
|
| 161 |
|
| 162 |
# Launch the app
|
| 163 |
if __name__ == "__main__":
|
| 164 |
+
demo.launch(share=True)
|