Spaces:
Sleeping
Sleeping
File size: 2,102 Bytes
793ff90 f1a0e2a 793ff90 f1a0e2a 793ff90 f1a0e2a 793ff90 f1a0e2a 793ff90 f1a0e2a 793ff90 f1a0e2a 7f8844d f1a0e2a 7f8844d 91de782 793ff90 f1a0e2a 793ff90 f1a0e2a 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 68 69 |
import gradio as gr
from transformers import pipeline
import PyPDF2
import json
# π Step 1: Extract text 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")
# π Step 2: Extract subjects and topics
def extract_subjects_and_topics(text):
subjects = {}
current_subject = None
for line in text.split("\n"):
line = line.strip()
if line.isupper(): # Assuming subject names are in uppercase
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)
# π Step 3: Convert to JSON format for easy searching
subjects_json = json.dumps(subjects_data, indent=4)
# π Load AI Model for Chatbot
chatbot = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
# π Step 4: Chat Function
def chat_response(message):
message = message.lower()
# If user asks for subjects
if "subjects" in message:
return "π Available Subjects:\n\n" + "\n".join(subjects_data.keys())
# If user asks for topics under a subject
for subject, topics in subjects_data.items():
if subject.lower() in message:
return f"π Topics under {subject}:\n\n" + "\n".join(topics)
# If chatbot response is needed
response = chatbot(message, max_length=100, do_sample=True)
return response[0]['generated_text']
# π Step 5: 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!"
)
# π Step 6: Launch App
if __name__ == "__main__":
iface.launch() |