shah1zil commited on
Commit
74e8afc
·
verified ·
1 Parent(s): 54d56a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -1,12 +1,20 @@
1
  import os
2
  import streamlit as st
3
  from PyPDF2 import PdfReader
4
- import requests
5
 
6
  # Set up the Streamlit app
7
  st.title("Pakistani Constitution Q&A App")
8
  st.write("Upload the Pakistani Constitution PDF, explore sections, and ask questions.")
9
 
 
 
 
 
 
 
 
 
10
  # Upload PDF
11
  uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
12
 
@@ -17,12 +25,6 @@ sections = {
17
  # Add more sections with their page ranges here
18
  }
19
 
20
- # Groq API configuration
21
- GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Read the API key from environment variables
22
-
23
- if not GROQ_API_KEY:
24
- st.error("Groq API key not found. Please set the 'GROQ_API_KEY' environment variable in Hugging Face Spaces settings.")
25
-
26
  # Helper function to extract text from PDF
27
  def extract_text_from_pdf(pdf_file, start_page, end_page):
28
  try:
@@ -35,7 +37,7 @@ def extract_text_from_pdf(pdf_file, start_page, end_page):
35
  st.error(f"Error extracting text from PDF: {e}")
36
  return ""
37
 
38
- # Section selection
39
  if uploaded_file:
40
  st.success("PDF uploaded successfully.")
41
  selected_section = st.selectbox("Select a Section", list(sections.keys()))
@@ -51,21 +53,22 @@ if uploaded_file:
51
  question = st.text_input("Ask a question about this section:")
52
 
53
  if question:
54
- # Make API call to Groq
55
  try:
56
  st.info("Querying Groq API...")
57
- response = requests.post(
58
- "https://api.groq.com/v1/ask",
59
- headers={"Authorization": f"Bearer {GROQ_API_KEY}"},
60
- json={"context": section_text, "question": question},
 
 
 
 
61
  )
62
 
63
- if response.status_code == 200:
64
- answer = response.json().get("answer", "No answer found.")
65
- st.success(f"Answer: {answer}")
66
- else:
67
- st.error(f"Error: {response.json().get('error', 'Unknown error occurred')}")
68
  except Exception as e:
69
- st.error(f"API call failed: {e}")
70
  else:
71
  st.warning("Please upload a PDF file to proceed.")
 
1
  import os
2
  import streamlit as st
3
  from PyPDF2 import PdfReader
4
+ from groq import Groq
5
 
6
  # Set up the Streamlit app
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 the 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.")
15
+ else:
16
+ client = Groq(api_key=GROQ_API_KEY)
17
+
18
  # Upload PDF
19
  uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
20
 
 
25
  # Add more sections with their page ranges here
26
  }
27
 
 
 
 
 
 
 
28
  # Helper function to extract text from PDF
29
  def extract_text_from_pdf(pdf_file, start_page, end_page):
30
  try:
 
37
  st.error(f"Error extracting text from PDF: {e}")
38
  return ""
39
 
40
+ # Section selection and Q&A functionality
41
  if uploaded_file:
42
  st.success("PDF uploaded successfully.")
43
  selected_section = st.selectbox("Select a Section", list(sections.keys()))
 
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
70
+ st.success(f"Answer: {answer}")
 
 
 
71
  except Exception as e:
72
+ st.error(f"Error communicating with Groq API: {e}")
73
  else:
74
  st.warning("Please upload a PDF file to proceed.")