bk939448 commited on
Commit
7ef50fb
·
verified ·
1 Parent(s): 251c997

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -38
app.py CHANGED
@@ -1,80 +1,55 @@
1
  import gradio as gr
2
  from weasyprint import HTML, CSS
3
- import os
4
-
5
- # We no longer read the CSS file here. We will let WeasyPrint handle it.
6
 
7
  def generate_pdf(html_content):
8
  """
9
- Takes HTML content as a string and returns the path to the generated PDF.
10
  """
11
- # The base_url is still good practice for any other relative links in the HTML.
12
- html = HTML(string=html_content, base_url='.')
13
-
14
- # --- THIS IS THE FIX ---
15
- # Instead of passing a pre-read string, we create the CSS object
16
- # directly from the filename. This preserves the path context.
17
  pdf_bytes = html.write_pdf(stylesheets=[CSS('style.css')])
18
-
19
- # Save the PDF to a temporary file that Gradio can serve
20
- output_filename = "mcq_document.pdf"
21
  with open(output_filename, "wb") as f:
22
  f.write(pdf_bytes)
23
-
24
  return output_filename
25
 
26
- # Default example text for the input box
27
  example_html = """
28
  <!DOCTYPE html>
29
  <html>
30
- <head>
31
- <title>MCQ Document</title>
32
- </head>
33
  <body>
34
  <div class="content-wrapper">
35
-
36
  <h2>अध्याय 1: इतिहास</h2>
37
-
38
  <div class="mcq-item">
39
- <p class="question-text">1. इतिास ा य प्रश्न बहुत महत्वपूर्ण है।</p>
40
  <div class="options-grid">
41
  <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div>
42
  <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div>
43
  </div>
44
  </div>
45
-
46
- <div class="mcq-item">
47
- <p class="question-text">2. यह दूसरा प्रश्न है।</p>
48
- <div class="options-grid">
49
- <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div>
50
- <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div>
51
- </div>
52
- </div>
53
-
54
  <h2>अध्याय 2: भूगोल</h2>
55
-
56
  <div class="mcq-item">
57
- <p class="question-text">3. भूगोल के इस प्रश्न का उत्तर क्या है?</p>
58
  <div class="options-grid">
59
  <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div>
60
  <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div>
61
  </div>
62
  </div>
63
-
64
  </div>
65
- </body>
66
  </html>
67
  """
68
 
69
- # Create the Gradio web interface
70
  iface = gr.Interface(
71
  fn=generate_pdf,
72
  inputs=gr.Code(value=example_html, language="html", label="HTML Input"),
73
  outputs=gr.File(label="Download PDF"),
74
- title="MCQ PDF Generator",
75
- description="Use the correct HTML structure: Place all headings and questions inside a single parent <div class='content-wrapper'>.",
76
  allow_flagging="never"
77
  )
78
 
79
- # Launch the app
80
  iface.launch()
 
1
  import gradio as gr
2
  from weasyprint import HTML, CSS
 
 
 
3
 
4
  def generate_pdf(html_content):
5
  """
6
+ Takes HTML content and generates a PDF using a self-contained CSS file.
7
  """
8
+ # We let WeasyPrint handle the CSS file directly.
9
+ # The font is embedded in the CSS, so no other paths are needed.
10
+ html = HTML(string=html_content)
 
 
 
11
  pdf_bytes = html.write_pdf(stylesheets=[CSS('style.css')])
12
+
13
+ output_filename = "final_document.pdf"
 
14
  with open(output_filename, "wb") as f:
15
  f.write(pdf_bytes)
16
+
17
  return output_filename
18
 
 
19
  example_html = """
20
  <!DOCTYPE html>
21
  <html>
22
+ <head><title>MCQ Document</title></head>
 
 
23
  <body>
24
  <div class="content-wrapper">
 
25
  <h2>अध्याय 1: इतिहास</h2>
 
26
  <div class="mcq-item">
27
+ <p class="question-text">1. इस अंतिम प्रयास से बनया प्रश्न।</p>
28
  <div class="options-grid">
29
  <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div>
30
  <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div>
31
  </div>
32
  </div>
 
 
 
 
 
 
 
 
 
33
  <h2>अध्याय 2: भूगोल</h2>
 
34
  <div class="mcq-item">
35
+ <p class="question-text">2. भूगोल के इस प्रश्न का उत्तर क्या है?</p>
36
  <div class="options-grid">
37
  <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div>
38
  <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div>
39
  </div>
40
  </div>
 
41
  </div>
42
+ </body>
43
  </html>
44
  """
45
 
 
46
  iface = gr.Interface(
47
  fn=generate_pdf,
48
  inputs=gr.Code(value=example_html, language="html", label="HTML Input"),
49
  outputs=gr.File(label="Download PDF"),
50
+ title="MCQ PDF Generator (Built-in Font)",
51
+ description="This version has the Devanagari font embedded directly. No separate font file is needed.",
52
  allow_flagging="never"
53
  )
54
 
 
55
  iface.launch()