Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,14 +66,19 @@ def process_text(input_text):
|
|
| 66 |
|
| 67 |
# === Core RAG Handler ===
|
| 68 |
def handle_input(file, clipboard, query):
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return f"❌ Error: {str(e)}"
|
| 79 |
|
|
@@ -87,7 +92,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 87 |
|
| 88 |
query_input = gr.Textbox(label="Ask a Question (optional)", placeholder="e.g., What risks am I agreeing to?")
|
| 89 |
submit_btn = gr.Button("🔍 Analyze")
|
| 90 |
-
output = gr.Textbox(label="Output", lines=
|
| 91 |
|
| 92 |
submit_btn.click(fn=handle_input, inputs=[file_input, clipboard_input, query_input], outputs=output)
|
| 93 |
|
|
|
|
| 66 |
|
| 67 |
# === Core RAG Handler ===
|
| 68 |
def handle_input(file, clipboard, query):
|
| 69 |
+
try:
|
| 70 |
+
raw_text = extract_text(file, clipboard)
|
| 71 |
+
if not raw_text:
|
| 72 |
+
return "Please provide either a PDF or clipboard text."
|
| 73 |
+
|
| 74 |
+
qa = process_text(raw_text)
|
| 75 |
+
response = qa.invoke({"query": query if query else "Summarize the key points and risks in this policy."})
|
| 76 |
+
|
| 77 |
+
result = response["result"]
|
| 78 |
+
sources = response["source_documents"]
|
| 79 |
+
source_preview = "\n\n📄 Sources:\n" + "\n---\n".join([doc.page_content[:300] + "..." for doc in sources[:3]])
|
| 80 |
+
|
| 81 |
+
return result + source_preview
|
| 82 |
except Exception as e:
|
| 83 |
return f"❌ Error: {str(e)}"
|
| 84 |
|
|
|
|
| 92 |
|
| 93 |
query_input = gr.Textbox(label="Ask a Question (optional)", placeholder="e.g., What risks am I agreeing to?")
|
| 94 |
submit_btn = gr.Button("🔍 Analyze")
|
| 95 |
+
output = gr.Textbox(label="Output", lines=20)
|
| 96 |
|
| 97 |
submit_btn.click(fn=handle_input, inputs=[file_input, clipboard_input, query_input], outputs=output)
|
| 98 |
|