File size: 5,836 Bytes
33a5c17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import whisper
import os
from groq import Groq
import tempfile
from gtts import gTTS
import fitz  # PyMuPDF

# Load Whisper model
whisper_model = whisper.load_model("base")

# Set Groq API Key
client = Groq(api_key="gsk_ngSXMLqBqFLEZAKyb3FY6haxWG05YrinF2YxvTeGsZQf")

# Urdu legal Q&A (text input)
def urdu_chat(question):
    response = client.chat.completions.create(
        model="llama3-70b-8192",
        messages=[
            {"role": "system", "content": "You are a helpful AI assistant that always replies in Urdu simply for legal queries."},
            {"role": "user", "content": question}
        ]
    )
    return response.choices[0].message.content

# Urdu legal Q&A (voice input)
def urdu_chat_voice(audio_file_path):
    result = whisper_model.transcribe(audio_file_path, language="ur")
    question = result["text"]

    response = client.chat.completions.create(
        model="llama3-70b-8192",
        messages=[
            {"role": "system", "content": "You are a helpful AI assistant that always replies in Urdu simply for legal queries."},
            {"role": "user", "content": question}
        ]
    )
    answer = response.choices[0].message.content

    tts = gTTS(answer, lang='ur')
    audio_path = tempfile.mktemp(suffix=".mp3")
    tts.save(audio_path)

    return question, answer, audio_path

# Analyze Urdu Legal PDF and summarize as text file output
def analyze_pdf(pdf_file):
    try:
        doc = fitz.open(pdf_file)
        full_text = ""
        for page in doc:
            full_text += page.get_text()

        response = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[
                {"role": "system", "content": "آپ ایک ماہر قانونی AI ہیں۔ آپ کو دیے گئے اردو قانونی دستاویز کا تجزیہ ہمیشہ اردو زبان میں کرنا ہے۔ انگریزی زبان میں ہرگز جواب نہ دیں۔ آپ کیس کی تفصیل، جج کا نام، درخواست گزار، فیصلہ، اور متعلقہ پاکستانی قوانین کی دفعات اردو میں بیان کریں۔"},
                {"role": "user", "content": full_text}
            ]
        )
        summary = response.choices[0].message.content

        summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8")
        summary_file.write(summary)
        summary_file.close()

        return summary, summary_file.name
    except Exception as e:
        return f"⚠️ خرابی: {str(e)}", None

# Interface
with gr.Blocks(title="⚖ اردو AI وکیل") as demo:

    # Autoplay intro audio with fallback button
    gr.HTML("""
    <audio id="introAudio" src="file/intro.mp3"></audio>
    <button id="playIntroBtn" style="display:none; font-size:16px; padding:10px; margin:10px auto; display:block; cursor:pointer;">
      اپنے وکیل کی ہدایات سننے کے لیے یہاں کلک کریں
    </button>
    <script>
    window.addEventListener('load', function() {
        const audio = document.getElementById("introAudio");
        const btn = document.getElementById("playIntroBtn");

        audio.play().catch((e) => {
            // Autoplay failed, show the button
            btn.style.display = "block";
        });

        btn.addEventListener('click', function() {
            audio.play();
            btn.style.display = "none";
        });
    });
    </script>
    """)

    gr.Markdown("### 🧑‍⚖ اردو میں قانونی سوالات پوچھیں", elem_classes="main-header")

    with gr.Tab("✍ ٹیکسٹ چیٹ"):
        with gr.Row():
            text_input = gr.Textbox(lines=2, placeholder="اپنا قانونی سوال یہاں لکھیں...", elem_classes="bold-text")
            text_output = gr.Textbox(label="جواب", elem_classes="bold-text")
        text_btn = gr.Button("جواب حاصل کریں")
        text_btn.click(fn=urdu_chat, inputs=text_input, outputs=text_output)

    with gr.Tab("🎙 آواز سے پوچھیں"):
        with gr.Row():
            audio_input = gr.Audio(type="filepath", label="🔊 اپنی آواز میں سوال ریکارڈ کریں یا اپلوڈ کریں")
        with gr.Row():
            transcribed = gr.Textbox(label="📝 سوال (آڈیو سے متن)", elem_classes="bold-text")
            ai_answer = gr.Textbox(label="🤖 AI جواب", elem_classes="bold-text")
        with gr.Row():
            audio_response = gr.Audio(type="filepath", label="🔊 آڈیو میں جواب سنیں")
        voice_btn = gr.Button("جواب سنیں")
        voice_btn.click(fn=urdu_chat_voice, inputs=audio_input, outputs=[transcribed, ai_answer, audio_response])

    with gr.Tab("📄 قانونی PDF تجزیہ"):
        with gr.Row():
            pdf_input = gr.File(file_types=[".pdf"], label="اپنا قانونی PDF اپلوڈ کریں")
        with gr.Row():
            pdf_output = gr.Textbox(label="⚖ AI قانونی تجزیہ", lines=10, elem_classes="bold-text")
        with gr.Row():
            download_button = gr.File(label="📥 سمری ٹیکسٹ ڈاؤنلوڈ کریں")
        pdf_btn = gr.Button("تجزیہ کریں")
        pdf_btn.click(fn=analyze_pdf, inputs=pdf_input, outputs=[pdf_output, download_button])

    # Custom CSS
    gr.HTML("""
    <style>
        body {
            background-color: #f2f2f2;
        }
        .main-header {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            direction: rtl;
            color: #2c3e50;
        }
        .bold-text textarea {
            font-weight: bold !important;
            font-size: 18px !important;
            direction: rtl;
        }
    </style>
    """)

demo.launch()