Piux24 commited on
Commit
4e6992c
Β·
verified Β·
1 Parent(s): f1a0e2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  import PyPDF2
4
- import json
5
 
6
- # πŸ“Œ Step 1: Extract text from PDF
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
- # πŸ“Œ Step 2: 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 line.isupper(): # Assuming subject names are in uppercase
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
- # πŸ“Œ Step 3: Convert to JSON format for easy searching
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
- # πŸ“Œ Step 4: Chat Function
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
- # If chatbot response is needed
55
- response = chatbot(message, max_length=100, do_sample=True)
56
- return response[0]['generated_text']
 
 
 
57
 
58
- # πŸ“Œ Step 5: Create Gradio Interface
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
- # πŸ“Œ Step 6: Launch App
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()