File size: 2,382 Bytes
f455c38
 
 
1dcc426
5933ec9
1dcc426
 
f455c38
1dcc426
 
 
5933ec9
 
 
 
 
 
 
 
 
 
 
 
f455c38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d05025
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr
from pypdf import PdfReader
from rag_pipeline import RAGPipeline
import warnings
import asyncio
import logging
import os

warnings.filterwarnings("ignore", category=FutureWarning)
logging.getLogger("transformers").setLevel(logging.ERROR)
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# Fix Python 3.13 asyncio cleanup bug
import sys
if sys.version_info >= (3, 13):
    import asyncio.base_events
    _original_del = asyncio.base_events.BaseEventLoop.__del__
    def _safe_del(self):
        try:
            _original_del(self)
        except Exception:
            pass
    asyncio.base_events.BaseEventLoop.__del__ = _safe_del
rag = RAGPipeline()

def process_file(file):
    if file is None:
        return "No file uploaded."
    
    # Support PDF and TXT
    if file.name.endswith(".pdf"):
        reader = PdfReader(file.name)
        text = "\n".join(page.extract_text() for page in reader.pages)
    else:
        with open(file.name, "r", encoding="utf-8") as f:
            text = f.read()

    count = rag.ingest(text)
    return f"βœ… Indexed {count} chunks from your document. You can now ask questions!"

def chat(query, history):
    if not query.strip():
        return history
    answer = rag.answer(query)
    history.append((query, answer))
    return history

with gr.Blocks(title="πŸ“š RAG App β€” No API Key") as demo:
    gr.Markdown("# πŸ“š RAG Q&A β€” Powered by Open-Source Models\nUpload a document, then ask questions about it.")

    with gr.Row():
        with gr.Column(scale=1):
            file_input = gr.File(label="Upload PDF or TXT", file_types=[".pdf", ".txt"])
            upload_btn = gr.Button("πŸ“₯ Process Document", variant="primary")
            status = gr.Textbox(label="Status", interactive=False)
        
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(label="Chat with your document")
            query_box = gr.Textbox(placeholder="Ask a question about your document...", label="Your question")
            ask_btn = gr.Button("Ask", variant="primary")

    upload_btn.click(process_file, inputs=file_input, outputs=status)
    ask_btn.click(chat, inputs=[query_box, chatbot], outputs=chatbot)
    query_box.submit(chat, inputs=[query_box, chatbot], outputs=chatbot)

demo.launch(
    server_name="0.0.0.0", 
    server_port=7860,
    ssr_mode=False,
    show_error=True,
    quiet=False)