Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -93,4 +93,51 @@ def search_documents(query):
|
|
| 93 |
context = "\n".join([match['metadata']['text'] for match in results['matches']])
|
| 94 |
|
| 95 |
prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
|
| 96 |
-
inputs = tokenizer(prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
context = "\n".join([match['metadata']['text'] for match in results['matches']])
|
| 94 |
|
| 95 |
prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
|
| 96 |
+
inputs = tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True)
|
| 97 |
+
|
| 98 |
+
outputs = model.generate(
|
| 99 |
+
**inputs,
|
| 100 |
+
max_length=512,
|
| 101 |
+
num_beams=4,
|
| 102 |
+
temperature=0.7,
|
| 103 |
+
top_p=0.9
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 107 |
+
|
| 108 |
+
# Format sources
|
| 109 |
+
sources = [f"Source: {match['metadata']['file_name']}" for match in results['matches']]
|
| 110 |
+
|
| 111 |
+
return answer, "\n".join(sources)
|
| 112 |
+
|
| 113 |
+
# Create Gradio interface
|
| 114 |
+
with gr.Blocks() as demo:
|
| 115 |
+
gr.Markdown("# PDF Document Search and Q&A")
|
| 116 |
+
|
| 117 |
+
with gr.Tab("Upload Documents"):
|
| 118 |
+
file_output = gr.File(
|
| 119 |
+
file_count="multiple",
|
| 120 |
+
label="Upload PDF Files"
|
| 121 |
+
)
|
| 122 |
+
upload_button = gr.Button("Process PDFs")
|
| 123 |
+
upload_output = gr.Textbox(label="Processing Results")
|
| 124 |
+
|
| 125 |
+
with gr.Tab("Search and Ask"):
|
| 126 |
+
query_input = gr.Textbox(label="Enter your question")
|
| 127 |
+
search_button = gr.Button("Search")
|
| 128 |
+
answer_output = gr.Textbox(label="Answer")
|
| 129 |
+
sources_output = gr.Textbox(label="Sources")
|
| 130 |
+
|
| 131 |
+
upload_button.click(
|
| 132 |
+
process_multiple_pdfs,
|
| 133 |
+
inputs=[file_output],
|
| 134 |
+
outputs=[upload_output]
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
search_button.click(
|
| 138 |
+
search_documents,
|
| 139 |
+
inputs=[query_input],
|
| 140 |
+
outputs=[answer_output, sources_output]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
demo.launch()
|