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("

📘 Secretariat Instructions QA App

") 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(""" """) # === Bind Answer Function === answer_button.click(fn=generate_answer, inputs=question_box, outputs=answer_output) # === Launch App === demo.launch()