File size: 9,723 Bytes
445dcdd
c6ef69f
aa8f6d5
 
 
a95d738
 
4bb39fa
a95d738
fae04bd
a95d738
8b1b7e7
a95d738
55ca5ab
a95d738
 
 
 
 
 
 
 
 
4bb39fa
a95d738
 
4bb39fa
 
a95d738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d81bc
a95d738
 
 
 
 
 
 
 
61d81bc
3ae5959
61d81bc
 
 
 
 
 
a0b28a9
3ae5959
61d81bc
 
 
 
 
 
a0b28a9
3ae5959
61d81bc
 
 
 
 
 
a0b28a9
a95d738
 
 
61d81bc
a95d738
 
 
 
 
 
 
 
 
61d81bc
a95d738
 
 
61d81bc
 
 
 
 
 
 
 
 
 
 
 
 
 
fb96bb7
61d81bc
fb96bb7
 
61d81bc
fb96bb7
 
 
 
 
 
 
 
 
 
 
61d81bc
fb96bb7
61d81bc
3ae5959
 
 
61d81bc
3ae5959
fb96bb7
61d81bc
fb96bb7
 
 
61d81bc
fb96bb7
 
61d81bc
3ae5959
 
 
61d81bc
3ae5959
fb96bb7
61d81bc
f039f06
e1dbc0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a95d738
e1dbc0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f039f06
 
fa88c63
e1dbc0c
a95d738
 
6e61cdd
210f7c2
765ba6d
 
 
a95d738
 
 
4bb39fa
a95d738
 
 
4bb39fa
a95d738
765ba6d
f039f06
6e61cdd
61d81bc
 
 
6e61cdd
 
765ba6d
 
a95d738
61d81bc
 
a95d738
61d81bc
 
a95d738
61d81bc
a95d738
61d81bc
fb96bb7
 
 
55fbb7a
6e61cdd
61d81bc
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import gradio as gr
import requests
from bs4 import BeautifulSoup
from PyPDF2 import PdfReader
from docx import Document
from pptx import Presentation
import numpy as np
import faiss
from groq import Groq
import re

# ---------------- Groq client ----------------
client = Groq(api_key=os.environ.get("MY_API"))

# ---------------- FAISS setup ----------------
dimension = 768
index = faiss.IndexFlatL2(dimension)
texts_storage = []

# ---------------- Chunking ----------------
def chunk_text(text, chunk_size=500):
    words = text.split()
    chunks = []
    for i in range(0, len(words), chunk_size):
        chunks.append(" ".join(words[i:i+chunk_size]))
    return chunks

# ---------------- Fake Embedding (replace with real later) ----------------
def embed(text):
    return np.random.rand(dimension).astype("float32")

# ---------------- Retrieval ----------------
def retrieve_context(query, top_k=5):
    q_emb = embed(query)
    distances, positions = index.search(np.array([q_emb]), top_k)
    results = [texts_storage[p] for p in positions[0] if p < len(texts_storage)]
    return "\n".join(results)

# ---------------- Document Extractors ----------------
def extract_pdf(file):
    reader = PdfReader(file)
    text = ""
    for page in reader.pages:
        text += page.extract_text() or ""
    return text

def extract_docx(file):
    doc = Document(file)
    return "\n".join([p.text for p in doc.paragraphs])

def extract_pptx(file):
    prs = Presentation(file)
    text = ""
    for slide in prs.slides:
        for shape in slide.shapes:
            if hasattr(shape, "text"):
                text += shape.text + "\n"
    return text

def extract_text_from_url(url):
    try:
        r = requests.get(url, timeout=10)
        r.raise_for_status()
        soup = BeautifulSoup(r.text, "html.parser")
        for s in soup(["script", "style"]):
            s.decompose()
        return soup.get_text(separator="\n")
    except Exception as e:
        return f"Error fetching URL: {e}"

# ---------------- Build FAISS Index with Chunks ----------------
def build_index(text):
    chunks = chunk_text(text)
    for ch in chunks:
        emb = embed(ch)
        index.add(np.array([emb]))
        texts_storage.append(ch)
    return "Index built successfully"

# ---------------- Generate Summary, MCQs, Short QA separately ----------------
def generate_summary(text):
    prompt = f"Write a detailed summary of the following content:\n\n{text}"
    response = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama-3.1-8b-instant"
    )
    return response.choices[0].message.content

def generate_mcqs(text, num=20):
    prompt = f"Create {num} multiple choice questions with answers based on the text below:\n\n{text}"
    response = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama-3.1-8b-instant"
    )
    return response.choices[0].message.content

def generate_shortqa(text, num=20):
    prompt = f"Create {num} short questions and answers based on the text below:\n\n{text}"
    response = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama-3.1-8b-instant"
    )
    return response.choices[0].message.content

# ---------------- Ask Question with RAG ----------------
def ask_question(question):
    context = retrieve_context(question, top_k=5)

    prompt = f"""
Answer based on the context below.
If context is irrelevant, answer general knowledge.
CONTEXT:
{context}
Question: {question}
"""
    response = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama-3.1-8b-instant"
    )
    return response.choices[0].message.content

# ---------------- Split Output into Sections ----------------
def split_analysis(text):
    summary = mcqs = short_qa = ""

    summary_match = re.search(r"\*\*Summary\*\*(.*?)(?=\*\*15 Multiple Choice Questions)", text, re.DOTALL)
    mcqs_match = re.search(r"\*\*15 Multiple Choice Questions\*\*(.*?)(?=\*\*15 Short Q/A)", text, re.DOTALL)
    short_match = re.search(r"\*\*15 Short Q/A\*\*(.*)", text, re.DOTALL)

    if summary_match: summary = summary_match.group(1).strip()
    if mcqs_match: mcqs = mcqs_match.group(1).strip()
    if short_match: short_qa = short_match.group(1).strip()

    return summary, mcqs, short_qa
    # -------- Button Functions --------
def process_documents(files):
    # RESET FAISS + STORAGE
    index.reset()
    texts_storage.clear()

    text = ""
    for f in files:
        n = f.name.lower()
        if n.endswith(".pdf"):
            text += extract_pdf(f)
        elif n.endswith(".docx"):
            text += extract_docx(f)
        elif n.endswith(".pptx"):
            text += extract_pptx(f)
        else:
            text += f.read().decode("utf-8")

    build_index(text)

    summary = generate_summary(text)
    mcqs = generate_mcqs(text, num=20)
    shortqa = generate_shortqa(text, num=20)

    return summary, mcqs, shortqa


def process_website(url):
    index.reset()
    texts_storage.clear()

    text = extract_text_from_url(url)
    build_index(text)

    summary = generate_summary(text)
    mcqs = generate_mcqs(text, num=20)
    shortqa = generate_shortqa(text, num=20)

    return summary, mcqs, shortqa


custom_css = """
/* ---------- Global Dark Theme ---------- */
body, .gradio-container {
    font-family: 'Inter', sans-serif;
    background: #0d0d0d !important;
    color: #d6e2f0 !important;
}
/* ---------- Page Title ---------- */
h1, h2, h3, h4 {
    color: #e8f5ff !important;
    font-weight: 700;
}
/* ---------- Main Card Container ---------- */
.gr-block, .gr-panel, .gr-group, .gr-accordion {
    background: rgba(20, 20, 20, 0.65) !important;
    border: 1px solid rgba(0, 255, 180, 0.15) !important;
    border-radius: 18px !important;
    backdrop-filter: blur(10px) !important;
    padding: 18px !important;
}
/* ---------- Tabs ---------- */
.gradio-tab {
    background: #0c0c0c !important;
}
.gradio-tab button {
    background: transparent !important;
    color: #b8c7d9 !important;
    padding: 10px 16px !important;
    border-radius: 10px !important;
    font-weight: 600;
    border: none !important;
}
.gradio-tab button:hover {
    background: rgba(0, 255, 160, 0.08) !important;
}
.gradio-tab button.selected {
    background: rgba(0, 255, 160, 0.16) !important;
    color: #00ffb4 !important;
    border: 1px solid rgba(0, 255, 160, 0.35) !important;
}
/* ---------- Textboxes ---------- */
textarea, input, .gr-textbox, .gr-textbox textarea {
    background: rgba(30, 30, 30, 0.7) !important;
    border: 1px solid rgba(0, 255, 160, 0.2) !important;
    border-radius: 14px !important;
    color: #d8f0ff !important;
    padding: 12px !important;
    font-size: 15px !important;
}
/* ---------- Scroll Boxes (Summary, MCQ, QA) ---------- */
#summary_box, #mcqs_box, #qa_box, #chat_output {
    background: rgba(20, 20, 20, 0.6) !important;
    border: 1px solid rgba(0, 255, 160, 0.25) !important;
    border-radius: 16px !important;
    padding: 14px !important;
    height: 500px;
    overflow-y: scroll;
    color: #eafffa !important;
}
/* ---------- Buttons ---------- */
button {
    background: linear-gradient(135deg, #00ffb4, #00c78c) !important;
    color: #000 !important;
    font-weight: 700 !important;
    border-radius: 12px !important;
    padding: 12px 20px !important;
    border: none !important;
    transition: 0.25s ease;
}
button:hover {
    transform: scale(1.03);
    background: linear-gradient(135deg, #00ffcf, #00e0a4) !important;
}
/* ---------- File Upload Box ---------- */
.gr-file-upload {
    background: rgba(20, 20, 20, 0.7) !important;
    border: 2px dashed rgba(0, 255, 160, 0.35) !important;
    border-radius: 16px !important;
    padding: 20px !important;
}
.gr-file-upload:hover {
    border-color: #00ffb4 !important;
}
"""


# ---------------- UI ----------------
with gr.Blocks() as ui:
    gr.HTML("<style>" + custom_css + "</style>")
    gr.Markdown("# πŸ“š StudyAssistant AI")

    with gr.Row():
        with gr.Column(scale=1):
            with gr.Tab("πŸ“„ Documents"):
                doc_files = gr.File(label="Upload PDF / DOCX / PPTX", file_count="multiple")
                doc_process = gr.Button("Process Documents")

            with gr.Tab("🌐 Website"):
                website_url = gr.Textbox(label="Enter Website URL")
                website_process = gr.Button("Process Website")

            with gr.Tab("πŸ’¬ Chatbot"):
                chat_input = gr.Textbox(label="Ask a Question")
                chat_button = gr.Button("Ask")
                chat_output = gr.Textbox(
                    label="Answer", 
                    lines=10, 
                    interactive=True, 
                    elem_id="chat_output"
                )

        with gr.Column(scale=2):
            with gr.Tab("πŸ“‘ Summary"):
                summary_box = gr.Textbox(lines=25, interactive=True, elem_id="summary_box")

            with gr.Tab("πŸ“ MCQs"):
                mcqs_box = gr.Textbox(lines=25, interactive=True, elem_id="mcqs_box")

            with gr.Tab("❓ Short Q/A"):
                qa_box = gr.Textbox(lines=25, interactive=True, elem_id="qa_box")

# -------- Button Click Bindings (INSIDE Blocks context!) --------
    doc_process.click(process_documents, inputs=doc_files, outputs=[summary_box, mcqs_box, qa_box])
    website_process.click(process_website, inputs=website_url, outputs=[summary_box, mcqs_box, qa_box])
    chat_button.click(ask_question, inputs=chat_input, outputs=chat_output)


# -------- Launch UI --------
ui.launch()