Ronio Jerico Roque commited on
Commit
fca2084
·
1 Parent(s): 5fd0079

refactor: clean up TargetMarketAnalyst class and improve chat functionality; add openai to requirements

Browse files
Files changed (2) hide show
  1. classes/Target_Market.py +23 -103
  2. requirements.txt +1 -0
classes/Target_Market.py CHANGED
@@ -32,8 +32,8 @@ class TargetMarketAnalyst:
32
  st.header(self.analyst_name)
33
 
34
  # EVALUATION FORM LINK
35
- url = os.getenv('Link')
36
- st.write('Evaluation Form: [Link](%s)' % url)
37
 
38
  def request_model(self, payload_txt):
39
  response = requests.post(self.model_url, json=payload_txt)
@@ -60,8 +60,7 @@ class TargetMarketAnalyst:
60
  return output
61
 
62
  def row1(self):
63
- col1, col2 = st.columns(gap="medium", spec=[0.33, 0.66])
64
- with col1:
65
  prompt = st.chat_input("How can I help you today?")
66
 
67
  #client = genai.Client(api_key="AIzaSyArNeCctdKaWhxprHTnHyISCd0CpKqB5dk")
@@ -88,8 +87,16 @@ class TargetMarketAnalyst:
88
 
89
  url = "http://172.17.21.23:7860/api/v1/run/382ac239-3231-4f9b-89fe-f5ee26e4b1eb?stream=false"
90
 
91
-
 
 
 
 
92
  if prompt:
 
 
 
 
93
  response = requests.post(url, json=payload_txt, headers=headers, params={'stream': 'false'})
94
  response.raise_for_status()
95
  output = response.json()
@@ -99,106 +106,19 @@ class TargetMarketAnalyst:
99
  return text.replace('\\n', '\n').replace('\\t', '\t').replace('\\r', '\r')
100
 
101
  self.text = remove_escape_sequences(json.dumps(text, ensure_ascii=False).strip('"'))
102
- '''
103
- self.topics = st.text_input("Topic of Interest: ", placeholder='Enter Topic of Interest:', key='topic')
104
- #self.start_date = st.date_input('Start Date:', value="today", key='start_date')
105
- #self.end_date = st.date_input('End Date:', value="today", key='end_date')
106
-
107
- self.sort = st.radio(
108
- "Sort by:",
109
- ["Published At", "Popularity", "Relevancy"],
110
- captions=[
111
- "Newest articles come first",
112
- "Articles from popular sources and publishers come first",
113
- "Articles more closely related to topic come first",
114
- ],
115
- )
116
-
117
- if self.sort == 'Relevancy':
118
- self.sort = 'relevancy'
119
- elif self.sort == 'Popularity':
120
- self.sort = 'popularity'
121
- elif self.sort == 'Published At':
122
- self.sort = 'publishedAt'
123
- '''
124
- with col2:
125
- #st.write("") # FOR THE HIDE BUTTON
126
- #st.write("") # FOR THE HIDE BUTTON
127
- st.write("Content: ")
128
- #st.session_state['analyzing'] = False
129
- #st.write("") # FOR THE HIDE BUTTON
130
- if prompt:
131
- st.write(self.text)
132
- '''analyze_button = st.button("", disabled=initialize_analyze_session())
133
- start_time = time.time()
134
- if analyze_button:
135
- hide_button()
136
- if self.topics:
137
- combined_text = ""
138
- with st.spinner('Analyzing...', show_time=True):
139
- st.write('')
140
- # INITIALIZING SESSIONS
141
-
142
- combined_text += f"Topic/s of Interest: {self.topics}\n"
143
- # OUTPUT FOR SEO ANALYST
144
- root = 'https://newsapi.org/v2/everything?'
145
- language = "en"
146
- api = os.getenv('apiKey')
147
- response = requests.get(f'{root}q={self.topics}&language={language}&sortBy={self.sort}&language="en"&apiKey={api}')
148
- try:
149
- response.raise_for_status()
150
- output = response.json()
151
- with st.expander("Output", expanded=True):
152
- text = output['articles']
153
-
154
- for article in text:
155
- source = article['source']['name']
156
- published = article['publishedAt']
157
- author = article['author']
158
- title = article['title']
159
- description = article['description']
160
- url = article['url']
161
- urlImage = article['urlToImage']
162
- content = article['content']
163
-
164
-
165
- left_co, cent_co,last_co = st.columns(3)
166
-
167
- if urlImage:
168
- with left_co:
169
- st.image(urlImage, width=400)
170
- st.write(f"**{title}**")
171
- st.write(f"{description}")
172
- st.write(f"**Author**: {author}")
173
- st.write(f"**Source**: {source}")
174
- st.write('Link: [URL](%s)' % url)
175
- st.write(f"**Published At**: {published}")
176
-
177
- st.write("---")
178
 
179
-
180
- st.write(article)
181
-
182
- end_time = time.time()
183
- time_lapsed = end_time - start_time
184
- debug_info = {
185
- 'analyst': self.analyst_name,
186
- 'time_lapsed': time_lapsed,
187
-
188
- }
189
-
190
- collect_telemetry(debug_info)
191
-
192
- with st.expander("Debug information", icon="⚙"):
193
- st.write(debug_info)
194
 
195
- st.session_state['analyzing'] = False
196
- except requests.exceptions.HTTPError:
197
- st.info("Exceeded limit, please come back tomorrow")
198
- else:
199
- st.info("Please provide a topic of interest")
200
- hide_button()
201
- '''
202
  if __name__ == "__main__":
203
  st.set_page_config(layout="wide")
204
 
 
32
  st.header(self.analyst_name)
33
 
34
  # EVALUATION FORM LINK
35
+ #url = os.getenv('Link')
36
+ #st.write('Evaluation Form: [Link](%s)' % url)
37
 
38
  def request_model(self, payload_txt):
39
  response = requests.post(self.model_url, json=payload_txt)
 
60
  return output
61
 
62
  def row1(self):
63
+
 
64
  prompt = st.chat_input("How can I help you today?")
65
 
66
  #client = genai.Client(api_key="AIzaSyArNeCctdKaWhxprHTnHyISCd0CpKqB5dk")
 
87
 
88
  url = "http://172.17.21.23:7860/api/v1/run/382ac239-3231-4f9b-89fe-f5ee26e4b1eb?stream=false"
89
 
90
+ if "messages" not in st.session_state:
91
+ st.session_state.messages = []
92
+ for message in st.session_state.messages:
93
+ with st.chat_message(message["role"]):
94
+ st.markdown(message["content"])
95
  if prompt:
96
+ st.chat_message("user").markdown(prompt)
97
+ # Add user message to chat history
98
+ st.session_state.messages.append({"role": "user", "content": prompt})
99
+
100
  response = requests.post(url, json=payload_txt, headers=headers, params={'stream': 'false'})
101
  response.raise_for_status()
102
  output = response.json()
 
106
  return text.replace('\\n', '\n').replace('\\t', '\t').replace('\\r', '\r')
107
 
108
  self.text = remove_escape_sequences(json.dumps(text, ensure_ascii=False).strip('"'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ response = f"Echo: {self.text}"
111
+ with st.chat_message("assistant"):
112
+ response = st.write_stream(self.stream_string(self.text))
113
+
114
+ # Add assistant response to chat history
115
+ st.session_state.messages.append({"role": "assistant", "content": response})
116
+
117
+ def stream_string(self, text, delay=0.01):
118
+ for char in text:
119
+ yield char
120
+ time.sleep(delay) # Small delay to simulate typing
 
 
 
 
121
 
 
 
 
 
 
 
 
122
  if __name__ == "__main__":
123
  st.set_page_config(layout="wide")
124
 
requirements.txt CHANGED
@@ -9,3 +9,4 @@ streamlit_paste_button
9
  pymupdf
10
  pymongo
11
  newsapi-python
 
 
9
  pymupdf
10
  pymongo
11
  newsapi-python
12
+ openai