engrjamalakram's picture
Upload app.py
33a5c17 verified
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()