Piux24 commited on
Commit
f1a0e2a
Β·
verified Β·
1 Parent(s): 6465d9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -14
app.py CHANGED
@@ -1,36 +1,69 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  import PyPDF2
4
- import os
5
 
6
- # πŸ“Œ Load syllabus from PDF
7
  def read_pdf(file_path):
8
- if not os.path.exists(file_path):
9
- return "Error: Syllabus file not found!"
10
-
11
  try:
12
  with open(file_path, "rb") as file:
13
  reader = PyPDF2.PdfReader(file)
14
  text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
15
- return text if text.strip() else "Error: Could not extract text from syllabus."
16
  except Exception as e:
17
  return f"Error loading syllabus: {str(e)}"
18
 
19
- syllabus_text = read_pdf("Syllabus.pdf") # πŸ›‘ Ensure the filename matches exactly
20
 
21
- # πŸ“Œ Load AI Model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  chatbot = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
23
 
24
- # πŸ“Œ Define Chat Function
25
  def chat_response(message):
26
- if "syllabus" in message.lower():
27
- return syllabus_text
 
 
 
 
 
 
 
 
 
 
28
  response = chatbot(message, max_length=100, do_sample=True)
29
  return response[0]['generated_text']
30
 
31
- # πŸ“Œ Create Gradio Interface
32
- iface = gr.Interface(fn=chat_response, inputs="text", outputs="text", title="Bit GPT 0.2.8")
 
 
 
 
 
 
33
 
34
- # πŸ“Œ Launch App
35
  if __name__ == "__main__":
36
  iface.launch()
 
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:
10
  reader = PyPDF2.PdfReader(file)
11
  text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
12
+ return text
13
  except Exception as e:
14
  return f"Error loading syllabus: {str(e)}"
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:
29
+ subjects[current_subject].append(line)
30
+
31
+ return subjects
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",
62
+ outputs="text",
63
+ title="Bit GPT 0.2.8",
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()