Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Key from environment
|
| 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 |
+
# Generate TOC
|
| 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}".
|
| 19 |
+
|
| 20 |
+
The textbook should include 5 to 7 chapters. Each chapter must have:
|
| 21 |
+
- A clear title
|
| 22 |
+
- 5 to 6 subheadings (titles only)
|
| 23 |
+
|
| 24 |
+
Do NOT write the chapter content. Only return the Table of Contents clearly organized.
|
| 25 |
+
"""
|
| 26 |
+
response = client.chat.completions.create(
|
| 27 |
+
model="llama3-70b-8192",
|
| 28 |
+
messages=[{"role": "user", "content": prompt}],
|
| 29 |
+
temperature=0.7
|
| 30 |
+
)
|
| 31 |
+
toc = response.choices[0].message.content.strip()
|
| 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 == "":
|
| 39 |
+
return "β οΈ Please generate the Table of Contents first."
|
| 40 |
+
|
| 41 |
+
prompt = f"""
|
| 42 |
+
Using the following Table of Contents for a beginner-level textbook on **{topic}**, generate the complete content of **Chapter {chapter_number}**.
|
| 43 |
+
|
| 44 |
+
The chapter must include:
|
| 45 |
+
1. Chapter Title
|
| 46 |
+
2. Chapter Introduction
|
| 47 |
+
3. Unit SLOs (Student Learning Outcomes)
|
| 48 |
+
4. Detailed content organized under 5β6 subheadings (as per the TOC)
|
| 49 |
+
5. Relevant Examples under appropriate subheadings
|
| 50 |
+
6. 10 Multiple Choice Questions (MCQs) β just questions and options
|
| 51 |
+
7. Answer Key at the end (numbered 1 to 10)
|
| 52 |
+
8. A Glossary of at least 5 important terms with simple definitions
|
| 53 |
+
|
| 54 |
+
Do NOT include system messages like βHere is the content of...β Just return clean, structured educational content.
|
| 55 |
+
"""
|
| 56 |
+
response = client.chat.completions.create(
|
| 57 |
+
model="llama3-70b-8192",
|
| 58 |
+
messages=[{"role": "user", "content": prompt}],
|
| 59 |
+
temperature=0.7
|
| 60 |
+
)
|
| 61 |
+
chapter = response.choices[0].message.content.strip()
|
| 62 |
+
chapters_memory[chapter_number] = chapter
|
| 63 |
+
return chapter
|
| 64 |
+
|
| 65 |
+
# Generate DOCX
|
| 66 |
+
def download_textbook(topic, grade):
|
| 67 |
+
doc = Document()
|
| 68 |
+
doc.add_heading(topic, 0)
|
| 69 |
+
doc.add_paragraph(f"Author: Najaf Ali Sharqi")
|
| 70 |
+
doc.add_paragraph(f"Grade: {grade}")
|
| 71 |
+
doc.add_paragraph(f"Date of Publication: {datetime.now().strftime('%B %d, %Y')}")
|
| 72 |
+
doc.add_page_break()
|
| 73 |
+
|
| 74 |
+
doc.add_heading("Table of Contents", level=1)
|
| 75 |
+
doc.add_paragraph(toc_memory["toc"])
|
| 76 |
+
doc.add_page_break()
|
| 77 |
+
|
| 78 |
+
for num in sorted(chapters_memory.keys()):
|
| 79 |
+
doc.add_heading(f"Chapter {num}", level=1)
|
| 80 |
+
doc.add_paragraph(chapters_memory[num])
|
| 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 UI
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("## π AI Textbook Generator App")
|
| 90 |
+
gr.Markdown("""
|
| 91 |
+
Welcome to the AI Textbook Generator developed by **Najaf Ali Sharqi**.
|
| 92 |
+
|
| 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 its content
|
| 97 |
+
4. After generating all chapters, click **Download Word File**
|
| 98 |
+
""")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
topic_input = gr.Textbox(label="Textbook Topic", placeholder="e.g., Basics of AI")
|
| 102 |
+
grade_input = gr.Textbox(label="Grade Level", placeholder="e.g., 6")
|
| 103 |
+
|
| 104 |
+
toc_btn = gr.Button("πΉ Generate Table of Contents")
|
| 105 |
+
toc_output = gr.Textbox(label="π Table of Contents", lines=10)
|
| 106 |
+
|
| 107 |
+
toc_btn.click(fn=generate_toc, inputs=topic_input, outputs=toc_output)
|
| 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=gr.Textbox(label="π Chapter Content", lines=30))
|
| 115 |
+
|
| 116 |
+
gr.Markdown("### π€ Export Full Textbook")
|
| 117 |
+
download_btn = gr.Button("β¬οΈ Download Word File")
|
| 118 |
+
download_file = gr.File()
|
| 119 |
+
|
| 120 |
+
download_btn.click(fn=download_textbook, inputs=[topic_input, grade_input], outputs=download_file)
|
| 121 |
+
|
| 122 |
+
gr.Markdown("---")
|
| 123 |
+
gr.Markdown("App developed by **Najaf Ali Sharqi** | Powered by **Groq + Gradio**")
|
| 124 |
+
|
| 125 |
+
demo.launch()
|