omarkashif commited on
Commit
21bbf02
·
verified ·
1 Parent(s): 7a65571

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -22
app.py CHANGED
@@ -103,24 +103,8 @@ def pinecone_search(queries: List[str], top_k: int = 10, max_chars: int = 10000)
103
  break
104
  return "\n".join(context_parts), citations
105
 
106
- # def markdown_to_docx(md_text: str) -> str:
107
- # """
108
- # Convert Markdown text into a temporary .docx file and return its path.
109
- # """
110
- # temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".docx")
111
- # doc = Document()
112
- # for line in md_text.split("\n"):
113
- # if line.startswith("## "):
114
- # doc.add_heading(line[3:], level=2)
115
- # elif line.startswith("# "):
116
- # doc.add_heading(line[2:], level=1)
117
- # else:
118
- # doc.add_paragraph(line)
119
- # doc.save(temp_file.name)
120
- # return temp_file.name
121
-
122
- def markdown_to_docx(md_text: str) -> BytesIO:
123
- """Convert Markdown text into Word DOCX with proper formatting."""
124
  html = markdown.markdown(md_text)
125
  soup = BeautifulSoup(html, "html.parser")
126
 
@@ -137,10 +121,33 @@ def markdown_to_docx(md_text: str) -> BytesIO:
137
  elif el.name == "li":
138
  doc.add_paragraph(f"• {el.get_text()}")
139
 
140
- buf = BytesIO()
141
- doc.save(buf)
142
- buf.seek(0)
143
- return buf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
 
146
  # ----------------- MAIN FUNCTION -----------------
@@ -212,6 +219,7 @@ def generate_legal_draft(case_text, uploaded_file=None, add_citations=True):
212
  return gr.update(value=draft_md), markdown_to_docx(draft_md)
213
 
214
 
 
215
  # ----------------- GRADIO INTERFACE -----------------
216
  with gr.Blocks() as demo:
217
  gr.Markdown("## ⚖️ AI Legal Draft Generator\nUpload a DOCX/PDF/TXT template and enter case details. Get a court-ready draft.")
 
103
  break
104
  return "\n".join(context_parts), citations
105
 
106
+ def markdown_to_docx(md_text: str) -> str:
107
+ """Convert Markdown text into a Word DOCX and return a file path."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  html = markdown.markdown(md_text)
109
  soup = BeautifulSoup(html, "html.parser")
110
 
 
121
  elif el.name == "li":
122
  doc.add_paragraph(f"• {el.get_text()}")
123
 
124
+ # Save into a temporary file
125
+ tmp_path = os.path.join(tempfile.gettempdir(), "draft.docx")
126
+ doc.save(tmp_path)
127
+ return tmp_path
128
+
129
+ # def markdown_to_docx(md_text: str) -> BytesIO:
130
+ # """Convert Markdown text into Word DOCX with proper formatting."""
131
+ # html = markdown.markdown(md_text)
132
+ # soup = BeautifulSoup(html, "html.parser")
133
+
134
+ # doc = Document()
135
+ # for el in soup.descendants:
136
+ # if el.name == "h1":
137
+ # doc.add_heading(el.get_text(), level=1)
138
+ # elif el.name == "h2":
139
+ # doc.add_heading(el.get_text(), level=2)
140
+ # elif el.name == "h3":
141
+ # doc.add_heading(el.get_text(), level=3)
142
+ # elif el.name == "p":
143
+ # doc.add_paragraph(el.get_text())
144
+ # elif el.name == "li":
145
+ # doc.add_paragraph(f"• {el.get_text()}")
146
+
147
+ # buf = BytesIO()
148
+ # doc.save(buf)
149
+ # buf.seek(0)
150
+ # return buf
151
 
152
 
153
  # ----------------- MAIN FUNCTION -----------------
 
219
  return gr.update(value=draft_md), markdown_to_docx(draft_md)
220
 
221
 
222
+
223
  # ----------------- GRADIO INTERFACE -----------------
224
  with gr.Blocks() as demo:
225
  gr.Markdown("## ⚖️ AI Legal Draft Generator\nUpload a DOCX/PDF/TXT template and enter case details. Get a court-ready draft.")