Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import PyPDF2
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# π
|
| 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
|
| 16 |
except Exception as e:
|
| 17 |
return f"Error loading syllabus: {str(e)}"
|
| 18 |
|
| 19 |
-
syllabus_text = read_pdf("Syllabus.pdf")
|
| 20 |
|
| 21 |
-
# π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
chatbot = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
|
| 23 |
|
| 24 |
-
# π
|
| 25 |
def chat_response(message):
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|