Bit_gpt_0.2.8 / app.py
Piux24's picture
Update app.py
4e6992c verified
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()