Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
import os
|
| 4 |
+
from groq import Groq
|
| 5 |
+
import tempfile
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
import fitz # PyMuPDF
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
+
# Load Whisper model
|
| 11 |
+
whisper_model = whisper.load_model("base")
|
| 12 |
+
|
| 13 |
+
# Set Groq API Key
|
| 14 |
+
client = Groq(api_key="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")
|
| 15 |
+
|
| 16 |
+
# Urdu legal Q&A (text input)
|
| 17 |
+
def urdu_chat(question):
|
| 18 |
+
response = client.chat.completions.create(
|
| 19 |
+
model="llama3-70b-8192",
|
| 20 |
+
messages=[
|
| 21 |
+
{"role": "system", "content": "You are a helpful AI assistant that always replies in Urdu simply for legal queries."},
|
| 22 |
+
{"role": "user", "content": question}
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
return response.choices[0].message.content
|
| 26 |
+
|
| 27 |
+
# Urdu legal Q&A (voice input)
|
| 28 |
+
def urdu_chat_voice(audio_file_path):
|
| 29 |
+
result = whisper_model.transcribe(audio_file_path, language="ur")
|
| 30 |
+
question = result["text"]
|
| 31 |
+
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="llama3-70b-8192",
|
| 34 |
+
messages=[
|
| 35 |
+
{"role": "system", "content": "You are a helpful AI assistant that always replies in Urdu simply for legal queries."},
|
| 36 |
+
{"role": "user", "content": question}
|
| 37 |
+
]
|
| 38 |
+
)
|
| 39 |
+
answer = response.choices[0].message.content
|
| 40 |
+
|
| 41 |
+
tts = gTTS(answer, lang='ur')
|
| 42 |
+
audio_path = tempfile.mktemp(suffix=".mp3")
|
| 43 |
+
tts.save(audio_path)
|
| 44 |
+
|
| 45 |
+
return question, answer, audio_path
|
| 46 |
+
|
| 47 |
+
# Analyze Urdu Legal PDF and summarize
|
| 48 |
+
def analyze_pdf(pdf_file):
|
| 49 |
+
try:
|
| 50 |
+
doc = fitz.open(pdf_file)
|
| 51 |
+
full_text = ""
|
| 52 |
+
for page in doc:
|
| 53 |
+
full_text += page.get_text()
|
| 54 |
+
|
| 55 |
+
response = client.chat.completions.create(
|
| 56 |
+
model="llama3-70b-8192",
|
| 57 |
+
messages=[
|
| 58 |
+
{"role": "system", "content": "آپ ایک ماہر قانونی AI ہیں۔ آپ کو دیے گئے اردو قانونی دستاویز کا تجزیہ ہمیشہ اردو زبان میں کرنا ہے۔ انگریزی زبان میں ہرگز جواب نہ دیں۔ آپ کیس کی تفصیل، جج کا نام، درخواست گزار، فیصلہ، اور متعلقہ پاکستانی قوانین کی دفعات اردو میں بیان کریں۔"},
|
| 59 |
+
{"role": "user", "content": full_text}
|
| 60 |
+
]
|
| 61 |
+
)
|
| 62 |
+
summary = response.choices[0].message.content
|
| 63 |
+
|
| 64 |
+
summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
| 65 |
+
with open(summary_file.name, "w", encoding="utf-8") as f:
|
| 66 |
+
f.write(summary)
|
| 67 |
+
|
| 68 |
+
return summary, summary_file.name
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"⚠️ خرابی: {str(e)}", None
|
| 71 |
+
|
| 72 |
+
# Encode intro audio to base64 for autoplay
|
| 73 |
+
def get_base64_audio_tag(audio_path):
|
| 74 |
+
with open(audio_path, "rb") as audio_file:
|
| 75 |
+
encoded_string = base64.b64encode(audio_file.read()).decode()
|
| 76 |
+
return f"""
|
| 77 |
+
<audio autoplay>
|
| 78 |
+
<source src="data:audio/mp3;base64,{encoded_string}" type="audio/mp3">
|
| 79 |
+
</audio>
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
# Load external CSS from style.css
|
| 83 |
+
with open("style.css", "r", encoding="utf-8") as f:
|
| 84 |
+
custom_css = f.read()
|
| 85 |
+
|
| 86 |
+
# Interface
|
| 87 |
+
with gr.Blocks(title="⚖ اردو AI وکیل", css=custom_css) as demo:
|
| 88 |
+
gr.HTML(get_base64_audio_tag("intro.mp3"))
|
| 89 |
+
gr.Markdown("### 🧑⚖ اردو میں قانونی سوالات پوچھیں", elem_classes="main-header")
|
| 90 |
+
|
| 91 |
+
with gr.Tab("✍ ٹیکسٹ چیٹ"):
|
| 92 |
+
with gr.Row():
|
| 93 |
+
text_input = gr.Textbox(lines=2, placeholder="اپنا قانونی سوال یہاں لکھیں...", elem_classes="bold-text")
|
| 94 |
+
text_output = gr.Textbox(label="جواب", elem_classes="bold-text")
|
| 95 |
+
text_btn = gr.Button("جواب حاصل کریں")
|
| 96 |
+
text_btn.click(fn=urdu_chat, inputs=text_input, outputs=text_output)
|
| 97 |
+
|
| 98 |
+
with gr.Tab("🎙 آواز سے پوچھیں"):
|
| 99 |
+
with gr.Row():
|
| 100 |
+
audio_input = gr.Audio(type="filepath", label="🔊 اپنی آواز میں سوال ریکارڈ کریں یا اپلوڈ کریں")
|
| 101 |
+
with gr.Row():
|
| 102 |
+
transcribed = gr.Textbox(label="📝 سوال (آڈیو سے متن)", elem_classes="bold-text")
|
| 103 |
+
ai_answer = gr.Textbox(label="🤖 AI جواب", elem_classes="bold-text")
|
| 104 |
+
with gr.Row():
|
| 105 |
+
audio_response = gr.Audio(type="filepath", label="🔊 آڈیو میں جواب سنیں")
|
| 106 |
+
voice_btn = gr.Button("جواب سنیں")
|
| 107 |
+
voice_btn.click(fn=urdu_chat_voice, inputs=audio_input, outputs=[transcribed, ai_answer, audio_response])
|
| 108 |
+
|
| 109 |
+
with gr.Tab("📄 قانونی PDF تجزیہ"):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
pdf_input = gr.File(file_types=[".pdf"], label="اپنا قانونی PDF اپلوڈ کریں")
|
| 112 |
+
with gr.Row():
|
| 113 |
+
pdf_output = gr.Textbox(label="⚖ AI قانونی تجزیہ", lines=10, elem_classes="bold-text")
|
| 114 |
+
with gr.Row():
|
| 115 |
+
download_button = gr.File(label="📥 سمری ٹیکسٹ ڈاؤنلوڈ کریں")
|
| 116 |
+
pdf_btn = gr.Button("تجزیہ کریں")
|
| 117 |
+
pdf_btn.click(fn=analyze_pdf, inputs=pdf_input, outputs=[pdf_output, download_button])
|
| 118 |
+
|
| 119 |
+
demo.launch()
|