omarkashif commited on
Commit
81287a5
·
verified ·
1 Parent(s): 072e1c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -7,6 +7,7 @@ from docx import Document
7
  from sentence_transformers import SentenceTransformer
8
  from pinecone import Pinecone
9
  import openai
 
10
  import json
11
  import re
12
 
@@ -98,7 +99,11 @@ def pinecone_search(queries: List[str], top_k: int = 10, max_chars: int = 10000)
98
  break
99
  return "\n".join(context_parts), citations
100
 
101
- def markdown_to_docx(md_text: str) -> BytesIO:
 
 
 
 
102
  doc = Document()
103
  for line in md_text.split("\n"):
104
  if line.startswith("## "):
@@ -107,10 +112,8 @@ def markdown_to_docx(md_text: str) -> BytesIO:
107
  doc.add_heading(line[2:], level=1)
108
  else:
109
  doc.add_paragraph(line)
110
- buf = BytesIO()
111
- doc.save(buf)
112
- buf.seek(0)
113
- return buf
114
 
115
  # ----------------- MAIN FUNCTION -----------------
116
  def generate_legal_draft(case_text, uploaded_file=None, add_citations=True):
@@ -177,10 +180,11 @@ Instructions
177
  for i, c in enumerate(citations, 1):
178
  draft_md += f"{i}. {c['source']} (score: {c['score']:.3f})\n"
179
 
180
- return draft_md, markdown_to_docx(draft_md)
 
181
 
182
  # ----------------- GRADIO INTERFACE -----------------
183
- with gr.Blocks() as iface:
184
  gr.Markdown("## ⚖️ AI Legal Draft Generator\nUpload a DOCX/PDF/TXT template and enter case details. Get a court-ready draft.")
185
 
186
  case_text = gr.Textbox(label="Case Details", lines=10, placeholder="Enter client and case info...")
@@ -188,7 +192,7 @@ with gr.Blocks() as iface:
188
  add_citations = gr.Checkbox(label="Append citations", value=True)
189
 
190
  draft_output = gr.Markdown(label="Draft Output")
191
- download_btn = gr.DownloadButton(label="⬇️ Download Word", file_name="legal_draft.docx")
192
 
193
  btn = gr.Button("Generate Draft")
194
  btn.click(
@@ -197,5 +201,6 @@ with gr.Blocks() as iface:
197
  outputs=[draft_output, download_btn],
198
  )
199
 
 
200
  if __name__ == "__main__":
201
- iface.launch()
 
7
  from sentence_transformers import SentenceTransformer
8
  from pinecone import Pinecone
9
  import openai
10
+ import tempfile
11
  import json
12
  import re
13
 
 
99
  break
100
  return "\n".join(context_parts), citations
101
 
102
+ def markdown_to_docx(md_text: str) -> str:
103
+ """
104
+ Convert Markdown text into a temporary .docx file and return its path.
105
+ """
106
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".docx")
107
  doc = Document()
108
  for line in md_text.split("\n"):
109
  if line.startswith("## "):
 
112
  doc.add_heading(line[2:], level=1)
113
  else:
114
  doc.add_paragraph(line)
115
+ doc.save(temp_file.name)
116
+ return temp_file.name
 
 
117
 
118
  # ----------------- MAIN FUNCTION -----------------
119
  def generate_legal_draft(case_text, uploaded_file=None, add_citations=True):
 
180
  for i, c in enumerate(citations, 1):
181
  draft_md += f"{i}. {c['source']} (score: {c['score']:.3f})\n"
182
 
183
+ docx_path = markdown_to_docx(draft_md)
184
+ return draft_md, docx_path
185
 
186
  # ----------------- GRADIO INTERFACE -----------------
187
+ with gr.Blocks() as demo:
188
  gr.Markdown("## ⚖️ AI Legal Draft Generator\nUpload a DOCX/PDF/TXT template and enter case details. Get a court-ready draft.")
189
 
190
  case_text = gr.Textbox(label="Case Details", lines=10, placeholder="Enter client and case info...")
 
192
  add_citations = gr.Checkbox(label="Append citations", value=True)
193
 
194
  draft_output = gr.Markdown(label="Draft Output")
195
+ download_btn = gr.DownloadButton(label="⬇️ Download Word")
196
 
197
  btn = gr.Button("Generate Draft")
198
  btn.click(
 
201
  outputs=[draft_output, download_btn],
202
  )
203
 
204
+ # ----------------- RUN -----------------
205
  if __name__ == "__main__":
206
+ demo.launch()