File size: 1,588 Bytes
6256d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Install dependencies
# pip install streamlit PyPDF2 groq

import streamlit as st
from PyPDF2 import PdfReader
import os
from groq import Groq

# Set the API key
os.environ["GROQ_API_KEY"] = "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"

# Initialize Groq client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# Streamlit App
st.title("Pakistani Constitution Q&A App")
st.write("Upload the PDF of the Pakistani Constitution and ask questions about it.")

# Upload PDF
uploaded_file = st.file_uploader("Upload PDF", type="pdf")
if uploaded_file:
    # Extract text from the PDF
    pdf_reader = PdfReader(uploaded_file)
    constitution_text = ""
    for page in pdf_reader.pages:
        constitution_text += page.extract_text()

    st.success("PDF Uploaded and Processed Successfully!")
    
    # Display a text input box for the question
    user_question = st.text_input("Ask a question about the Constitution:")
    
    if user_question:
        st.write("Processing your question...")
        
        # Send the question to Groq API
        try:
            chat_completion = client.chat.completions.create(
                messages=[
                    {"role": "user", "content": f"Context: {constitution_text}\nQuestion: {user_question}"}
                ],
                model="llama3-8b-8192",
            )
            # Display the answer
            answer = chat_completion.choices[0].message.content
            st.write("### Answer:")
            st.write(answer)
        except Exception as e:
            st.error(f"An error occurred: {e}")