Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,25 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
from groq import Groq
|
| 4 |
from docx import Document
|
| 5 |
from docx.shared import Pt
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
# Load GROQ API
|
| 9 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 10 |
|
| 11 |
# Memory to store TOC and chapters
|
| 12 |
toc_memory = {"toc": ""}
|
| 13 |
chapters_memory = {}
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def generate_toc(topic):
|
| 17 |
prompt = f"""
|
| 18 |
You are an expert curriculum developer. Create a detailed Table of Contents for an absolute beginner-level textbook on the topic: "{topic}".
|
|
@@ -32,7 +39,7 @@ Do NOT write the chapter content. Only return the Table of Contents clearly orga
|
|
| 32 |
toc_memory["toc"] = toc
|
| 33 |
return toc
|
| 34 |
|
| 35 |
-
# Generate Chapter
|
| 36 |
def generate_chapter(topic, chapter_number):
|
| 37 |
toc = toc_memory["toc"]
|
| 38 |
if toc == "":
|
|
@@ -62,29 +69,61 @@ Do NOT include system messages like βHere is the content of...β Just return
|
|
| 62 |
chapters_memory[chapter_number] = chapter
|
| 63 |
return chapter
|
| 64 |
|
| 65 |
-
# Generate
|
| 66 |
def download_textbook(topic, grade):
|
| 67 |
doc = Document()
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
doc.
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
doc.add_page_break()
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
doc.add_page_break()
|
| 77 |
|
|
|
|
| 78 |
for num in sorted(chapters_memory.keys()):
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
doc.add_page_break()
|
| 82 |
|
|
|
|
| 83 |
file_path = "/tmp/Generated_Textbook.docx"
|
| 84 |
doc.save(file_path)
|
| 85 |
return file_path
|
| 86 |
|
| 87 |
-
# Gradio
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
gr.Markdown("## π AI Textbook Generator App")
|
| 90 |
gr.Markdown("""
|
|
@@ -93,7 +132,7 @@ Welcome to the AI Textbook Generator developed by **Najaf Ali Sharqi**.
|
|
| 93 |
### π Instructions:
|
| 94 |
1. Enter the **textbook topic** and **grade level**
|
| 95 |
2. Click **Generate Table of Contents**
|
| 96 |
-
3. Tap a chapter button to generate
|
| 97 |
4. After generating all chapters, click **Download Word File**
|
| 98 |
""")
|
| 99 |
|
|
@@ -108,12 +147,15 @@ Welcome to the AI Textbook Generator developed by **Najaf Ali Sharqi**.
|
|
| 108 |
|
| 109 |
gr.Markdown("### π₯ Generate Chapter Content")
|
| 110 |
|
|
|
|
|
|
|
| 111 |
with gr.Row():
|
| 112 |
for i in range(1, 8):
|
| 113 |
btn = gr.Button(f"Chapter {i}")
|
| 114 |
-
btn.click(fn=generate_chapter, inputs=[topic_input, gr.Textbox(value=str(i), visible=False)], outputs=
|
| 115 |
|
| 116 |
gr.Markdown("### π€ Export Full Textbook")
|
|
|
|
| 117 |
download_btn = gr.Button("β¬οΈ Download Word File")
|
| 118 |
download_file = gr.File()
|
| 119 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
from datetime import datetime
|
| 4 |
from groq import Groq
|
| 5 |
from docx import Document
|
| 6 |
from docx.shared import Pt
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Load GROQ API key from Hugging Face secret environment
|
| 10 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 11 |
|
| 12 |
# Memory to store TOC and chapters
|
| 13 |
toc_memory = {"toc": ""}
|
| 14 |
chapters_memory = {}
|
| 15 |
|
| 16 |
+
# π§ Clean markdown, emojis, bullets
|
| 17 |
+
def clean_formatting(text):
|
| 18 |
+
text = re.sub(r"[*#β’βββ
πΉπΈππππ€π₯β¬οΈπ>]+", "", text)
|
| 19 |
+
text = re.sub(r"\s{2,}", " ", text)
|
| 20 |
+
return text.strip()
|
| 21 |
+
|
| 22 |
+
# π Generate Table of Contents
|
| 23 |
def generate_toc(topic):
|
| 24 |
prompt = f"""
|
| 25 |
You are an expert curriculum developer. Create a detailed Table of Contents for an absolute beginner-level textbook on the topic: "{topic}".
|
|
|
|
| 39 |
toc_memory["toc"] = toc
|
| 40 |
return toc
|
| 41 |
|
| 42 |
+
# π Generate Chapter
|
| 43 |
def generate_chapter(topic, chapter_number):
|
| 44 |
toc = toc_memory["toc"]
|
| 45 |
if toc == "":
|
|
|
|
| 69 |
chapters_memory[chapter_number] = chapter
|
| 70 |
return chapter
|
| 71 |
|
| 72 |
+
# π Generate Word document
|
| 73 |
def download_textbook(topic, grade):
|
| 74 |
doc = Document()
|
| 75 |
+
|
| 76 |
+
# === Set Default Font ===
|
| 77 |
+
style = doc.styles['Normal']
|
| 78 |
+
font = style.font
|
| 79 |
+
font.name = 'Times New Roman'
|
| 80 |
+
font.size = Pt(12)
|
| 81 |
+
|
| 82 |
+
def add_paragraph(text, size=12, bold=False, space_after=12):
|
| 83 |
+
p = doc.add_paragraph()
|
| 84 |
+
run = p.add_run(text)
|
| 85 |
+
run.font.name = 'Times New Roman'
|
| 86 |
+
run.font.size = Pt(size)
|
| 87 |
+
run.bold = bold
|
| 88 |
+
p.paragraph_format.line_spacing = 1.5
|
| 89 |
+
p.paragraph_format.space_after = Pt(space_after)
|
| 90 |
+
return p
|
| 91 |
+
|
| 92 |
+
def add_heading(text, level=1):
|
| 93 |
+
size_map = {1: 16, 2: 14}
|
| 94 |
+
return add_paragraph(text.strip(), size=size_map.get(level, 12), bold=True)
|
| 95 |
+
|
| 96 |
+
# === Cover Page ===
|
| 97 |
+
add_heading(topic.upper(), level=1)
|
| 98 |
+
add_paragraph(f"Author: Najaf Ali Sharqi")
|
| 99 |
+
add_paragraph(f"Grade: {grade}")
|
| 100 |
+
add_paragraph(f"Date of Publication: {datetime.now().strftime('%B %d, %Y')}")
|
| 101 |
doc.add_page_break()
|
| 102 |
|
| 103 |
+
# === Table of Contents ===
|
| 104 |
+
add_heading("Table of Contents", level=1)
|
| 105 |
+
add_paragraph(toc_memory["toc"])
|
| 106 |
doc.add_page_break()
|
| 107 |
|
| 108 |
+
# === Chapters ===
|
| 109 |
for num in sorted(chapters_memory.keys()):
|
| 110 |
+
add_heading(f"Chapter {num}", level=1)
|
| 111 |
+
cleaned = clean_formatting(chapters_memory[num])
|
| 112 |
+
for line in cleaned.split('\n'):
|
| 113 |
+
if line.strip() == "":
|
| 114 |
+
continue
|
| 115 |
+
if any(keyword in line.lower() for keyword in ["introduction", "unit slos", "examples", "glossary", "answer key", "mcqs"]):
|
| 116 |
+
add_heading(line.strip(), level=2)
|
| 117 |
+
else:
|
| 118 |
+
add_paragraph(line.strip())
|
| 119 |
doc.add_page_break()
|
| 120 |
|
| 121 |
+
# Save file
|
| 122 |
file_path = "/tmp/Generated_Textbook.docx"
|
| 123 |
doc.save(file_path)
|
| 124 |
return file_path
|
| 125 |
|
| 126 |
+
# π Gradio Interface
|
| 127 |
with gr.Blocks() as demo:
|
| 128 |
gr.Markdown("## π AI Textbook Generator App")
|
| 129 |
gr.Markdown("""
|
|
|
|
| 132 |
### π Instructions:
|
| 133 |
1. Enter the **textbook topic** and **grade level**
|
| 134 |
2. Click **Generate Table of Contents**
|
| 135 |
+
3. Tap a chapter button to generate that chapter
|
| 136 |
4. After generating all chapters, click **Download Word File**
|
| 137 |
""")
|
| 138 |
|
|
|
|
| 147 |
|
| 148 |
gr.Markdown("### π₯ Generate Chapter Content")
|
| 149 |
|
| 150 |
+
chapter_display = gr.Textbox(label="π Chapter Content", lines=30)
|
| 151 |
+
|
| 152 |
with gr.Row():
|
| 153 |
for i in range(1, 8):
|
| 154 |
btn = gr.Button(f"Chapter {i}")
|
| 155 |
+
btn.click(fn=generate_chapter, inputs=[topic_input, gr.Textbox(value=str(i), visible=False)], outputs=chapter_display)
|
| 156 |
|
| 157 |
gr.Markdown("### π€ Export Full Textbook")
|
| 158 |
+
|
| 159 |
download_btn = gr.Button("β¬οΈ Download Word File")
|
| 160 |
download_file = gr.File()
|
| 161 |
|