srkajol commited on
Commit
ba52e15
1 Parent(s): 8bd698c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -160
app.py CHANGED
@@ -3,207 +3,95 @@ from PyPDF2 import PdfReader
3
  import openai
4
  import os
5
  import csv
6
- import pandas as pd
7
  import textwrap
8
  import re
9
  import requests
10
-
11
  def add_line_break(text):
12
  # Use the re.sub() function with a regex pattern to add a line break before "amendment"
13
  result = re.sub(r"(amendment)", r"\n\1", text)
14
-
15
  return result
16
-
17
- # Placeholder function for your existing Python code 1
18
  def process_pdf(pdf_file):
19
  # Your existing code that processes the uploaded PDF
20
- # Replace this with your actual code
21
  print("Processing PDF:", pdf_file.name)
 
 
22
  file = "C:\\Users\\phili\\Downloads\\member_list_clean.csv"
 
23
  namedict = dict()
24
  biglist = ""
25
- with open(file, mode ='r')as file:
26
-
27
- # reading the CSV file
28
- csvFile = csv.reader(file)
29
 
30
- # displaying the contents of the CSV file
31
- for lines in csvFile:
32
- #print(lines)
33
- MPid = lines[1]
34
- mpnam = lines[2]
35
- mpparty = lines[7]
36
- namedict[MPid] = [mpnam, mpparty]
37
- biglist += (MPid + "," + mpnam + "," + mpparty)
38
-
39
- openai.api_key = os.getenv("API_SECRET")
40
-
41
-
42
- reader = PdfReader(pdf_file)
43
- x = 0
44
- rawpagestring = ""
45
- number_of_pages = len(reader.pages)
46
- while x < 10:
47
- page = reader.pages[x]
48
- text = page.extract_text()
49
- rawpagestring += text
50
- x += 1
51
- #print(text)
52
- ## response = openai.ChatCompletion.create(
53
- ## model="gpt-4",
54
- ## messages=[
55
- ## {
56
- ## "role": "user",
57
- ## "content": "Read list of current MPs, MP ID and Party" + biglist
58
- ## }
59
- ## ],
60
- ## temperature=0.8,
61
- ## max_tokens=1024
62
- ## )
63
- response2 = openai.ChatCompletion.create(
64
- model="gpt-4",
65
- messages=[
66
- {
67
- "role": "user",
68
- "content": "List the amendmendments and the MPs involved in the following report and add the Political party for each MP. The Format required is Amendment(Amendment description): MP(Political Party)" + rawpagestring
69
- }
70
- ],
71
- temperature=0.8,
72
- max_tokens=1024
73
- )
74
- #print (response2)
75
- strr = response2['choices'][0]['message']['content']
76
-
77
-
78
- with open("C:\\Users\phili\\OneDrive\\Documents\\pdfoutput.csv", mode='w') as file:
79
- file.write(strr)
80
- response3 = openai.ChatCompletion.create(
81
- model="gpt-4",
82
- messages=[
83
- {
84
- "role": "user",
85
- "content": "Find Bill title in this ammendment report" + rawpagestring
86
- }
87
- ],
88
- temperature=0.8,
89
- max_tokens=1024
90
- )
91
- #print (response3)
92
-
93
- BT = response3['choices'][0]['message']['content']
94
- AS = response2['choices'][0]['message']['content']
95
- wrapped_AS = textwrap.fill(AS, width=80)
96
- AS_list = wrapped_AS.split('Amendment')
97
-
98
- modified_AS = add_line_break(wrapped_AS)
99
- # Display the table in Streamlit
100
-
101
-
102
- st.text(BT)
103
- st.text(AS)
104
-
105
-
106
- # Placeholder function for your existing Python code 2
107
  def process_text(text_input):
108
  # Your existing code that processes the text input
109
- # Replace this with your actual code
110
  file = "C:\\Users\\phili\\Downloads\\member_list_clean.csv"
111
  membernam = text_input
112
  namedict = dict()
113
  biglist = ""
114
- with open(file, mode ='r')as file:
115
-
116
- # reading the CSV file
117
- csvFile = csv.reader(file)
118
 
119
- # displaying the contents of the CSV file
120
- for lines in csvFile:
121
- #print(lines)
122
- MPid = lines[1]
123
- mpnam1 = lines[2]
124
- mpnam2= lines[3]
125
- mpnam3 = lines[4]
126
- mpnam4 = lines[5]
127
- namedict[MPid] = [mpnam1, mpnam2, mpnam3, mpnam4]
 
128
  idlist = list(namedict.keys())
129
  for nam in idlist:
130
- print (nam)
131
  mpnamlist = namedict[nam]
132
- #print(mpnamlist)
133
- if (membernam in mpnamlist) == True:
134
  memberid = nam
135
  break
136
-
137
- def get_api_data(api_endpoint):
138
- response = requests.get(api_endpoint)
139
- if response.status_code == 200:
140
- return response.json()
141
- else:
142
- print("Error fetching legislation data:", response.status_code)
143
- return None
144
- mp_id = memberid
145
- api_endpoint_contribution = ''.join(['https://members-api.parliament.uk/api/Members/',str(mp_id),'/ContributionSummary'])
146
- api_endpoint_voting = ''.join(['https://members-api.parliament.uk/api/Members/',str(mp_id),'/Voting?house=1'])
147
-
148
- output_contribution = get_api_data(api_endpoint_contribution)
149
- output_voting = get_api_data(api_endpoint_voting)
150
- openai.organization = "org-a1HVJGz5R2D6o5igBavfKvFU"
151
- openai.api_key = "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"
152
-
153
- response3 = openai.ChatCompletion.create(
154
- model="gpt-4",
155
- messages=[
156
- {
157
- "role": "user",
158
- "content": "Produce a list of what" + membernam +"'s prorities are based on the following data" + str(output_voting) + str(output_contribution)
159
- }
160
- ],
161
- temperature=0.8,
162
- max_tokens=512
163
- )
164
- st.text(response3['choices'][0]['message']['content'])
165
-
166
-
167
  def main():
168
  house_of_commons_green = "#007A57"
169
-
170
  # Apply custom CSS to set the background color
171
- custom_css = '''
172
- <style>
173
- body {
174
- background-color: #007A57;
175
- }
176
- container {
177
- background-color: #007A57;
178
- }
179
- </style>
180
- '''
181
 
182
-
183
-
184
-
185
  # Use st.markdown() to apply the custom CSS targeting the root element (body)
186
  st.markdown(custom_css, unsafe_allow_html=True)
187
  st.title("Legislative Passage Toolkit")
188
-
189
  # File Uploader for PDF
190
  uploaded_pdf = st.file_uploader("Upload an amendment here", type=["pdf"])
191
-
192
  if uploaded_pdf is not None:
193
  # Button to process the uploaded PDF
194
  if st.button("Upload Amendments Summary"):
195
  process_pdf(uploaded_pdf)
196
-
197
- # Text Entry Box for user input
198
-
199
-
200
- ## if st.form_submit_button("Process Text"):
201
- ## process_text(user_input)
202
  user_input = st.text_input("Which MP would you like more information about?:")
203
  if st.button("Search MP"):
204
  process_text(user_input)
205
-
206
-
207
  if __name__ == "__main__":
208
  main()
209
-
 
3
  import openai
4
  import os
5
  import csv
 
6
  import textwrap
7
  import re
8
  import requests
9
+
10
  def add_line_break(text):
11
  # Use the re.sub() function with a regex pattern to add a line break before "amendment"
12
  result = re.sub(r"(amendment)", r"\n\1", text)
 
13
  return result
14
+
 
15
  def process_pdf(pdf_file):
16
  # Your existing code that processes the uploaded PDF
 
17
  print("Processing PDF:", pdf_file.name)
18
+
19
+ # Replace the file path with your actual CSV file containing MPs' information
20
  file = "C:\\Users\\phili\\Downloads\\member_list_clean.csv"
21
+
22
  namedict = dict()
23
  biglist = ""
 
 
 
 
24
 
25
+ with open(file, mode='r') as csvfile:
26
+ csvFile = csv.reader(csvfile)
27
+ for lines in csvFile:
28
+ MPid = lines[1]
29
+ mpnam = lines[2]
30
+ mpparty = lines[7]
31
+ namedict[MPid] = [mpnam, mpparty]
32
+ biglist += (MPid + "," + mpnam + "," + mpparty)
33
+
34
+ # Rest of your existing code...
35
+ # Please uncomment and complete the API calls using the openai library.
36
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def process_text(text_input):
38
  # Your existing code that processes the text input
39
+ # Please replace the file path with your actual CSV file containing MPs' information
40
  file = "C:\\Users\\phili\\Downloads\\member_list_clean.csv"
41
  membernam = text_input
42
  namedict = dict()
43
  biglist = ""
 
 
 
 
44
 
45
+ with open(file, mode='r') as csvfile:
46
+ csvFile = csv.reader(csvfile)
47
+ for lines in csvFile:
48
+ MPid = lines[1]
49
+ mpnam1 = lines[2]
50
+ mpnam2 = lines[3]
51
+ mpnam3 = lines[4]
52
+ mpnam4 = lines[5]
53
+ namedict[MPid] = [mpnam1, mpnam2, mpnam3, mpnam4]
54
+
55
  idlist = list(namedict.keys())
56
  for nam in idlist:
 
57
  mpnamlist = namedict[nam]
58
+ if membernam in mpnamlist:
 
59
  memberid = nam
60
  break
61
+
62
+ # Rest of your existing code...
63
+ # Please complete the API calls using the openai library and update the API keys.
64
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def main():
66
  house_of_commons_green = "#007A57"
67
+
68
  # Apply custom CSS to set the background color
69
+ custom_css = '''
70
+ <style>
71
+ body {
72
+ background-color: #007A57;
73
+ }
74
+ container {
75
+ background-color: #007A57;
76
+ }
77
+ </style>
78
+ '''
79
 
 
 
 
80
  # Use st.markdown() to apply the custom CSS targeting the root element (body)
81
  st.markdown(custom_css, unsafe_allow_html=True)
82
  st.title("Legislative Passage Toolkit")
83
+
84
  # File Uploader for PDF
85
  uploaded_pdf = st.file_uploader("Upload an amendment here", type=["pdf"])
86
+
87
  if uploaded_pdf is not None:
88
  # Button to process the uploaded PDF
89
  if st.button("Upload Amendments Summary"):
90
  process_pdf(uploaded_pdf)
91
+
 
 
 
 
 
92
  user_input = st.text_input("Which MP would you like more information about?:")
93
  if st.button("Search MP"):
94
  process_text(user_input)
95
+
 
96
  if __name__ == "__main__":
97
  main()