File size: 3,959 Bytes
909ffed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import gradio as gr

from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

# MODEL
model_name = "google/flan-t5-base"

model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# 1. PROCESS PDF
def process_pdfs(files):

    if not files:
        return None, "No files uploaded."

    all_docs = []

    for file in files:
        loader = PyPDFLoader(file.name)
        docs = loader.load()
        all_docs.extend(docs)

    splitter = RecursiveCharacterTextSplitter(
        chunk_size=500,
        chunk_overlap=100
    )

    split_docs = splitter.split_documents(all_docs)

    embeddings = HuggingFaceEmbeddings(
        model_name="sentence-transformers/all-MiniLM-L6-v2"
    )

    vectorstore = FAISS.from_documents(split_docs, embeddings)

    return vectorstore, f"βœ… {len(split_docs)} chunks processed"


# 2. ASK QUESTION

def ask_question(question, vectorstore):

    if vectorstore is None:
        return "❌ Please upload PDF files first."

    docs = vectorstore.similarity_search(question, k=5)
    context = "\n".join([d.page_content for d in docs])

    prompt = f"""

You are a university professor teaching Machine Learning.



You MUST give a detailed, long and structured answer.



Format your answer EXACTLY like this:



1. Definition:

2. Explanation:

3. Intuition (why it is used):

4. Step-by-step working (if applicable):

5. Example:

6. Summary:



Do NOT give short answers.



Context:

{context}



Question:

{question}



Now write a complete educational answer:

"""

#3. generated output
    inputs = tokenizer(prompt, return_tensors="pt", truncation=True)

    with torch.no_grad():
        outputs = model.generate(
    **inputs,
    max_new_tokens=300,
    temperature=0.8,
    top_p=0.9,
    do_sample=True,
    repetition_penalty=1.2
)

    return tokenizer.decode(outputs[0], skip_special_tokens=True)


# GRADIO UI (FIX IS HERE)
import gradio as gr

with gr.Blocks(theme=gr.themes.Soft(), css="""

#title {

    text-align: center;

    font-size: 28px;

    font-weight: bold;

    color: #4A90E2;

}



.container {

    max-width: 900px;

    margin: auto;

}



.chatbox {

    height: 400px;

    overflow-y: auto;

    border-radius: 12px;

}



button {

    border-radius: 10px !important;

}

""") as demo:

    gr.Markdown("<h1 id='title'>πŸ“„ AI PDF Q&A Chatbot (RAG System)</h1>")

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“‚ Upload PDF")
            pdf_input = gr.File(file_count="multiple", file_types=[".pdf"])
            upload_btn = gr.Button(" Process PDFs", variant="primary")
            upload_output = gr.Textbox(label="Status")

        with gr.Column(scale=2):
            gr.Markdown("### πŸ’¬ Ask Questions")

            question_input = gr.Textbox(
                placeholder="Ask anything from your PDF...",
                label="Your Question"
            )

            ask_btn = gr.Button(" Ask AI", variant="primary")

            answer_output = gr.Textbox(
                label="AI Answer",
                lines=10
            )

    # -------------------------
    # STATE
    # -------------------------
    vector_state = gr.State(None)

    # -------------------------
    # EVENTS
    # -------------------------
    upload_btn.click(
        process_pdfs,
        inputs=pdf_input,
        outputs=[vector_state, upload_output]
    )

    ask_btn.click(
        ask_question,
        inputs=[question_input, vector_state],
        outputs=answer_output
    )

demo.launch()