Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -0,0 +1,245 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import streamlit as st
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            import meta
         | 
| 4 | 
            +
            import fitz
         | 
| 5 | 
            +
            import requests
         | 
| 6 | 
            +
            from bs4 import BeautifulSoup
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            from best_seller import get_items
         | 
| 9 | 
            +
            from pharmap_url import get_link_mapped
         | 
| 10 | 
            +
            from summarize import get_summary_app
         | 
| 11 | 
            +
            from utils.results_utils import get_brief, ReadPDFFile, PopulateDict
         | 
| 12 | 
            +
            from utils.utils import load_image_from_local, local_css, pure_comma_separation, remote_css
         | 
| 13 | 
            +
            from examples import CATEGORY_LIST, LINK_LIST, STUDY_LIST, FILE_LIST
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            with open("bestseller.html", "r") as f:
         | 
| 16 | 
            +
            	html_content = f.read()
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            # html_content[:500]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
             | 
| 21 | 
            +
             | 
| 22 | 
            +
             | 
| 23 | 
            +
             | 
| 24 | 
            +
            def main():
         | 
| 25 | 
            +
            	st.set_page_config(
         | 
| 26 | 
            +
            		page_title="Tracer",
         | 
| 27 | 
            +
            		page_icon="👻",
         | 
| 28 | 
            +
            		layout="wide",
         | 
| 29 | 
            +
            		initial_sidebar_state="expanded"
         | 
| 30 | 
            +
            	)
         | 
| 31 | 
            +
            	remote_css("https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600&family=Poppins:wght@600&display=swap")
         | 
| 32 | 
            +
            	local_css("asset/css/style.css")
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            	col1, col2 = st.columns([5, 5])
         | 
| 35 | 
            +
            	with col2:
         | 
| 36 | 
            +
            		# st.image(load_image_from_local("asset/images/main_logo.png"), width=300)
         | 
| 37 | 
            +
            		# st.markdown("Discover --> Extract --> Sense --> Decide --> Render", unsafe_allow_html=True)
         | 
| 38 | 
            +
            		st.image(load_image_from_local("asset/images/task_theme.png"), width=500)
         | 
| 39 | 
            +
            		with st.expander("Design & Development", expanded=True):
         | 
| 40 | 
            +
            			st.markdown(meta.SIDEBAR_INFO, unsafe_allow_html=True)
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            		with st.expander("What does Tracer do?", expanded=True):
         | 
| 43 | 
            +
            			st.markdown(meta.STORY, unsafe_allow_html=True)
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            		st.markdown(meta.CONCEPT_INFO, unsafe_allow_html=True)
         | 
| 46 | 
            +
            		st.image(load_image_from_local("asset/images/side_bar.png"), width=600)
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            	with col1:
         | 
| 49 | 
            +
            		st.markdown(meta.HEADER_INFO, unsafe_allow_html=True)
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            		st.markdown(meta.CHEF_INFO, unsafe_allow_html=True)
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            		# st.write('<style>div.row-widget.stRadio > div{flex-direction:row;justify-content: center;} </style>',
         | 
| 54 | 
            +
            		#          unsafe_allow_html=True)
         | 
| 55 | 
            +
            		#
         | 
| 56 | 
            +
            		# st.write('<style>div.st-bf{flex-direction:column;} div.st-ag{font-weight:bold;padding-left:2px;}</style>',
         | 
| 57 | 
            +
            		#          unsafe_allow_html=True)
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            		choose = st.radio("Choose Task", ("Get Best Sellers - Amazon",
         | 
| 60 | 
            +
            		                                  "Get Therapy Area Mapped for Given Disease/s",
         | 
| 61 | 
            +
            		                                  "Generate Summary for Clinical Trials",
         | 
| 62 | 
            +
            		                                  "Generate Financial Brief for given 10-Q file"))
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            		if choose == 'Get Best Sellers - Amazon':
         | 
| 65 | 
            +
             | 
| 66 | 
            +
            			content = BeautifulSoup(html_content, "html.parser")
         | 
| 67 | 
            +
            			mydivs = content.find_all("a", {"class": "a-link-normal"})
         | 
| 68 | 
            +
            			bs = get_items(mydivs)
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            			prompts = list(CATEGORY_LIST.keys()) + ["Custom"]
         | 
| 71 | 
            +
            			prompt = st.selectbox(
         | 
| 72 | 
            +
            				'Examples (select from this list)',
         | 
| 73 | 
            +
            				prompts,
         | 
| 74 | 
            +
            				# index=len(prompts) - 1,
         | 
| 75 | 
            +
            				index=0,
         | 
| 76 | 
            +
             | 
| 77 | 
            +
            			)
         | 
| 78 | 
            +
             | 
| 79 | 
            +
            			if prompt == "Custom":
         | 
| 80 | 
            +
            				prompt_box = ""
         | 
| 81 | 
            +
            			else:
         | 
| 82 | 
            +
            				prompt_box = CATEGORY_LIST[prompt]
         | 
| 83 | 
            +
             | 
| 84 | 
            +
            			items = st.text_area(
         | 
| 85 | 
            +
            				'Selected Best Seller Category: ',
         | 
| 86 | 
            +
            				prompt_box,
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            			)
         | 
| 89 | 
            +
            			# items = pure_comma_separation(items, return_list=False)
         | 
| 90 | 
            +
            			entered_items = st.empty()
         | 
| 91 | 
            +
            			result_button = st.button('Get Best Selling Items!')
         | 
| 92 | 
            +
             | 
| 93 | 
            +
            			st.markdown(
         | 
| 94 | 
            +
            				"<hr />",
         | 
| 95 | 
            +
            				unsafe_allow_html=True
         | 
| 96 | 
            +
            			)
         | 
| 97 | 
            +
             | 
| 98 | 
            +
             | 
| 99 | 
            +
            			if result_button:
         | 
| 100 | 
            +
            				with st.spinner("Getting the best sellers"):
         | 
| 101 | 
            +
            					st.write(items)
         | 
| 102 | 
            +
            					st.write(bs[items])
         | 
| 103 | 
            +
             | 
| 104 | 
            +
            		if choose == 'Get Therapy Area Mapped for Given Disease/s':
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            			prompts = list(LINK_LIST.keys()) + ["Custom"]
         | 
| 107 | 
            +
            			prompt = st.selectbox(
         | 
| 108 | 
            +
            				'Examples (select from this list)',
         | 
| 109 | 
            +
            				prompts,
         | 
| 110 | 
            +
            				# index=len(prompts) - 1,
         | 
| 111 | 
            +
            				index=0
         | 
| 112 | 
            +
            			)
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            			if prompt == "Custom":
         | 
| 115 | 
            +
            				prompt_box = ""
         | 
| 116 | 
            +
            			else:
         | 
| 117 | 
            +
            				prompt_box = LINK_LIST[prompt]
         | 
| 118 | 
            +
             | 
| 119 | 
            +
            			items = st.text_area(
         | 
| 120 | 
            +
            				'Selected Sample Webpage Link: ',
         | 
| 121 | 
            +
            				prompt_box,
         | 
| 122 | 
            +
            			)
         | 
| 123 | 
            +
            			# items = pure_comma_separation(items, return_list=False)
         | 
| 124 | 
            +
            			entered_items = st.empty()
         | 
| 125 | 
            +
            			result_button = st.button('Get Therapy Areas!')
         | 
| 126 | 
            +
             | 
| 127 | 
            +
            			st.markdown(
         | 
| 128 | 
            +
            				"<hr />",
         | 
| 129 | 
            +
            				unsafe_allow_html=True
         | 
| 130 | 
            +
            			)
         | 
| 131 | 
            +
             | 
| 132 | 
            +
            			if result_button:
         | 
| 133 | 
            +
            				get_link_mapped(items)
         | 
| 134 | 
            +
             | 
| 135 | 
            +
            		if choose == 'Generate Summary for Clinical Trials':
         | 
| 136 | 
            +
             | 
| 137 | 
            +
            			prompts = list(STUDY_LIST.keys()) + ["Custom"]
         | 
| 138 | 
            +
            			prompt = st.selectbox(
         | 
| 139 | 
            +
            				'Examples (select from this list)',
         | 
| 140 | 
            +
            				prompts,
         | 
| 141 | 
            +
            				# index=len(prompts) - 1,
         | 
| 142 | 
            +
            				index=0
         | 
| 143 | 
            +
            			)
         | 
| 144 | 
            +
             | 
| 145 | 
            +
            			if prompt == "Custom":
         | 
| 146 | 
            +
            				prompt_box = ""
         | 
| 147 | 
            +
            			else:
         | 
| 148 | 
            +
            				prompt_box = STUDY_LIST[prompt]
         | 
| 149 | 
            +
             | 
| 150 | 
            +
            			items = st.text_area(
         | 
| 151 | 
            +
            				'Selected Sample Study ID (Clinical Trial.Gov): ',
         | 
| 152 | 
            +
            				prompt_box,
         | 
| 153 | 
            +
            			)
         | 
| 154 | 
            +
            			# items = pure_comma_separation(items, return_list=False)
         | 
| 155 | 
            +
            			# st.write(items)
         | 
| 156 | 
            +
            			entered_items = st.empty()
         | 
| 157 | 
            +
            			result_button = st.button('Get Summary!')
         | 
| 158 | 
            +
             | 
| 159 | 
            +
            			st.markdown(
         | 
| 160 | 
            +
            				"<hr />",
         | 
| 161 | 
            +
            				unsafe_allow_html=True
         | 
| 162 | 
            +
            			)
         | 
| 163 | 
            +
             | 
| 164 | 
            +
            			if result_button:
         | 
| 165 | 
            +
            				headline_output, summary_output = get_summary_app(items)
         | 
| 166 | 
            +
             | 
| 167 | 
            +
            				# headline = f"""
         | 
| 168 | 
            +
            				# <p class="story-box font-body">{headline_output}</p>
         | 
| 169 | 
            +
            				# """
         | 
| 170 | 
            +
            				# summary = f"""
         | 
| 171 | 
            +
            				# <p class="story-box font-body">{summary_output}</p>
         | 
| 172 | 
            +
            				# """
         | 
| 173 | 
            +
            				# st.markdown(summary, unsafe_allow_html=True)
         | 
| 174 | 
            +
             | 
| 175 | 
            +
            				# st.write(headline_output)
         | 
| 176 | 
            +
            				st.write(summary_output)
         | 
| 177 | 
            +
             | 
| 178 | 
            +
            		if choose == 'Generate Financial Brief for given 10-Q file':
         | 
| 179 | 
            +
             | 
| 180 | 
            +
            			prompts = list(FILE_LIST.keys()) + ["Custom"]
         | 
| 181 | 
            +
            			prompt = st.selectbox(
         | 
| 182 | 
            +
            				'Examples (select from this list)',
         | 
| 183 | 
            +
            				prompts,
         | 
| 184 | 
            +
            				# index=len(prompts) - 1,
         | 
| 185 | 
            +
            				index=0
         | 
| 186 | 
            +
            			)
         | 
| 187 | 
            +
            			if prompt == "Custom":
         | 
| 188 | 
            +
            				file = st.file_uploader("Choose a file")
         | 
| 189 | 
            +
            				uploaded_file  = open(file, 'rb')
         | 
| 190 | 
            +
            				doc = fitz.open(uploaded_file)
         | 
| 191 | 
            +
            				result_button = st.button('Get Earnings Brief!')
         | 
| 192 | 
            +
             | 
| 193 | 
            +
            				st.markdown(
         | 
| 194 | 
            +
            					"<hr />",
         | 
| 195 | 
            +
            					unsafe_allow_html=True
         | 
| 196 | 
            +
            				)
         | 
| 197 | 
            +
            				print("before custom result button")
         | 
| 198 | 
            +
            				if result_button and uploaded_file is not None:
         | 
| 199 | 
            +
            					with st.spinner("Generating Brief..."):
         | 
| 200 | 
            +
            						DictText = {}
         | 
| 201 | 
            +
            						Extracttext = ReadPDFFile(doc, 19, "(Dollars in millions except per share amounts)", "")
         | 
| 202 | 
            +
            						DictText = PopulateDict(Extracttext, DictText)
         | 
| 203 | 
            +
            						Extracttext = ReadPDFFile(doc, 20, "Overview (continued)",
         | 
| 204 | 
            +
            						                          "Results may not sum due to rounding")
         | 
| 205 | 
            +
            						DictText = PopulateDict(Extracttext, DictText)
         | 
| 206 | 
            +
            						Extracttext = ReadPDFFile(doc, 21, "(Dollars in millions)", "Three months ended")
         | 
| 207 | 
            +
            						DictText = PopulateDict(Extracttext, DictText)
         | 
| 208 | 
            +
            						# st.write(DictText)
         | 
| 209 | 
            +
            						st.write(get_brief(DictText))
         | 
| 210 | 
            +
             | 
| 211 | 
            +
            			else:
         | 
| 212 | 
            +
            				prompt_box = FILE_LIST[prompt]
         | 
| 213 | 
            +
             | 
| 214 | 
            +
            				items = st.text_area(
         | 
| 215 | 
            +
            					'Selected File Name: ',
         | 
| 216 | 
            +
            					prompt_box,
         | 
| 217 | 
            +
            				)
         | 
| 218 | 
            +
            				# items = pure_comma_separation(items, return_list=False)
         | 
| 219 | 
            +
            				# st.write(items)
         | 
| 220 | 
            +
            				entered_items = st.empty()
         | 
| 221 | 
            +
            				result_button = st.button('Get Earnings Brief!')
         | 
| 222 | 
            +
             | 
| 223 | 
            +
            				st.markdown(
         | 
| 224 | 
            +
            					"<hr />",
         | 
| 225 | 
            +
            					unsafe_allow_html=True
         | 
| 226 | 
            +
            				)
         | 
| 227 | 
            +
             | 
| 228 | 
            +
            				if result_button:
         | 
| 229 | 
            +
            					uploaded_file = open("asset/data/TF-Q1'22-10Q.pdf", 'rb')
         | 
| 230 | 
            +
            					doc = fitz.open(uploaded_file)
         | 
| 231 | 
            +
            					with st.spinner("Generating Brief..."):
         | 
| 232 | 
            +
            						DictText = {}
         | 
| 233 | 
            +
            						Extracttext = ReadPDFFile(doc, 19, "(Dollars in millions except per share amounts)", "")
         | 
| 234 | 
            +
            						DictText = PopulateDict(Extracttext, DictText)
         | 
| 235 | 
            +
            						Extracttext = ReadPDFFile(doc, 20, "Overview (continued)",
         | 
| 236 | 
            +
            						                          "Results may not sum due to rounding")
         | 
| 237 | 
            +
            						DictText = PopulateDict(Extracttext, DictText)
         | 
| 238 | 
            +
            						Extracttext = ReadPDFFile(doc, 21, "(Dollars in millions)", "Three months ended")
         | 
| 239 | 
            +
            						DictText = PopulateDict(Extracttext, DictText)
         | 
| 240 | 
            +
            						# st.write(DictText)
         | 
| 241 | 
            +
            						st.write(get_brief(DictText))
         | 
| 242 | 
            +
             | 
| 243 | 
            +
             | 
| 244 | 
            +
            if __name__ == '__main__':
         | 
| 245 | 
            +
            	main()
         |