shah1zil commited on
Commit
56592c8
·
verified ·
1 Parent(s): b5dd8a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -14
app.py CHANGED
@@ -26,30 +26,40 @@ if uploaded_file:
26
  constitution_text += page.extract_text()
27
 
28
  st.success("PDF Uploaded and Processed Successfully!")
29
-
30
- # Split text into chunks to handle token limits
31
- chunk_size = 5000 # Number of characters per chunk
32
- chunks = [constitution_text[i:i+chunk_size] for i in range(0, len(constitution_text), chunk_size)]
33
 
34
- # Let user select a chunk to ask questions about
35
- st.write("The document is too large, so it's split into smaller parts. Select one to ask questions:")
36
- selected_chunk = st.selectbox("Select a part of the document:", range(len(chunks)))
37
- current_chunk = chunks[selected_chunk]
 
 
 
 
 
 
 
 
 
 
38
 
39
- st.write("You selected part:", selected_chunk + 1)
40
- st.text_area("Selected Text Segment", current_chunk, height=200)
 
 
 
 
41
 
42
- # Ask a question about the selected chunk
43
- user_question = st.text_input("Ask a question about the selected part:")
44
 
45
  if user_question:
46
  st.write("Processing your question...")
47
 
48
- # Send the selected chunk and question to Groq API
49
  try:
50
  chat_completion = client.chat.completions.create(
51
  messages=[
52
- {"role": "user", "content": f"Context: {current_chunk}\nQuestion: {user_question}"}
53
  ],
54
  model="llama3-8b-8192",
55
  )
@@ -59,3 +69,66 @@ if uploaded_file:
59
  st.write(answer)
60
  except Exception as e:
61
  st.error(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  constitution_text += page.extract_text()
27
 
28
  st.success("PDF Uploaded and Processed Successfully!")
 
 
 
 
29
 
30
+ # Predefined sections or chapters of the Constitution
31
+ # Assuming the Constitution is structured, you can define sections like this:
32
+ sections = {
33
+ "Preamble": (0, 1000), # You may need to adjust the range manually based on text positions
34
+ "Fundamental Rights": (1000, 5000),
35
+ "Directive Principles of State Policy": (5000, 8000),
36
+ "Structure of Government": (8000, 12000),
37
+ "Emergency Provisions": (12000, 15000),
38
+ "Amendment Procedure": (15000, 18000)
39
+ }
40
+
41
+ # Allow user to select a specific section
42
+ section_choice = st.selectbox("Select a section of the Constitution:", list(sections.keys()))
43
+ section_start, section_end = sections[section_choice]
44
 
45
+ # Extract selected section text
46
+ selected_section_text = constitution_text[section_start:section_end]
47
+
48
+ # Show the extracted section text to the user
49
+ st.write(f"### Selected Section: {section_choice}")
50
+ st.text_area("Text from selected section", selected_section_text, height=200)
51
 
52
+ # Ask a question about the selected section
53
+ user_question = st.text_input(f"Ask a question about the '{section_choice}' section:")
54
 
55
  if user_question:
56
  st.write("Processing your question...")
57
 
58
+ # Send the selected section text and question to Groq API
59
  try:
60
  chat_completion = client.chat.completions.create(
61
  messages=[
62
+ {"role": "user", "content": f"Context: {selected_section_text}\nQuestion: {user_question}"}
63
  ],
64
  model="llama3-8b-8192",
65
  )
 
69
  st.write(answer)
70
  except Exception as e:
71
  st.error(f"An error occurred: {e}")
72
+
73
+
74
+ # # Install dependencies
75
+ # # pip install streamlit PyPDF2 groq
76
+
77
+ # import streamlit as st
78
+ # from PyPDF2 import PdfReader
79
+ # import os
80
+ # from groq import Groq
81
+
82
+ # # Set the API key
83
+ # os.environ["GROQ_API_KEY"] = "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"
84
+
85
+ # # Initialize Groq client
86
+ # client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
87
+
88
+ # # Streamlit App
89
+ # st.title("Pakistani Constitution Q&A App")
90
+ # st.write("Upload the PDF of the Pakistani Constitution and ask questions about it.")
91
+
92
+ # # Upload PDF
93
+ # uploaded_file = st.file_uploader("Upload PDF", type="pdf")
94
+ # if uploaded_file:
95
+ # # Extract text from the PDF
96
+ # pdf_reader = PdfReader(uploaded_file)
97
+ # constitution_text = ""
98
+ # for page in pdf_reader.pages:
99
+ # constitution_text += page.extract_text()
100
+
101
+ # st.success("PDF Uploaded and Processed Successfully!")
102
+
103
+ # # Split text into chunks to handle token limits
104
+ # chunk_size = 5000 # Number of characters per chunk
105
+ # chunks = [constitution_text[i:i+chunk_size] for i in range(0, len(constitution_text), chunk_size)]
106
+
107
+ # # Let user select a chunk to ask questions about
108
+ # st.write("The document is too large, so it's split into smaller parts. Select one to ask questions:")
109
+ # selected_chunk = st.selectbox("Select a part of the document:", range(len(chunks)))
110
+ # current_chunk = chunks[selected_chunk]
111
+
112
+ # st.write("You selected part:", selected_chunk + 1)
113
+ # st.text_area("Selected Text Segment", current_chunk, height=200)
114
+
115
+ # # Ask a question about the selected chunk
116
+ # user_question = st.text_input("Ask a question about the selected part:")
117
+
118
+ # if user_question:
119
+ # st.write("Processing your question...")
120
+
121
+ # # Send the selected chunk and question to Groq API
122
+ # try:
123
+ # chat_completion = client.chat.completions.create(
124
+ # messages=[
125
+ # {"role": "user", "content": f"Context: {current_chunk}\nQuestion: {user_question}"}
126
+ # ],
127
+ # model="llama3-8b-8192",
128
+ # )
129
+ # # Display the answer
130
+ # answer = chat_completion.choices[0].message.content
131
+ # st.write("### Answer:")
132
+ # st.write(answer)
133
+ # except Exception as e:
134
+ # st.error(f"An error occurred: {e}")