Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import PyPDF2
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
def read_pdf(file_path):
|
| 8 |
try:
|
| 9 |
with open(file_path, "rb") as file:
|
|
@@ -15,14 +15,14 @@ def read_pdf(file_path):
|
|
| 15 |
|
| 16 |
syllabus_text = read_pdf("Syllabus.pdf")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
def extract_subjects_and_topics(text):
|
| 20 |
subjects = {}
|
| 21 |
current_subject = None
|
| 22 |
|
| 23 |
for line in text.split("\n"):
|
| 24 |
line = line.strip()
|
| 25 |
-
if
|
| 26 |
current_subject = line
|
| 27 |
subjects[current_subject] = []
|
| 28 |
elif current_subject and line:
|
|
@@ -32,30 +32,28 @@ def extract_subjects_and_topics(text):
|
|
| 32 |
|
| 33 |
subjects_data = extract_subjects_and_topics(syllabus_text)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
subjects_json = json.dumps(subjects_data, indent=4)
|
| 37 |
-
|
| 38 |
-
# π Load AI Model for Chatbot
|
| 39 |
chatbot = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
|
| 40 |
|
| 41 |
-
#
|
| 42 |
def chat_response(message):
|
| 43 |
message = message.lower()
|
| 44 |
|
| 45 |
-
# If user asks for subjects
|
| 46 |
if "subjects" in message:
|
| 47 |
return "π Available Subjects:\n\n" + "\n".join(subjects_data.keys())
|
| 48 |
|
| 49 |
-
# If user asks for topics under a subject
|
| 50 |
for subject, topics in subjects_data.items():
|
| 51 |
if subject.lower() in message:
|
| 52 |
return f"π Topics under {subject}:\n\n" + "\n".join(topics)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
iface = gr.Interface(
|
| 60 |
fn=chat_response,
|
| 61 |
inputs="text",
|
|
@@ -64,6 +62,6 @@ iface = gr.Interface(
|
|
| 64 |
description="Ask me about syllabus subjects, topics, or general questions!"
|
| 65 |
)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
if __name__ == "__main__":
|
| 69 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import PyPDF2
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
+
# Load syllabus from PDF
|
| 7 |
def read_pdf(file_path):
|
| 8 |
try:
|
| 9 |
with open(file_path, "rb") as file:
|
|
|
|
| 15 |
|
| 16 |
syllabus_text = read_pdf("Syllabus.pdf")
|
| 17 |
|
| 18 |
+
# Extract subjects and topics
|
| 19 |
def extract_subjects_and_topics(text):
|
| 20 |
subjects = {}
|
| 21 |
current_subject = None
|
| 22 |
|
| 23 |
for line in text.split("\n"):
|
| 24 |
line = line.strip()
|
| 25 |
+
if re.match(r"^[A-Z ]+$", line): # Matches UPPERCASE subject names
|
| 26 |
current_subject = line
|
| 27 |
subjects[current_subject] = []
|
| 28 |
elif current_subject and line:
|
|
|
|
| 32 |
|
| 33 |
subjects_data = extract_subjects_and_topics(syllabus_text)
|
| 34 |
|
| 35 |
+
# Load AI Model for Chatbot
|
|
|
|
|
|
|
|
|
|
| 36 |
chatbot = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
|
| 37 |
|
| 38 |
+
# Chat function
|
| 39 |
def chat_response(message):
|
| 40 |
message = message.lower()
|
| 41 |
|
|
|
|
| 42 |
if "subjects" in message:
|
| 43 |
return "π Available Subjects:\n\n" + "\n".join(subjects_data.keys())
|
| 44 |
|
|
|
|
| 45 |
for subject, topics in subjects_data.items():
|
| 46 |
if subject.lower() in message:
|
| 47 |
return f"π Topics under {subject}:\n\n" + "\n".join(topics)
|
| 48 |
|
| 49 |
+
for subject, topics in subjects_data.items():
|
| 50 |
+
for topic in topics:
|
| 51 |
+
if topic.lower() in message:
|
| 52 |
+
return f"π {topic} is covered under {subject}. Refer to your syllabus for details."
|
| 53 |
+
|
| 54 |
+
return "β Topic not found in syllabus. Please check the spelling or ask about a different topic."
|
| 55 |
|
| 56 |
+
# Create Gradio Interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=chat_response,
|
| 59 |
inputs="text",
|
|
|
|
| 62 |
description="Ask me about syllabus subjects, topics, or general questions!"
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Launch App
|
| 66 |
if __name__ == "__main__":
|
| 67 |
iface.launch()
|