Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,6 @@ book_data = {
|
|
| 20 |
}
|
| 21 |
|
| 22 |
# Clean formatting
|
| 23 |
-
|
| 24 |
def clean_formatting(text):
|
| 25 |
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
|
| 26 |
text = re.sub(r"\*(.*?)\*", r"\1", text)
|
|
@@ -29,7 +28,6 @@ def clean_formatting(text):
|
|
| 29 |
return text.strip()
|
| 30 |
|
| 31 |
# Generate TOC and Preface
|
| 32 |
-
|
| 33 |
def generate_book_intro(title, grade, slos_input):
|
| 34 |
book_data["title"] = title.strip()
|
| 35 |
book_data["grade"] = grade.strip()
|
|
@@ -64,7 +62,6 @@ def generate_book_intro(title, grade, slos_input):
|
|
| 64 |
return f"Preface:\n\n{book_data['preface']}\n\nTable of Contents:\n\n" + "\n".join(book_data["toc"])
|
| 65 |
|
| 66 |
# Generate chapter by number
|
| 67 |
-
|
| 68 |
def generate_chapter(ch_num):
|
| 69 |
if ch_num in book_data["chapters"]:
|
| 70 |
return book_data["chapters"][ch_num]
|
|
@@ -100,7 +97,6 @@ def generate_chapter(ch_num):
|
|
| 100 |
return chapter
|
| 101 |
|
| 102 |
# Export full textbook to Word
|
| 103 |
-
|
| 104 |
def export_book():
|
| 105 |
doc = Document()
|
| 106 |
style = doc.styles['Normal']
|
|
@@ -159,26 +155,26 @@ def export_book():
|
|
| 159 |
|
| 160 |
# Gradio Interface
|
| 161 |
with gr.Blocks() as demo:
|
| 162 |
-
gr.Markdown("##
|
| 163 |
|
| 164 |
with gr.Row():
|
| 165 |
title = gr.Textbox(label="Subject Title")
|
| 166 |
grade = gr.Textbox(label="Grade Level")
|
| 167 |
|
| 168 |
slos = gr.Textbox(label="Paste SLOs (One per Line)", lines=10)
|
| 169 |
-
generate_structure = gr.Button("
|
| 170 |
structure_output = gr.Textbox(label="Generated Preface and TOC", lines=15)
|
| 171 |
|
| 172 |
generate_structure.click(fn=generate_book_intro, inputs=[title, grade, slos], outputs=structure_output)
|
| 173 |
|
| 174 |
-
gr.Markdown("###
|
| 175 |
chapter_output = gr.Textbox(label="Generated Chapter Content", lines=25)
|
| 176 |
|
| 177 |
with gr.Row():
|
| 178 |
for i in range(1, 7):
|
| 179 |
gr.Button(f"Generate Chapter {i}").click(fn=generate_chapter, inputs=gr.Number(value=i, visible=False), outputs=chapter_output)
|
| 180 |
|
| 181 |
-
gr.Markdown("###
|
| 182 |
download_btn = gr.Button("Download MS Word Textbook")
|
| 183 |
download_file = gr.File()
|
| 184 |
|
|
@@ -186,4 +182,5 @@ with gr.Blocks() as demo:
|
|
| 186 |
|
| 187 |
gr.Markdown("Developed by **Najaf Ali Sharqi** | Powered by **Groq + Gradio**")
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
| 20 |
}
|
| 21 |
|
| 22 |
# Clean formatting
|
|
|
|
| 23 |
def clean_formatting(text):
|
| 24 |
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
|
| 25 |
text = re.sub(r"\*(.*?)\*", r"\1", text)
|
|
|
|
| 28 |
return text.strip()
|
| 29 |
|
| 30 |
# Generate TOC and Preface
|
|
|
|
| 31 |
def generate_book_intro(title, grade, slos_input):
|
| 32 |
book_data["title"] = title.strip()
|
| 33 |
book_data["grade"] = grade.strip()
|
|
|
|
| 62 |
return f"Preface:\n\n{book_data['preface']}\n\nTable of Contents:\n\n" + "\n".join(book_data["toc"])
|
| 63 |
|
| 64 |
# Generate chapter by number
|
|
|
|
| 65 |
def generate_chapter(ch_num):
|
| 66 |
if ch_num in book_data["chapters"]:
|
| 67 |
return book_data["chapters"][ch_num]
|
|
|
|
| 97 |
return chapter
|
| 98 |
|
| 99 |
# Export full textbook to Word
|
|
|
|
| 100 |
def export_book():
|
| 101 |
doc = Document()
|
| 102 |
style = doc.styles['Normal']
|
|
|
|
| 155 |
|
| 156 |
# Gradio Interface
|
| 157 |
with gr.Blocks() as demo:
|
| 158 |
+
gr.Markdown("## ๐ AI Textbook Generator โ Step-by-Step (APA Style)")
|
| 159 |
|
| 160 |
with gr.Row():
|
| 161 |
title = gr.Textbox(label="Subject Title")
|
| 162 |
grade = gr.Textbox(label="Grade Level")
|
| 163 |
|
| 164 |
slos = gr.Textbox(label="Paste SLOs (One per Line)", lines=10)
|
| 165 |
+
generate_structure = gr.Button("๐ง Generate TOC + Preface")
|
| 166 |
structure_output = gr.Textbox(label="Generated Preface and TOC", lines=15)
|
| 167 |
|
| 168 |
generate_structure.click(fn=generate_book_intro, inputs=[title, grade, slos], outputs=structure_output)
|
| 169 |
|
| 170 |
+
gr.Markdown("### ๐ฅ Generate Chapters One by One")
|
| 171 |
chapter_output = gr.Textbox(label="Generated Chapter Content", lines=25)
|
| 172 |
|
| 173 |
with gr.Row():
|
| 174 |
for i in range(1, 7):
|
| 175 |
gr.Button(f"Generate Chapter {i}").click(fn=generate_chapter, inputs=gr.Number(value=i, visible=False), outputs=chapter_output)
|
| 176 |
|
| 177 |
+
gr.Markdown("### ๐พ Download Complete Textbook")
|
| 178 |
download_btn = gr.Button("Download MS Word Textbook")
|
| 179 |
download_file = gr.File()
|
| 180 |
|
|
|
|
| 182 |
|
| 183 |
gr.Markdown("Developed by **Najaf Ali Sharqi** | Powered by **Groq + Gradio**")
|
| 184 |
|
| 185 |
+
# ๐ This makes it work on Colab/Hugging Face
|
| 186 |
+
demo.launch(share=True)
|