Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +112 -0
- html_pdf.py +50 -0
- model.py +73 -0
- pdf_process.py +15 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from model import AllModels
|
3 |
+
from html_pdf import Pdf
|
4 |
+
from pdf_process import PdfExtract
|
5 |
+
from PIL import Image
|
6 |
+
model = AllModels()
|
7 |
+
roadmap_generator = Pdf()
|
8 |
+
|
9 |
+
|
10 |
+
st.title('NoteFusion')
|
11 |
+
st.markdown("""
|
12 |
+
<style>
|
13 |
+
.justified-text {
|
14 |
+
text-align: justify;
|
15 |
+
}
|
16 |
+
</style>
|
17 |
+
""", unsafe_allow_html=True)
|
18 |
+
|
19 |
+
# Sidebar Section
|
20 |
+
st.sidebar.title("About")
|
21 |
+
|
22 |
+
st.sidebar.caption("""
|
23 |
+
<div class="justified-text">
|
24 |
+
NoteFusion is an AI-powered educational assistant helps students by providing summarizing textbook content, and generating personalized study plans roadmap Pdf and converts image to Notes. Powered by the Gemini API and built with Streamlit.
|
25 |
+
</div>
|
26 |
+
""", unsafe_allow_html=True)
|
27 |
+
|
28 |
+
for _ in range(3):
|
29 |
+
st.sidebar.write("")
|
30 |
+
|
31 |
+
menu = ["Image To Notes", "Summarize Textbook", "Generate Roadmap PDF","Generate own book"]
|
32 |
+
choice = st.sidebar.selectbox("Choose an option", menu)
|
33 |
+
|
34 |
+
for _ in range(3):
|
35 |
+
st.sidebar.write("")
|
36 |
+
|
37 |
+
if choice == 'Generate Roadmap PDF':
|
38 |
+
st.subheader('Generate Roadmap PDF')
|
39 |
+
subjects = st.text_input("Enter the subjects you need help with (comma-separated)")
|
40 |
+
if subjects:
|
41 |
+
button = st.button("Generate RoadMap")
|
42 |
+
if subjects and button:
|
43 |
+
with st.spinner('creating...'):
|
44 |
+
response = model.road_map_model(subjects)
|
45 |
+
print(response)
|
46 |
+
response = response.replace("```html", "")
|
47 |
+
response = response.replace("```", "")
|
48 |
+
pdf_bytes = roadmap_generator.pdf_creator(response)
|
49 |
+
st.success("PDF generated successfully!")
|
50 |
+
|
51 |
+
st.download_button("Download PDF", data=pdf_bytes, file_name="output.pdf", mime="application/pdf")
|
52 |
+
|
53 |
+
elif choice == "Summarize Textbook":
|
54 |
+
st.subheader("Summarize Textbook")
|
55 |
+
uploaded_file = st.file_uploader("Upload a textbook pdf", type=["pdf",'png','jpg','jpeg'])
|
56 |
+
if uploaded_file:
|
57 |
+
button = st.button("Generate Summary")
|
58 |
+
type = uploaded_file.name.split('.')[-1]
|
59 |
+
if type!='pdf':
|
60 |
+
st.image(uploaded_file,caption='uploaded image')
|
61 |
+
else:
|
62 |
+
pdf_text = PdfExtract(uploaded_file)
|
63 |
+
if uploaded_file and button:
|
64 |
+
with st.spinner('creating...'):
|
65 |
+
if type == 'pdf':
|
66 |
+
response = model.summary_model(input_doc = pdf_text.extracted_text)
|
67 |
+
st.write(response)
|
68 |
+
else:
|
69 |
+
img = Image.open(uploaded_file)
|
70 |
+
response = model.summary_model(image=img)
|
71 |
+
st.write(response)
|
72 |
+
|
73 |
+
|
74 |
+
elif choice == "Generate own book":
|
75 |
+
st.subheader("Create your own book")
|
76 |
+
st.caption('Give the book name and pages our AI will create book for you')
|
77 |
+
book_name = st.text_input("textbook Naame")
|
78 |
+
if book_name:
|
79 |
+
button = st.button("Generate Book")
|
80 |
+
if book_name and button:
|
81 |
+
with st.spinner('creating...'):
|
82 |
+
response = model.book_creator(book_name)
|
83 |
+
st.write(response)
|
84 |
+
response = response.replace("```html", "")
|
85 |
+
response = response.replace("```", "")
|
86 |
+
|
87 |
+
pdf_bytes = roadmap_generator.pdf_creator(response)
|
88 |
+
st.success("PDF generated successfully!")
|
89 |
+
|
90 |
+
st.download_button("Download PDF", data=pdf_bytes, file_name="output.pdf", mime="application/pdf")
|
91 |
+
|
92 |
+
elif choice == "Image To Notes":
|
93 |
+
st.subheader("Image To Notes")
|
94 |
+
uploaded_file = st.file_uploader("Upload a image or screenshot", type=['png','jpg','jpeg'])
|
95 |
+
if uploaded_file:
|
96 |
+
st.image(uploaded_file,caption='uploaded image')
|
97 |
+
|
98 |
+
button = st.button("Generate Notes")
|
99 |
+
|
100 |
+
if uploaded_file and button:
|
101 |
+
with st.spinner('creating...'):
|
102 |
+
img = Image.open(uploaded_file)
|
103 |
+
response = model.note_model(img)
|
104 |
+
st.write(response)
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
html_pdf.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fpdf import FPDF
|
2 |
+
from fpdf.html import HTMLMixin
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
|
7 |
+
class MyFPDF(FPDF, HTMLMixin):
|
8 |
+
def header(self):
|
9 |
+
# Add a border around the page
|
10 |
+
self.rect(5.0, 5.0, self.w - 10.0, self.h - 10.0)
|
11 |
+
|
12 |
+
class Pdf:
|
13 |
+
def __init__(self):
|
14 |
+
self.pdf = MyFPDF()
|
15 |
+
|
16 |
+
def replace_special_characters(self, text):
|
17 |
+
replacements = {
|
18 |
+
'’': "'",
|
19 |
+
'“': '"',
|
20 |
+
'”': '"',
|
21 |
+
'–': '-',
|
22 |
+
'—': '-',
|
23 |
+
'…': '...',
|
24 |
+
}
|
25 |
+
for original, replacement in replacements.items():
|
26 |
+
text = text.replace(original, replacement)
|
27 |
+
return text
|
28 |
+
|
29 |
+
def pdf_creator(self, html_text):
|
30 |
+
self.pdf.add_page()
|
31 |
+
|
32 |
+
# Use the built-in Arial font
|
33 |
+
self.pdf.set_font("Times", size=12)
|
34 |
+
|
35 |
+
soup = BeautifulSoup(html_text, 'html.parser')
|
36 |
+
|
37 |
+
# Replace special characters in the text
|
38 |
+
for element in soup.find_all(text=True):
|
39 |
+
element.replace_with(self.replace_special_characters(element))
|
40 |
+
|
41 |
+
# Convert entire HTML content to PDF
|
42 |
+
self.pdf.write_html(str(soup))
|
43 |
+
|
44 |
+
# Save PDF to bytes object
|
45 |
+
output = BytesIO()
|
46 |
+
self.pdf.output(output)
|
47 |
+
pdf_bytes = output.getvalue()
|
48 |
+
output.close()
|
49 |
+
|
50 |
+
return pdf_bytes
|
model.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
7 |
+
genai.configure(api_key="AIzaSyDA6zE16NjMSYefmnIo74aJ8cacoDxJsrE")
|
8 |
+
|
9 |
+
|
10 |
+
class AllModels:
|
11 |
+
def __init__(self):
|
12 |
+
self.model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
13 |
+
|
14 |
+
|
15 |
+
def road_map_model(self,subjects):
|
16 |
+
prompt = f"""Create an HTML format roadmap for learning {subjects} with proving week task . The roadmap should include the following sections:
|
17 |
+
|
18 |
+
Introduction: Brief overview of the subject and its importance.
|
19 |
+
|
20 |
+
Foundation: Fundamental concepts and theories to grasp before diving deeper.
|
21 |
+
|
22 |
+
Intermediate Level: More advanced topics building upon the foundation.
|
23 |
+
|
24 |
+
Advanced Level: Complex concepts, techniques, or applications within the subject.
|
25 |
+
|
26 |
+
Resources: List of recommended books, online courses, tutorials, and websites for further learning using hyperlink tag.
|
27 |
+
|
28 |
+
Projects: Hands-on projects or exercises to reinforce learning and apply knowledge.
|
29 |
+
|
30 |
+
Conclusion: Summary of key takeaways and encouragement for further exploration.
|
31 |
+
|
32 |
+
The HTML format should be visually appealing with appropriate headings, subheadings, bullet points, and hyperlinks where necessary. Ensure clarity and logical progression in the roadmap.
|
33 |
+
NOTE: The roadmap should be provided in pure HTML format without any Markdown or other formatting styles and In the html format dont need style tags and title tags"""
|
34 |
+
|
35 |
+
response = self.model.generate_content([prompt.format(subjects=subjects)])
|
36 |
+
return response.text
|
37 |
+
|
38 |
+
|
39 |
+
def summary_model(self,input_doc=None,image=None):
|
40 |
+
if input_doc:
|
41 |
+
prompt = """you are an intelligent assistant using the giving document summarize it shortly
|
42 |
+
document:
|
43 |
+
{document}
|
44 |
+
"""
|
45 |
+
response = self.model.generate_content([prompt.format(document=input_doc)])
|
46 |
+
return response.text
|
47 |
+
elif image:
|
48 |
+
prompt = """you are an intelligent assistant using the giving image summarize it and its content."""
|
49 |
+
response = self.model.generate_content([prompt,image])
|
50 |
+
return response.text
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
def book_creator(self,book_name):
|
56 |
+
prompt = """"Generate a detailed and extensive book titled '{book_title}'.
|
57 |
+
Please provide the content in HTML format with appropriate tags for headings, paragraphs,
|
58 |
+
NOTE: The book should be provided in pure "HTML" format without any "Markdown" or other formatting styles and In the html format dont need style tags and title tags
|
59 |
+
Ensure the HTML is well-structured and valid. Here is the beginning of the book in HTML:\n\n
|
60 |
+
<html>\n<head>\n<title>'{book_title}'</title>\n</head>\n<body>\n"
|
61 |
+
<h1>'{book_title}'</h1>\n"
|
62 |
+
<div>\n"""
|
63 |
+
|
64 |
+
response = self.model.generate_content([prompt.format(book_title=book_name)])
|
65 |
+
return response.text
|
66 |
+
|
67 |
+
def note_model(self,img):
|
68 |
+
prompt = """You are an intelligent Notes creator.create a notes using given screenshot
|
69 |
+
important note: if the screenshot not contain any text means you must say 'please upload a valid screenshot."""
|
70 |
+
|
71 |
+
response = self.model.generate_content([prompt,img])
|
72 |
+
return response.text
|
73 |
+
|
pdf_process.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PyPDF2 import PdfReader
|
2 |
+
|
3 |
+
class PdfExtract:
|
4 |
+
|
5 |
+
def __init__(self, pdf_file):
|
6 |
+
self.extracted_text = self.get_pdf_text(pdf_file)
|
7 |
+
|
8 |
+
def get_pdf_text(self, pdf_file):
|
9 |
+
text = ""
|
10 |
+
pdf_reader = PdfReader(pdf_file)
|
11 |
+
for page in pdf_reader.pages:
|
12 |
+
page_text = page.extract_text()
|
13 |
+
if page_text:
|
14 |
+
text += page_text
|
15 |
+
return text
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
beautifulsoup4
|
4 |
+
python-dotenv
|
5 |
+
fpdf
|
6 |
+
PyPDF2
|