Spaces:
Sleeping
Sleeping
Last commit not found
# 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}") | |