File size: 14,614 Bytes
f3e8218
23aa6e1
f3e8218
 
 
23aa6e1
f3e8218
23aa6e1
 
f3e8218
23aa6e1
f3e8218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287f3d3
f3e8218
 
 
 
 
 
 
 
 
23aa6e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3e8218
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa6e1
f3e8218
 
 
 
 
 
 
 
 
 
 
 
824efec
9f2a45b
f3e8218
23aa6e1
f3e8218
 
 
9f2a45b
f3e8218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa6e1
f3e8218
 
 
 
 
 
23aa6e1
f3e8218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa6e1
 
 
 
 
f3e8218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa6e1
f3e8218
 
 
 
 
 
 
 
23aa6e1
f3e8218
 
 
 
 
 
 
 
 
 
 
 
23aa6e1
f3e8218
 
 
287f3d3
f3e8218
 
 
 
 
 
23aa6e1
96d07b6
 
f3e8218
 
 
 
 
f0e5fa6
f3e8218
 
 
 
 
 
23aa6e1
f3e8218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2294fe
f3e8218
 
 
 
 
 
 
96d07b6
f3e8218
 
 
 
 
 
287f3d3
f3e8218
 
23aa6e1
 
 
 
f3e8218
23aa6e1
f3e8218
 
23aa6e1
 
 
 
 
 
 
 
f3e8218
 
 
23aa6e1
f3e8218
 
 
 
 
 
 
 
 
 
 
287f3d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import gradio as gr
from groq import Groq
import requests
import numpy as np
from typing import Tuple, Dict
import os

# Initialize Groq client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# Prompts for Groq
TRANSLATION_PROMPT = """You are an expert policy document translator specializing in converting complex policy language into clear, plain language that average citizens can easily understand.

**Your Task:**
Transform the following policy document into plain language while:
1. Preserving all critical information and legal requirements
2. Using simple, everyday words (avoid jargon unless absolutely necessary)
3. Breaking down complex sentences into shorter, clearer ones
4. Using active voice instead of passive voice
5. Explaining technical terms when they must be used
6. Organizing information logically with clear headings
7. Using bullet points or numbered lists for clarity when appropriate

**Guidelines:**
- Target reading level: 8th grade
- Sentence length: Aim for 15-20 words per sentence
- Use "you" and "your" to make it personal
- Replace legal jargon with everyday equivalents
- If a term must be kept, provide a brief explanation in parentheses

**Original Policy Document:**
{document}

**Additional Context (if provided):**
Jurisdiction: {jurisdiction}
Special terms to explain: {jargon}

**Please provide the plain language version below:**"""

IMPROVEMENT_PROMPT = """The previous plain language translation has a similarity score of {similarity:.2f} with the back-translated version, which indicates potential information loss or changes in meaning.

**Your Task:**
Improve the plain language translation to better preserve the original meaning while maintaining clarity. Focus on:
1. Ensuring all key facts and requirements are preserved
2. Maintaining simple language but being more precise
3. Checking that numbers, dates, and specific requirements match the original
4. Clarifying any ambiguous statements

**Original Policy Document:**
{document}

**Previous Plain Language Version:**
{plain_language}

**Issues Identified:**
- Similarity score is below threshold (target: 0.9, current: {similarity:.2f})
- This suggests potential meaning drift or information loss

**Additional Context:**
Jurisdiction: {jurisdiction}
Special terms to explain: {jargon}

**Please provide an improved plain language version:**"""


def chat_with_groq(prompt: str, use_tools: bool = False) -> str:
    """Chat with Groq API"""
    try:
        messages = [{"role": "user", "content": prompt}]
        
        kwargs = {
            "model": "groq/compound",
            "messages": messages,
            "temperature": 0.5,
            "max_completion_tokens": 8000,
            "top_p": 1,
            "stream": True,
            "stop": None,
        }
        
        if use_tools:
            kwargs["compound_custom"] = {
                "tools": {"enabled_tools": ["visit_website", "web_search"]}
            }
        
        completion = client.chat.completions.create(**kwargs)
        
        response = ""
        for chunk in completion:
            if chunk.choices[0].delta.content:
                response += chunk.choices[0].delta.content
        
        return response.strip()
    except Exception as e:
        raise Exception(f"Groq API error: {str(e)}")


def translate_text(text: str, source: str, target: str) -> str:
    """Translate text using LibreTranslate API"""
    try:
        response = requests.post(
            "https://jeff86-libretranslate.hf.space/translate",
            json={
                "q": text,
                "source": source,
                "target": target,
                "format": "text",
                "alternatives": 0,
                "api_key": ""
            },
            headers={"Content-Type": "application/json"},
            timeout=60
        )
        response.raise_for_status()
        result = response.json()
        return result.get("translatedText", "")
    except Exception as e:
        raise Exception(f"Translation error: {str(e)}")


def get_embedding(text: str) -> np.ndarray:
    """Get embedding using Qwen3-Embedding-0.6B API"""
    try:
        response = requests.post(
            "https://fahmiaziz-api-embedding.hf.space/api/v1/embeddings",
            json={"input": [text],"model": "qwen3-0.6b"},
            headers={"Content-Type": "application/json"},
            timeout=60
        )
        response.raise_for_status()
        result = response.json()
        return np.array(result.get("data", [])[0].get("embedding", []))
    except Exception as e:
        raise Exception(f"Embedding error: {str(e)}")


def cosine_similarity(vec1: np.ndarray, vec2: np.ndarray) -> float:
    """Calculate cosine similarity between two vectors"""
    if len(vec1) == 0 or len(vec2) == 0:
        return 0.0
    dot_product = np.dot(vec1, vec2)
    norm1 = np.linalg.norm(vec1)
    norm2 = np.linalg.norm(vec2)
    if norm1 == 0 or norm2 == 0:
        return 0.0
    return float(dot_product / (norm1 * norm2))


def process_document(
    policy_text: str,
    jurisdiction: str = "",
    jargon_terms: str = "",
    max_iterations: int = 3
) -> Tuple[str, str, float, str]:
    """
    Process policy document and translate to plain language with quality verification
    
    Returns: (plain_language_text, status_message, similarity_score, details)
    """
    if not policy_text.strip():
        return "", "⚠️ Please provide a policy document to translate.", 0.0, ""
    
    details = "### Processing Steps:\n\n"
    
    # Step 1: Initial translation
    details += "**1. Translating to plain language...**\n"
    prompt = TRANSLATION_PROMPT.format(
        document=policy_text,
        jurisdiction=jurisdiction if jurisdiction else "Not specified",
        jargon=jargon_terms if jargon_terms else "None specified"
    )
    
    try:
        plain_language = chat_with_groq(prompt)
        details += f"βœ“ Initial translation completed ({len(plain_language)} characters)\n\n"
    except Exception as e:
        return "", f"❌ Error during translation: {str(e)}", 0.0, details
    
    iteration = 0
    best_similarity = 0.0
    best_translation = plain_language
    
    while iteration < max_iterations:
        iteration += 1
        details += f"**Iteration {iteration}:**\n"
        
        # Step 2: Translate to Chinese
        details += "- Translating to Chinese for verification...\n"
        try:
            chinese_text = translate_text(plain_language, "en", "zh")
            details += f"  βœ“ Chinese translation: {len(chinese_text)} characters\n"
        except Exception as e:
            details += f"  ❌ Translation to Chinese failed: {str(e)}\n"
            break
        
        # Step 3: Translate back to English
        details += "- Translating back to English...\n"
        try:
            back_translated = translate_text(chinese_text, "zh", "en")
            details += f"  βœ“ Back-translation completed: {len(back_translated)} characters\n"
        except Exception as e:
            details += f"  ❌ Back-translation failed: {str(e)}\n"
            break
        
        # Step 4: Calculate similarity
        details += "- Calculating semantic similarity...\n"
        try:
            emb1 = get_embedding(plain_language)
            emb2 = get_embedding(back_translated)
            similarity = cosine_similarity(emb1, emb2)
            
            if similarity > best_similarity:
                best_similarity = similarity
                best_translation = plain_language
            
            details += f"  βœ“ Similarity score: **{similarity:.4f}**\n"
        except Exception as e:
            details += f"  ❌ Similarity calculation failed: {str(e)}\n"
            break
        
        # Check if similarity is acceptable
        if similarity >= 0.8:
            details += f"\nβœ… **Quality threshold met!** (similarity: {similarity:.4f} β‰₯ 0.8)\n"
            status = f"βœ… Translation successful! Similarity: {similarity:.4f}"
            return plain_language, status, similarity, details
        else:
            details += f"  ⚠️ Below threshold (target: 0.8, current: {similarity:.4f})\n"
            
            if iteration < max_iterations:
                # Step 5: Request improvement
                details += "- Requesting improved translation from Groq...\n"
                improvement_prompt = IMPROVEMENT_PROMPT.format(
                    document=policy_text,
                    plain_language=plain_language,
                    similarity=similarity,
                    jurisdiction=jurisdiction if jurisdiction else "Not specified",
                    jargon=jargon_terms if jargon_terms else "None specified"
                )
                try:
                    plain_language = chat_with_groq(improvement_prompt)
                    details += f"  βœ“ Improved translation generated\n\n"
                except Exception as e:
                    details += f"  ❌ Improvement failed: {str(e)}\n"
                    break
            else:
                details += f"\n⚠️ **Maximum iterations reached.** Best similarity: {best_similarity:.4f}\n"
    
    if best_similarity < 0.8:
        status = f"⚠️ Translation completed but similarity ({best_similarity:.4f}) is below threshold. Consider adding more context."
    else:
        status = f"βœ… Translation successful! Final similarity: {best_similarity:.4f}"
    
    return best_translation, status, best_similarity, details


# Gradio Interface
with gr.Blocks(title="Policy to Plain Language Translator") as demo:
    gr.Markdown("""
    # πŸ“œ Policy to Plain Language Translator
    
    Transform complex policy documents into clear, understandable language using AI-powered translation and quality verification.
    
    ### How it works:
    1. **Plain Language Translation**: Groq AI converts your policy document into simple, clear language
    2. **Quality Verification**: The text is translated using old fashion machine translation to Chinese and back to English. Qwen3 embeddings verify the meaning is preserved (target: β‰₯0.8 similarity) after dummy machine translations verify the plain language is easy to interpret
    3. **Iterative Improvement**: If quality is low, the AI refines the plain language automatically (up to 3 iterations)
    
    ### πŸ“‹ Best Practices:
    - **Use English policy documents** for best results
    - **Use Markdown format** ([Learn Markdown basics](https://www.markdownguide.org/basic-syntax/))
    - Provide jurisdiction and jargon terms for better context
    - Demo does not take long document, [notify us if you want paid demo](https://huggingface.co/spaces/npc0/policy-2-plain-language/discussions/new)
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            policy_input = gr.Textbox(
                label="Policy Document (English, Markdown format recommended)",
                placeholder="Paste your policy document here...\n\nExample:\n# Data Privacy Policy\n\n## Section 1: Data Collection\nThe organization shall collect personally identifiable information (PII) pursuant to applicable regulations...",
                lines=15,
                max_lines=20
            )
            
            with gr.Accordion("πŸ“ Optional: Additional Context", open=False):
                jurisdiction_input = gr.Textbox(
                    label="Jurisdiction (e.g., 'California, USA', 'EU', 'Ontario, Canada')",
                    placeholder="Specify the legal jurisdiction if relevant...",
                    lines=1
                )
                jargon_input = gr.Textbox(
                    label="Jargon Terms to Explain (comma-separated)",
                    placeholder="e.g., 'PII, GDPR, data controller, consent decree'",
                    lines=2
                )
            
            translate_btn = gr.Button("πŸ”„ Translate to Plain Language", variant="primary", size="lg")
            
        with gr.Column(scale=1):
            plain_output = gr.Textbox(
                label="Plain Language Version",
                lines=15,
                max_lines=20,
                buttons=["copy"]
            )
            status_output = gr.Textbox(
                label="Status",
                lines=2,
                interactive=False
            )
            similarity_output = gr.Number(
                label="Quality Score (0-1, target: β‰₯0.8)",
                precision=4,
                interactive=False
            )
    
    with gr.Accordion("πŸ” Processing Details", open=False):
        details_output = gr.Markdown()
        
    gr.Markdown("""
    ### πŸ’‘ Tips for Better Results:
    - **Markdown Format**: Structure your document with headers (`#`), bullet points (`-`), and paragraphs for better organization
    - **Context Matters**: Providing jurisdiction and jargon terms helps the AI understand legal and regional context
    - **Iteration**: The system automatically tries up to 3 times to improve translation quality if similarity is below 0.8
    - **Review Output**: Always review the output for accuracyβ€”AI is a powerful tool but human judgment is essential for legal documents
    
    ### πŸ“š Markdown Resources:
    - [Markdown Guide - Basic Syntax](https://www.markdownguide.org/basic-syntax/)
    - [Google's Markdown Style Guide](https://google.github.io/styleguide/docguide/style.html)
    - [Markdown Best Practices for Documentation](https://www.markdowntoolbox.com/blog/markdown-best-practices-for-documentation/)
    
    ### πŸ”§ Technical Details:
    - **Workflow**: Inspired by [Seattle Language Access Program](https://codeforamerica.org/news/government-gains-with-the-city-of-seattle/)
    - **AI Model**: Groq Compound (efficient language model)
    - **Translation**: LibreTranslate (en→zh→en for quality verification)
    - **Embeddings**: Qwen3-Embedding-0.6B (semantic similarity calculation)
    - **Quality Metric**: Cosine similarity between original and back-translated text
    
    ---
    
    **⚠️ Important Disclaimer**: This tool is a demo using AI models for translation and may not be accurate. Always have documents reviewed by qualified professionals for your real world use case.
    """)
    
    # Event handler
    translate_btn.click(
        fn=process_document,
        inputs=[policy_input, jurisdiction_input, jargon_input],
        outputs=[plain_output, status_output, similarity_output, details_output]
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft())