File size: 2,036 Bytes
793ff90
 
 
4e6992c
793ff90
4e6992c
793ff90
 
 
 
 
f1a0e2a
793ff90
 
 
f1a0e2a
793ff90
4e6992c
f1a0e2a
 
 
 
 
 
4e6992c
f1a0e2a
 
 
 
 
 
 
 
 
4e6992c
793ff90
 
4e6992c
7f8844d
f1a0e2a
 
 
 
 
 
 
 
 
4e6992c
 
 
 
 
 
793ff90
4e6992c
f1a0e2a
 
 
 
 
 
 
793ff90
4e6992c
793ff90
91de782
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
import gradio as gr
from transformers import pipeline
import PyPDF2
import re

# Load syllabus from PDF
def read_pdf(file_path):
    try:
        with open(file_path, "rb") as file:
            reader = PyPDF2.PdfReader(file)
            text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
        return text
    except Exception as e:
        return f"Error loading syllabus: {str(e)}"

syllabus_text = read_pdf("Syllabus.pdf")

# Extract subjects and topics
def extract_subjects_and_topics(text):
    subjects = {}
    current_subject = None

    for line in text.split("\n"):
        line = line.strip()
        if re.match(r"^[A-Z ]+$", line):  # Matches UPPERCASE subject names
            current_subject = line
            subjects[current_subject] = []
        elif current_subject and line:
            subjects[current_subject].append(line)

    return subjects

subjects_data = extract_subjects_and_topics(syllabus_text)

# Load AI Model for Chatbot
chatbot = pipeline("text-generation", model="facebook/blenderbot-400M-distill")

# Chat function
def chat_response(message):
    message = message.lower()

    if "subjects" in message:
        return "πŸ“š Available Subjects:\n\n" + "\n".join(subjects_data.keys())

    for subject, topics in subjects_data.items():
        if subject.lower() in message:
            return f"πŸ“– Topics under {subject}:\n\n" + "\n".join(topics)

    for subject, topics in subjects_data.items():
        for topic in topics:
            if topic.lower() in message:
                return f"πŸ“Œ {topic} is covered under {subject}. Refer to your syllabus for details."

    return "❌ Topic not found in syllabus. Please check the spelling or ask about a different topic."

# Create Gradio Interface
iface = gr.Interface(
    fn=chat_response,
    inputs="text",
    outputs="text",
    title="Bit GPT 0.2.8",
    description="Ask me about syllabus subjects, topics, or general questions!"
)

# Launch App
if __name__ == "__main__":
    iface.launch()