Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install dependencies
|
2 |
+
# pip install streamlit PyPDF2 groq
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from PyPDF2 import PdfReader
|
6 |
+
import os
|
7 |
+
from groq import Groq
|
8 |
+
|
9 |
+
# Set the API key
|
10 |
+
os.environ["GROQ_API_KEY"] = "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"
|
11 |
+
|
12 |
+
# Initialize Groq client
|
13 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
14 |
+
|
15 |
+
# Streamlit App
|
16 |
+
st.title("Pakistani Constitution Q&A App")
|
17 |
+
st.write("Upload the PDF of the Pakistani Constitution and ask questions about it.")
|
18 |
+
|
19 |
+
# Upload PDF
|
20 |
+
uploaded_file = st.file_uploader("Upload PDF", type="pdf")
|
21 |
+
if uploaded_file:
|
22 |
+
# Extract text from the PDF
|
23 |
+
pdf_reader = PdfReader(uploaded_file)
|
24 |
+
constitution_text = ""
|
25 |
+
for page in pdf_reader.pages:
|
26 |
+
constitution_text += page.extract_text()
|
27 |
+
|
28 |
+
st.success("PDF Uploaded and Processed Successfully!")
|
29 |
+
|
30 |
+
# Display a text input box for the question
|
31 |
+
user_question = st.text_input("Ask a question about the Constitution:")
|
32 |
+
|
33 |
+
if user_question:
|
34 |
+
st.write("Processing your question...")
|
35 |
+
|
36 |
+
# Send the question to Groq API
|
37 |
+
try:
|
38 |
+
chat_completion = client.chat.completions.create(
|
39 |
+
messages=[
|
40 |
+
{"role": "user", "content": f"Context: {constitution_text}\nQuestion: {user_question}"}
|
41 |
+
],
|
42 |
+
model="llama3-8b-8192",
|
43 |
+
)
|
44 |
+
# Display the answer
|
45 |
+
answer = chat_completion.choices[0].message.content
|
46 |
+
st.write("### Answer:")
|
47 |
+
st.write(answer)
|
48 |
+
except Exception as e:
|
49 |
+
st.error(f"An error occurred: {e}")
|