Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ st.title("Pakistani Constitution Q&A App")
|
|
8 |
st.write("Upload the Pakistani Constitution PDF, explore sections, and ask questions.")
|
9 |
|
10 |
# Initialize the Groq API client
|
11 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Read
|
12 |
|
13 |
if not GROQ_API_KEY:
|
14 |
st.error("Groq API key not found. Please set the 'GROQ_API_KEY' environment variable in Hugging Face Spaces settings.")
|
@@ -26,13 +26,14 @@ sections = {
|
|
26 |
}
|
27 |
|
28 |
# Helper function to extract text from PDF
|
29 |
-
def extract_text_from_pdf(pdf_file, start_page, end_page):
|
|
|
30 |
try:
|
31 |
reader = PdfReader(pdf_file)
|
32 |
text = ""
|
33 |
for page in range(start_page, end_page + 1):
|
34 |
text += reader.pages[page].extract_text()
|
35 |
-
return text.strip()
|
36 |
except Exception as e:
|
37 |
st.error(f"Error extracting text from PDF: {e}")
|
38 |
return ""
|
@@ -53,17 +54,19 @@ if uploaded_file:
|
|
53 |
question = st.text_input("Ask a question about this section:")
|
54 |
|
55 |
if question:
|
56 |
-
# Interact with the Groq API
|
57 |
try:
|
58 |
st.info("Querying Groq API...")
|
|
|
59 |
chat_completion = client.chat.completions.create(
|
60 |
messages=[
|
61 |
{
|
62 |
"role": "user",
|
63 |
-
"content": f"Based on this section of the Pakistani Constitution: {section_text}\nQuestion: {question}",
|
64 |
}
|
65 |
],
|
66 |
model="llama-3.3-70b-versatile",
|
|
|
|
|
67 |
)
|
68 |
|
69 |
answer = chat_completion.choices[0].message.content
|
@@ -72,3 +75,82 @@ if uploaded_file:
|
|
72 |
st.error(f"Error communicating with Groq API: {e}")
|
73 |
else:
|
74 |
st.warning("Please upload a PDF file to proceed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
st.write("Upload the Pakistani Constitution PDF, explore sections, and ask questions.")
|
9 |
|
10 |
# Initialize the Groq API client
|
11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Read API key from environment variables
|
12 |
|
13 |
if not GROQ_API_KEY:
|
14 |
st.error("Groq API key not found. Please set the 'GROQ_API_KEY' environment variable in Hugging Face Spaces settings.")
|
|
|
26 |
}
|
27 |
|
28 |
# Helper function to extract text from PDF
|
29 |
+
def extract_text_from_pdf(pdf_file, start_page, end_page, max_chars=4000):
|
30 |
+
"""Extracts text from a specific range of pages in the PDF and limits text length."""
|
31 |
try:
|
32 |
reader = PdfReader(pdf_file)
|
33 |
text = ""
|
34 |
for page in range(start_page, end_page + 1):
|
35 |
text += reader.pages[page].extract_text()
|
36 |
+
return text[:max_chars].strip() # Limit text to prevent token overflow
|
37 |
except Exception as e:
|
38 |
st.error(f"Error extracting text from PDF: {e}")
|
39 |
return ""
|
|
|
54 |
question = st.text_input("Ask a question about this section:")
|
55 |
|
56 |
if question:
|
|
|
57 |
try:
|
58 |
st.info("Querying Groq API...")
|
59 |
+
|
60 |
chat_completion = client.chat.completions.create(
|
61 |
messages=[
|
62 |
{
|
63 |
"role": "user",
|
64 |
+
"content": f"Based on this section of the Pakistani Constitution: {section_text}\n\nQuestion: {question}",
|
65 |
}
|
66 |
],
|
67 |
model="llama-3.3-70b-versatile",
|
68 |
+
max_tokens=500, # Limiting output tokens
|
69 |
+
temperature=0.7 # Adjust for more/less randomness
|
70 |
)
|
71 |
|
72 |
answer = chat_completion.choices[0].message.content
|
|
|
75 |
st.error(f"Error communicating with Groq API: {e}")
|
76 |
else:
|
77 |
st.warning("Please upload a PDF file to proceed.")
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
# import os
|
84 |
+
# import streamlit as st
|
85 |
+
# from PyPDF2 import PdfReader
|
86 |
+
# from groq import Groq
|
87 |
+
|
88 |
+
# # Set up the Streamlit app
|
89 |
+
# st.title("Pakistani Constitution Q&A App")
|
90 |
+
# st.write("Upload the Pakistani Constitution PDF, explore sections, and ask questions.")
|
91 |
+
|
92 |
+
# # Initialize the Groq API client
|
93 |
+
# GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Read the API key from environment variables
|
94 |
+
|
95 |
+
# if not GROQ_API_KEY:
|
96 |
+
# st.error("Groq API key not found. Please set the 'GROQ_API_KEY' environment variable in Hugging Face Spaces settings.")
|
97 |
+
# else:
|
98 |
+
# client = Groq(api_key=GROQ_API_KEY)
|
99 |
+
|
100 |
+
# # Upload PDF
|
101 |
+
# uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
102 |
+
|
103 |
+
# # Predefined sections of the Constitution
|
104 |
+
# sections = {
|
105 |
+
# "Preamble": (0, 1),
|
106 |
+
# "Fundamental Rights": (2, 10),
|
107 |
+
# # Add more sections with their page ranges here
|
108 |
+
# }
|
109 |
+
|
110 |
+
# # Helper function to extract text from PDF
|
111 |
+
# def extract_text_from_pdf(pdf_file, start_page, end_page):
|
112 |
+
# try:
|
113 |
+
# reader = PdfReader(pdf_file)
|
114 |
+
# text = ""
|
115 |
+
# for page in range(start_page, end_page + 1):
|
116 |
+
# text += reader.pages[page].extract_text()
|
117 |
+
# return text.strip()
|
118 |
+
# except Exception as e:
|
119 |
+
# st.error(f"Error extracting text from PDF: {e}")
|
120 |
+
# return ""
|
121 |
+
|
122 |
+
# # Section selection and Q&A functionality
|
123 |
+
# if uploaded_file:
|
124 |
+
# st.success("PDF uploaded successfully.")
|
125 |
+
# selected_section = st.selectbox("Select a Section", list(sections.keys()))
|
126 |
+
|
127 |
+
# if selected_section:
|
128 |
+
# start_page, end_page = sections[selected_section]
|
129 |
+
# section_text = extract_text_from_pdf(uploaded_file, start_page, end_page)
|
130 |
+
|
131 |
+
# if section_text:
|
132 |
+
# st.text_area("Selected Section Text", section_text, height=300)
|
133 |
+
|
134 |
+
# # Question input
|
135 |
+
# question = st.text_input("Ask a question about this section:")
|
136 |
+
|
137 |
+
# if question:
|
138 |
+
# # Interact with the Groq API
|
139 |
+
# try:
|
140 |
+
# st.info("Querying Groq API...")
|
141 |
+
# chat_completion = client.chat.completions.create(
|
142 |
+
# messages=[
|
143 |
+
# {
|
144 |
+
# "role": "user",
|
145 |
+
# "content": f"Based on this section of the Pakistani Constitution: {section_text}\nQuestion: {question}",
|
146 |
+
# }
|
147 |
+
# ],
|
148 |
+
# model="llama-3.3-70b-versatile",
|
149 |
+
# )
|
150 |
+
|
151 |
+
# answer = chat_completion.choices[0].message.content
|
152 |
+
# st.success(f"Answer: {answer}")
|
153 |
+
# except Exception as e:
|
154 |
+
# st.error(f"Error communicating with Groq API: {e}")
|
155 |
+
# else:
|
156 |
+
# st.warning("Please upload a PDF file to proceed.")
|