omarkashif commited on
Commit
072e1c8
·
verified ·
1 Parent(s): 940f9bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -9,7 +9,6 @@ from pinecone import Pinecone
9
  import openai
10
  import json
11
  import re
12
- import markdown
13
 
14
  # ----------------- CONFIG -----------------
15
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
@@ -25,6 +24,8 @@ embedder = SentenceTransformer("all-mpnet-base-v2")
25
 
26
  # ----------------- HELPERS -----------------
27
  def load_reference_text(uploaded_file) -> str:
 
 
28
  if uploaded_file.name.lower().endswith(".docx"):
29
  doc = Document(uploaded_file)
30
  return "\n".join(p.text for p in doc.paragraphs)
@@ -38,8 +39,7 @@ def load_reference_text(uploaded_file) -> str:
38
  return text
39
  elif uploaded_file.name.lower().endswith(".txt"):
40
  return uploaded_file.read().decode("utf-8", errors="ignore")
41
- else:
42
- return ""
43
 
44
  def parse_json_safe(raw_text: str, fallback: str) -> List[str]:
45
  try:
@@ -54,7 +54,7 @@ def build_queries_with_llm(user_text: str, max_queries: int = 15) -> List[str]:
54
  system_prompt = (
55
  "You are a legal research assistant. "
56
  "A new petition needs to be drafted using the following client/case description. "
57
- "Devise 5-6 or more concise queries that will be helpful to retrieve relevant information "
58
  "from a knowledge base containing the Constitution of Pakistan, Punjab case law, "
59
  "and FBR tax ordinances. "
60
  "Return ONLY a JSON array of strings, no extra text."
@@ -73,7 +73,7 @@ def build_queries_with_llm(user_text: str, max_queries: int = 15) -> List[str]:
73
  )
74
  raw = resp.choices[0].message.content.strip()
75
  queries = parse_json_safe(raw, user_text)
76
- except Exception as e:
77
  queries = [user_text[:512]]
78
  return queries[:max_queries]
79
 
@@ -114,7 +114,7 @@ def markdown_to_docx(md_text: str) -> BytesIO:
114
 
115
  # ----------------- MAIN FUNCTION -----------------
116
  def generate_legal_draft(case_text, uploaded_file=None, add_citations=True):
117
- ref_text = load_reference_text(uploaded_file) if uploaded_file else ""
118
 
119
  queries = build_queries_with_llm(case_text)
120
  context_text, citations = pinecone_search(queries, top_k=10)
@@ -157,6 +157,7 @@ Instructions
157
  **Reference Template (format+headings+structure+tone+language):**
158
  {(ref_text[:50000] + '...') if ref_text else '(none)'}
159
  """
 
160
  try:
161
  resp = openai_client.chat.completions.create(
162
  model="gpt-4o-mini",
@@ -176,23 +177,25 @@ Instructions
176
  for i, c in enumerate(citations, 1):
177
  draft_md += f"{i}. {c['source']} (score: {c['score']:.3f})\n"
178
 
179
- return draft_md, (markdown_to_docx(draft_md), "legal_draft.docx")
180
 
181
  # ----------------- GRADIO INTERFACE -----------------
182
- iface = gr.Interface(
183
- fn=generate_legal_draft,
184
- inputs=[
185
- gr.Textbox(label="Case Details", lines=10, placeholder="Enter client and case info..."),
186
- gr.File(label="Reference Template (DOCX/PDF/TXT)", file_types=[".docx",".pdf",".txt"]),
187
- gr.Checkbox(label="Append citations", value=True)
188
- ],
189
- outputs=[
190
- gr.Textbox(label="Draft Output", lines=30),
191
- gr.File(label="Download Word")
192
- ],
193
- title="⚖️ AI Legal Draft Generator",
194
- description="Upload a DOCX/PDF/TXT reference template, enter case details, and generate a court-ready legal draft in Markdown and Word."
195
- )
 
 
196
 
197
  if __name__ == "__main__":
198
  iface.launch()
 
9
  import openai
10
  import json
11
  import re
 
12
 
13
  # ----------------- CONFIG -----------------
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
24
 
25
  # ----------------- HELPERS -----------------
26
  def load_reference_text(uploaded_file) -> str:
27
+ if not uploaded_file:
28
+ return ""
29
  if uploaded_file.name.lower().endswith(".docx"):
30
  doc = Document(uploaded_file)
31
  return "\n".join(p.text for p in doc.paragraphs)
 
39
  return text
40
  elif uploaded_file.name.lower().endswith(".txt"):
41
  return uploaded_file.read().decode("utf-8", errors="ignore")
42
+ return ""
 
43
 
44
  def parse_json_safe(raw_text: str, fallback: str) -> List[str]:
45
  try:
 
54
  system_prompt = (
55
  "You are a legal research assistant. "
56
  "A new petition needs to be drafted using the following client/case description. "
57
+ "Devise 56 or more concise queries that will be helpful to retrieve relevant information "
58
  "from a knowledge base containing the Constitution of Pakistan, Punjab case law, "
59
  "and FBR tax ordinances. "
60
  "Return ONLY a JSON array of strings, no extra text."
 
73
  )
74
  raw = resp.choices[0].message.content.strip()
75
  queries = parse_json_safe(raw, user_text)
76
+ except Exception:
77
  queries = [user_text[:512]]
78
  return queries[:max_queries]
79
 
 
114
 
115
  # ----------------- MAIN FUNCTION -----------------
116
  def generate_legal_draft(case_text, uploaded_file=None, add_citations=True):
117
+ ref_text = load_reference_text(uploaded_file)
118
 
119
  queries = build_queries_with_llm(case_text)
120
  context_text, citations = pinecone_search(queries, top_k=10)
 
157
  **Reference Template (format+headings+structure+tone+language):**
158
  {(ref_text[:50000] + '...') if ref_text else '(none)'}
159
  """
160
+
161
  try:
162
  resp = openai_client.chat.completions.create(
163
  model="gpt-4o-mini",
 
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...")
187
+ uploaded_file = gr.File(label="Reference Template (DOCX/PDF/TXT)", file_types=[".docx",".pdf",".txt"])
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(
195
+ generate_legal_draft,
196
+ inputs=[case_text, uploaded_file, add_citations],
197
+ outputs=[draft_output, download_btn],
198
+ )
199
 
200
  if __name__ == "__main__":
201
  iface.launch()