Turbiling's picture
Update app.py
a7a0548 verified
Raw
History Blame Contribute Delete
3.05 kB
import gradio as gr
import fitz # PyMuPDF
import os
from groq import Groq
# === Load the uploaded PDF ===
PDF_PATH = "MANUAL OF SECRETARIAT INSTRUCTIONS (Updated 2023)_1_0.pdf"
if not os.path.exists(PDF_PATH):
raise FileNotFoundError(f"❌ File not found: {PDF_PATH}")
# === Groq Client ===
client = Groq(api_key=os.environ["GROQ_API_KEY"])
# === Generate Answer ===
def generate_answer(question):
prompt = f"""You are a helpful assistant. Read the document titled "MANUAL OF SECRETARIAT INSTRUCTIONS" and answer this question:
Question: {question}
Answer:"""
chat_completion = client.chat.completions.create(
model="llama3-8b-8192",
messages=[{"role": "user", "content": prompt}]
)
return chat_completion.choices[0].message.content.strip()
# === Suggested Questions ===
suggested_questions = [
"What are the primary responsibilities of a section officer?",
"How is correspondence maintained between different departments?",
"What are the procedures for maintaining confidentiality?",
"How is a case file processed in the Secretariat?",
"What are the duties of a superintendent as per the manual?",
"Explain the protocol for submitting summaries to the Minister."
]
# === Gradio UI ===
with gr.Blocks(theme=gr.themes.Soft()) as demo:
dark_mode = gr.State(False)
# === HEADER ===
with gr.Row():
gr.Markdown("<h1 style='text-align:center; color:#2c3e50;'>πŸ“˜ Secretariat Instructions QA App</h1>")
mode_button = gr.Button("πŸŒ™ Dark Mode", elem_id="mode-toggle")
with gr.Row():
# === LEFT SIDEBAR ===
with gr.Column(scale=1):
gr.Markdown("### 🧠 Suggested Questions")
for q in suggested_questions:
gr.Markdown(f"- {q}")
# === CENTER: Main QA ===
with gr.Column(scale=2):
gr.Markdown("### ❓ Ask Your Own Question")
question_box = gr.Textbox(placeholder="Type your question here...", label="Your Question")
answer_button = gr.Button("Generate Answer", variant="primary")
answer_output = gr.Textbox(label="AI Answer", lines=10)
# === RIGHT SIDEBAR ===
with gr.Column(scale=1):
gr.Markdown("### ℹ️ About")
gr.Markdown("Developed by ❀️ **Najaf Ali Sharqi** ❀️")
gr.Markdown("Powered by **Groq API** and **LLaMA3 Model**.")
# === JS for Dark Mode Toggle ===
gr.HTML("""
<script>
let isDark = false;
document.getElementById('mode-toggle').onclick = () => {
isDark = !isDark;
document.body.style.backgroundColor = isDark ? '#1e1e1e' : 'white';
document.body.style.color = isDark ? '#e0e0e0' : 'black';
document.getElementById('mode-toggle').innerText = isDark ? 'β˜€οΈ Light Mode' : 'πŸŒ™ Dark Mode';
};
</script>
""")
# === Bind Answer Function ===
answer_button.click(fn=generate_answer, inputs=question_box, outputs=answer_output)
# === Launch App ===
demo.launch()