Initial Draft
Browse files
app.py
CHANGED
@@ -1,620 +1,603 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
-
import requests
|
4 |
-
import streamlit as st
|
5 |
-
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
import
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
from
|
19 |
-
from
|
20 |
-
from
|
21 |
-
from
|
22 |
-
|
23 |
-
from
|
24 |
-
from
|
25 |
-
|
26 |
-
from
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
#
|
64 |
-
def
|
65 |
-
"""
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
elif(ui_llm_provider == '
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
lv_markdown
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
lv_markdown
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
lv_markdown += f"
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
try:
|
383 |
-
st.subheader("
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
# --
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
if
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
)
|
604 |
-
lv_summarize_prompt_formatted = lv_summarize_prompt.format(
|
605 |
-
context=ui_summary_input
|
606 |
-
)
|
607 |
-
|
608 |
-
# -- LLM Response
|
609 |
-
if lv_model:
|
610 |
-
lv_response = fn_chatllm_response(ui_llm_provider, lv_summarize_prompt_formatted, lv_model, ui_processing_message)
|
611 |
-
st.session_state.lv_response = lv_response
|
612 |
-
|
613 |
-
# -- Display LLM response
|
614 |
-
if lv_response:
|
615 |
-
st.subheader("Summary")
|
616 |
-
st.markdown(lv_response)
|
617 |
-
|
618 |
-
# Loading Main
|
619 |
-
if __name__ == "__main__":
|
620 |
main()
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
from google.oauth2 import service_account
|
7 |
+
from json_repair import repair_json
|
8 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
9 |
+
|
10 |
+
import dto.user_story as us
|
11 |
+
import dto.release_notes as rs
|
12 |
+
import dto.requirement_gathering as rq
|
13 |
+
import prompts as pt
|
14 |
+
|
15 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
16 |
+
from langchain_groq import ChatGroq
|
17 |
+
from langchain_cohere import ChatCohere
|
18 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
19 |
+
from langchain_google_vertexai import ChatVertexAI
|
20 |
+
from langchain_openai import ChatOpenAI
|
21 |
+
from langchain.prompts import PromptTemplate
|
22 |
+
|
23 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
24 |
+
from langchain_community.document_loaders import WebBaseLoader
|
25 |
+
from langchain_community.document_loaders import PyPDFLoader
|
26 |
+
from langchain.output_parsers import PydanticOutputParser
|
27 |
+
|
28 |
+
# Caching LLM response
|
29 |
+
if "lv_response" not in st.session_state:
|
30 |
+
lv_response = None
|
31 |
+
st.session_state.lv_response = lv_response
|
32 |
+
else:
|
33 |
+
lv_response = st.session_state.lv_response
|
34 |
+
|
35 |
+
# Caching Extracted Text
|
36 |
+
if "lv_extracted_text" not in st.session_state:
|
37 |
+
lv_extracted_text = ""
|
38 |
+
st.session_state.lv_extracted_text = lv_extracted_text
|
39 |
+
else:
|
40 |
+
lv_extracted_text = st.session_state.lv_extracted_text
|
41 |
+
|
42 |
+
# Caching LLM Model
|
43 |
+
if "lv_model_session" not in st.session_state:
|
44 |
+
st.session_state.lv_model_session = None
|
45 |
+
|
46 |
+
# Display user Error, Warning or Success Message
|
47 |
+
def fn_display_user_messages(lv_extracted_text, lv_type, mv_processing_message):
|
48 |
+
"""Display user Info, Error, Warning or Success Messages"""
|
49 |
+
|
50 |
+
if lv_type == "Success":
|
51 |
+
with mv_processing_message.container():
|
52 |
+
st.success(lv_extracted_text)
|
53 |
+
elif lv_type == "Error":
|
54 |
+
with mv_processing_message.container():
|
55 |
+
st.error(lv_extracted_text)
|
56 |
+
elif lv_type == "Warning":
|
57 |
+
with mv_processing_message.container():
|
58 |
+
st.warning(lv_extracted_text)
|
59 |
+
else:
|
60 |
+
with mv_processing_message.container():
|
61 |
+
st.info(lv_extracted_text)
|
62 |
+
|
63 |
+
# Function to set proxy
|
64 |
+
def fn_set_proxy(ui_proxy_url, ui_no_proxy_url):
|
65 |
+
"""Configure http and https proxy programmatically"""
|
66 |
+
|
67 |
+
os.environ['HTTP_PROXY'] = ui_proxy_url
|
68 |
+
os.environ['HTTPS_PROXY'] = ui_proxy_url
|
69 |
+
os.environ['NO_PROXY'] = ui_no_proxy_url
|
70 |
+
|
71 |
+
print("=== Proxy SET ===")
|
72 |
+
print("HTTP_PROXY:", os.environ.get('HTTP_PROXY'))
|
73 |
+
print("HTTPS_PROXY:", os.environ.get('HTTPS_PROXY'))
|
74 |
+
print("NO_PROXY:", os.environ.get('NO_PROXY'))
|
75 |
+
print("=================")
|
76 |
+
|
77 |
+
# Function to convert Website URL content into text
|
78 |
+
def fn_scrape_website(ui_grounding_url):
|
79 |
+
"""Function to convert Website URL content into text"""
|
80 |
+
|
81 |
+
lv_html_loader = WebBaseLoader(ui_grounding_url)
|
82 |
+
lv_html = lv_html_loader.load()
|
83 |
+
|
84 |
+
return lv_html
|
85 |
+
|
86 |
+
# Function to convert PDF content into Documents
|
87 |
+
def fn_scraper_pdf(ui_grounding_pdf):
|
88 |
+
"""Function to convert PDF content into text"""
|
89 |
+
|
90 |
+
# -- Saving file
|
91 |
+
lv_temp_file_path = os.path.join("pdf-data",ui_grounding_pdf.name)
|
92 |
+
if not os.path.exists(lv_temp_file_path):
|
93 |
+
with open(lv_temp_file_path,"wb") as lv_file:
|
94 |
+
lv_file.write(ui_grounding_pdf.getbuffer())
|
95 |
+
|
96 |
+
# -- Extracting Data
|
97 |
+
lv_pdf_loader = PyPDFLoader(lv_temp_file_path)
|
98 |
+
lv_pdf_content = lv_pdf_loader.load()
|
99 |
+
|
100 |
+
return lv_pdf_content
|
101 |
+
|
102 |
+
# Function to search internet for information
|
103 |
+
def fn_search_web(ui_search_web_input):
|
104 |
+
"""Search internet for information"""
|
105 |
+
|
106 |
+
lv_search_run = DuckDuckGoSearchRun()
|
107 |
+
lv_result = lv_search_run.run(ui_search_web_input)
|
108 |
+
|
109 |
+
return lv_result
|
110 |
+
|
111 |
+
# Function to extract YouTube Video Transcript
|
112 |
+
def fn_you_tube_video_transcript(ui_youtube_url,ui_processing_message):
|
113 |
+
"""Extract YouTube Video Transcript"""
|
114 |
+
|
115 |
+
fn_display_user_messages("Generating Youtube Transcript","Info", ui_processing_message)
|
116 |
+
|
117 |
+
try:
|
118 |
+
lv_youtube_transcript = YouTubeTranscriptApi.get_transcript(ui_youtube_url)
|
119 |
+
lv_response = ' '.join([item['text'] for item in lv_youtube_transcript])
|
120 |
+
|
121 |
+
fn_display_user_messages("Successfully generated Youtube transcript","Success", ui_processing_message)
|
122 |
+
|
123 |
+
return lv_response
|
124 |
+
except Exception as error:
|
125 |
+
print('Error Generating Youtube Transcript', error)
|
126 |
+
fn_display_user_messages("Error Generating Youtube Transcript","Error", ui_processing_message)
|
127 |
+
raise error
|
128 |
+
|
129 |
+
# Function to unset proxy
|
130 |
+
def fn_unset_proxy():
|
131 |
+
"""Unset http and https proxy"""
|
132 |
+
|
133 |
+
os.environ.pop('HTTP_PROXY', None)
|
134 |
+
os.environ.pop('HTTPS_PROXY', None)
|
135 |
+
os.environ.pop('NO_PROXY', None)
|
136 |
+
|
137 |
+
print("=== Proxy UNSET ===")
|
138 |
+
print("HTTP_PROXY:", os.environ.get('HTTP_PROXY'))
|
139 |
+
print("HTTPS_PROXY:", os.environ.get('HTTPS_PROXY'))
|
140 |
+
print("NO_PROXY:", os.environ.get('NO_PROXY'))
|
141 |
+
print("===================")
|
142 |
+
|
143 |
+
# Create Chat LLM Instance
|
144 |
+
@st.cache_resource
|
145 |
+
def fn_create_chatllm(ui_llm_provider, ui_api_key, ui_model_details):
|
146 |
+
"""Create Chat LLM Instance"""
|
147 |
+
|
148 |
+
lv_model = None
|
149 |
+
|
150 |
+
try:
|
151 |
+
if(ui_llm_provider == 'Huggingface'):
|
152 |
+
lv_model = HuggingFaceEndpoint(
|
153 |
+
repo_id=ui_model_details,
|
154 |
+
temperature=1.0,
|
155 |
+
huggingfacehub_api_token=ui_api_key
|
156 |
+
)
|
157 |
+
elif(ui_llm_provider == 'Groq'):
|
158 |
+
lv_model = ChatGroq(
|
159 |
+
temperature=1.0,
|
160 |
+
model_name=ui_model_details
|
161 |
+
)
|
162 |
+
|
163 |
+
elif(ui_llm_provider == 'Cohere'):
|
164 |
+
lv_model = ChatCohere(
|
165 |
+
temperature=1.0,
|
166 |
+
model=ui_model_details
|
167 |
+
|
168 |
+
)
|
169 |
+
elif(ui_llm_provider == 'Google'):
|
170 |
+
lv_model = ChatGoogleGenerativeAI(
|
171 |
+
temperature=1.0,
|
172 |
+
model=ui_model_details,
|
173 |
+
max_output_tokens=1000000
|
174 |
+
)
|
175 |
+
elif(ui_llm_provider == 'OpenAI'):
|
176 |
+
lv_model = ChatOpenAI(
|
177 |
+
temperature=1.0,
|
178 |
+
model=ui_model_details
|
179 |
+
)
|
180 |
+
elif(ui_llm_provider == 'Google VertexAI'):
|
181 |
+
lv_api_key = json.loads(ui_api_key)
|
182 |
+
g_creds = service_account.Credentials.from_service_account_info(lv_api_key)
|
183 |
+
lv_model = ChatVertexAI(
|
184 |
+
project=lv_api_key.get("project_id"),
|
185 |
+
temperature=1.0,
|
186 |
+
model=ui_model_details,
|
187 |
+
credentials=g_creds
|
188 |
+
)
|
189 |
+
|
190 |
+
print("Returning new model")
|
191 |
+
|
192 |
+
except Exception as e:
|
193 |
+
print("Error Configuring Model"+str(e))
|
194 |
+
|
195 |
+
return lv_model
|
196 |
+
|
197 |
+
# Generate Speech to Text
|
198 |
+
@st.cache_resource
|
199 |
+
def fn_generate_speech_to_text(ui_audio_bytes,ui_api_key):
|
200 |
+
"""Generate Speech to Text"""
|
201 |
+
lv_extracted_text = None
|
202 |
+
|
203 |
+
try:
|
204 |
+
lv_url = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
|
205 |
+
lv_headers = {
|
206 |
+
'Authorization': "Bearer "+ui_api_key,
|
207 |
+
'Content-Type': "audio/wav"
|
208 |
+
}
|
209 |
+
response = requests.request("POST", lv_url, data=ui_audio_bytes, headers=lv_headers)
|
210 |
+
lv_extracted_text = response.json().get('text')
|
211 |
+
|
212 |
+
print(lv_extracted_text)
|
213 |
+
|
214 |
+
return lv_extracted_text
|
215 |
+
except Exception as error:
|
216 |
+
print('Error Generating Speech to Text', error)
|
217 |
+
raise error
|
218 |
+
|
219 |
+
# Generate LLM response
|
220 |
+
def fn_chatllm_response(ui_llm_provider, lv_summarize_prompt_formatted, lv_model, ui_processing_message):
|
221 |
+
"""Generate LLM response"""
|
222 |
+
|
223 |
+
fn_display_user_messages("Generating LLM Response","Info", ui_processing_message)
|
224 |
+
lv_response = None
|
225 |
+
|
226 |
+
try:
|
227 |
+
|
228 |
+
if(ui_llm_provider == 'Google VertexAI' or ui_llm_provider=='Google' or ui_llm_provider=='OpenAI' or ui_llm_provider=='Groq' or ui_llm_provider=='Cohere'):
|
229 |
+
lv_response = lv_model.invoke(lv_summarize_prompt_formatted).content
|
230 |
+
else:
|
231 |
+
lv_response = lv_model.invoke(lv_summarize_prompt_formatted)
|
232 |
+
|
233 |
+
lv_response = str(lv_response).replace("```json","")
|
234 |
+
lv_response = lv_response.replace("```","")
|
235 |
+
|
236 |
+
fn_display_user_messages("Generated LLM Response","Success", ui_processing_message)
|
237 |
+
return lv_response
|
238 |
+
except Exception as error:
|
239 |
+
print('Error Generating LLM Response', error)
|
240 |
+
fn_display_user_messages("Error Generating LLM Response","Error", ui_processing_message)
|
241 |
+
|
242 |
+
raise error
|
243 |
+
|
244 |
+
# Function to convert user story JSON to Markdown
|
245 |
+
def fn_convert_user_story_json_to_markdown(lv_json):
|
246 |
+
"""Convert User Story JSON to Markdown"""
|
247 |
+
|
248 |
+
lv_markdown = ""
|
249 |
+
try:
|
250 |
+
# Convert the dictionary to Markdown format
|
251 |
+
lv_markdown = f"# {lv_json['title']}\n\n"
|
252 |
+
lv_markdown += f"**Role:** {lv_json['role']}\n\n"
|
253 |
+
lv_markdown += f"**Feature:** {lv_json['feature']}\n\n"
|
254 |
+
lv_markdown += f"**Benefit:** {lv_json['benefit']}\n\n"
|
255 |
+
lv_markdown += "## User Story Scenarios\n"
|
256 |
+
|
257 |
+
for lv_scenario in lv_json['user_story_scenarios']:
|
258 |
+
lv_markdown += f"### {lv_scenario['scenario_title']}\n\n"
|
259 |
+
lv_markdown += f"**Pre-conditions:** {lv_scenario['pre_conditions']}\n\n"
|
260 |
+
lv_markdown += f"**Action Details:** {lv_scenario['action_details']}\n\n"
|
261 |
+
lv_markdown += f"**Expected Outcome:** {lv_scenario['expected_outcome']}\n\n"
|
262 |
+
except Exception as e:
|
263 |
+
print("UserStory - Error converting JSON to Markdown",str(e))
|
264 |
+
|
265 |
+
return lv_markdown
|
266 |
+
|
267 |
+
# Function to convert release notes JSON to Markdown
|
268 |
+
def fn_convert_release_notes_json_to_markdown(lv_json):
|
269 |
+
"""Convert Release Notes JSON to Markdown"""
|
270 |
+
|
271 |
+
lv_markdown = ""
|
272 |
+
try:
|
273 |
+
# Convert the dictionary to Markdown format
|
274 |
+
lv_markdown = f"# Release Notes\n\n"
|
275 |
+
lv_markdown += f"**Release Date:** {lv_json['release_date']}\n\n"
|
276 |
+
lv_markdown += f"**Product Name:** {lv_json['product_name']}\n\n"
|
277 |
+
lv_markdown += f"**Summary:** {lv_json['summary']}\n\n"
|
278 |
+
lv_markdown += "## Enhancements\n"
|
279 |
+
|
280 |
+
for lv_enhancement in lv_json['enhancements']:
|
281 |
+
lv_markdown += f"### {lv_enhancement['title']}\n\n"
|
282 |
+
lv_markdown += f"**Description:** {lv_enhancement['description']}\n\n"
|
283 |
+
lv_markdown += f"**Benefits:** {lv_enhancement['benefits']}\n\n"
|
284 |
+
lv_markdown += f"**Reason:** {lv_enhancement['reason']}\n\n"
|
285 |
+
except Exception as e:
|
286 |
+
print("ReleaseNotes - Error converting JSON to Markdown",str(e))
|
287 |
+
|
288 |
+
return lv_markdown
|
289 |
+
|
290 |
+
# Function to convert requirement generation JSON to Markdown
|
291 |
+
def fn_convert_requirement_generation_json_to_markdown(lv_json):
|
292 |
+
"""Convert Requirement Generation JSON to Markdown"""
|
293 |
+
|
294 |
+
lv_markdown = ""
|
295 |
+
try:
|
296 |
+
# Convert the dictionary to Markdown format
|
297 |
+
lv_markdown = f"# {lv_json['header']}\n\n"
|
298 |
+
lv_markdown += "## Requirements\n"
|
299 |
+
|
300 |
+
for requirement in lv_json['requirements']:
|
301 |
+
lv_markdown += f"### {requirement['overview']}\n\n"
|
302 |
+
lv_markdown += f"**Description:** {requirement['description']}\n\n"
|
303 |
+
lv_markdown += f"**Benefits:** {requirement['benefits']}\n\n"
|
304 |
+
lv_markdown += f"**Reason:** {requirement['reason']}\n\n"
|
305 |
+
lv_markdown += f"**Priority:** {requirement['priority']}\n\n"
|
306 |
+
if requirement['tags']:
|
307 |
+
tags = ', '.join(requirement['tags'])
|
308 |
+
lv_markdown += f"**Tags:** {tags}\n\n"
|
309 |
+
except Exception as e:
|
310 |
+
print("Requirement Gathering - Error converting JSON to Markdown",str(e))
|
311 |
+
|
312 |
+
return lv_markdown
|
313 |
+
|
314 |
+
# Main Program
|
315 |
+
def main():
|
316 |
+
|
317 |
+
# -- Streamlit Settings
|
318 |
+
st.set_page_config(
|
319 |
+
page_title="OBMA AI Assist",
|
320 |
+
page_icon="🧊",
|
321 |
+
layout="wide",
|
322 |
+
initial_sidebar_state="expanded"
|
323 |
+
)
|
324 |
+
|
325 |
+
# -- Display Processing Details
|
326 |
+
col1, col2, col3 = st.columns(3)
|
327 |
+
ui_processing_message = col2.empty()
|
328 |
+
ui_search_web_input =st.empty()
|
329 |
+
if "lv_model_session" in st.session_state:
|
330 |
+
lv_model = st.session_state.lv_model_session
|
331 |
+
else:
|
332 |
+
lv_model= None
|
333 |
+
|
334 |
+
global lv_response
|
335 |
+
global lv_extracted_text
|
336 |
+
|
337 |
+
col2.text("")
|
338 |
+
|
339 |
+
col2.header("OBMA - AI Assist")
|
340 |
+
col2.text("")
|
341 |
+
col2.text("")
|
342 |
+
col2.text("")
|
343 |
+
|
344 |
+
# -- Variables
|
345 |
+
cn_llm_providers_lov_values = ['Huggingface','Groq','Cohere','Google','Google VertexAI','OpenAI']
|
346 |
+
cn_huggingface_models_lov_values = ['meta-llama/Meta-Llama-3-70B-Instruct','CohereForAI/c4ai-command-r-plus','mistralai/Mistral-7B-Instruct-v0.2','microsoft/Phi-3-mini-128k-instruct','google/gemma-7b']
|
347 |
+
lv_user_actions = ["User Story","Release Notes","Requirement Generation","Summarization"]
|
348 |
+
|
349 |
+
# -- Configuration
|
350 |
+
with st.sidebar:
|
351 |
+
st.header("Configurations")
|
352 |
+
st.text("")
|
353 |
+
|
354 |
+
# -- Recording User Output
|
355 |
+
st.subheader("Output")
|
356 |
+
ui_user_actions = st.multiselect(
|
357 |
+
label='User Actions',
|
358 |
+
options=lv_user_actions,
|
359 |
+
default="User Story"
|
360 |
+
)
|
361 |
+
ui_show_json = st.toggle("Show JSON", value=False)
|
362 |
+
st.text("")
|
363 |
+
|
364 |
+
# -- Recording Proxy Details
|
365 |
+
try:
|
366 |
+
st.subheader("HTTP Proxy")
|
367 |
+
ui_proxy_url = st.text_input("URL")
|
368 |
+
ui_no_proxy_url = st.text_input("No Proxy URL",value="localhost,127.0.0.1")
|
369 |
+
col1, col2, col3 = st.columns([0.60,0.85,1.40])
|
370 |
+
with col1:
|
371 |
+
if st.button("Set"):
|
372 |
+
fn_set_proxy(ui_proxy_url,ui_no_proxy_url)
|
373 |
+
with col2:
|
374 |
+
if st.button("Unset"):
|
375 |
+
fn_unset_proxy()
|
376 |
+
except Exception as e:
|
377 |
+
st.error('Error Configuring LLM Details',str(e))
|
378 |
+
fn_display_user_messages("Error updating proxy details","Error", ui_processing_message)
|
379 |
+
|
380 |
+
# -- Read LLM Configuration
|
381 |
+
st.text("")
|
382 |
+
try:
|
383 |
+
st.subheader("LLM")
|
384 |
+
ui_llm_provider = st.selectbox(label='LLM Provider',options=cn_llm_providers_lov_values)
|
385 |
+
ui_api_key = st.empty()
|
386 |
+
ui_model_details = st.empty()
|
387 |
+
|
388 |
+
if ui_llm_provider:
|
389 |
+
# -- Prepopulated Configuration Details, Comment in production
|
390 |
+
if ui_llm_provider == 'Huggingface':
|
391 |
+
ui_api_key = st.text_input("HUGGINGFACEHUB_API_TOKEN",type="password")
|
392 |
+
ui_model_details = st.selectbox("Model Details",options=cn_huggingface_models_lov_values)
|
393 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = ui_api_key
|
394 |
+
elif(ui_llm_provider == 'Groq'):
|
395 |
+
ui_api_key = st.text_input("GROQ_API_KEY",type="password")
|
396 |
+
ui_model_details = st.text_input("Model Details","mixtral-8x7b-32768")
|
397 |
+
os.environ["GROQ_API_KEY"] = ui_api_key
|
398 |
+
elif(ui_llm_provider == 'Cohere'):
|
399 |
+
ui_api_key = st.text_input("COHERE_API_KEY", type="password")
|
400 |
+
ui_model_details = st.text_input("Model Details","command-r-plus")
|
401 |
+
os.environ["COHERE_API_KEY"] = ui_api_key
|
402 |
+
elif(ui_llm_provider == 'Google'):
|
403 |
+
ui_api_key = st.text_input("GOOGLE_API_KEY","AIzaSyAsksUKYnB4SuDNT6rB3d2Qd2hVk_TA5AA",type="password")
|
404 |
+
ui_model_details = st.text_input("Model Details","gemini-1.5-pro-latest")
|
405 |
+
os.environ["GOOGLE_API_KEY"] = ui_api_key
|
406 |
+
elif(ui_llm_provider == 'Google VertexAI'):
|
407 |
+
ui_api_key = st.text_area("GOOGLE_APPLICATION_CREDENTIALS")
|
408 |
+
ui_model_details = st.text_input("Model Details","gemini-1.5-pro-preview-0409")
|
409 |
+
elif(ui_llm_provider == 'OpenAI'):
|
410 |
+
ui_api_key = st.text_input("OPENAI_API_KEY", type="password")
|
411 |
+
ui_model_details = st.text_input("Model Details","gpt-4o")
|
412 |
+
os.environ["OPENAI_API_KEY"] = ui_api_key
|
413 |
+
else:
|
414 |
+
st.error('Please configure LLM Details')
|
415 |
+
fn_display_user_messages("Please configure LLM Details","Error", ui_processing_message)
|
416 |
+
|
417 |
+
if st.button("Configure LLM"):
|
418 |
+
# -- Create LLM Instance
|
419 |
+
if ui_llm_provider and ui_api_key and ui_model_details:
|
420 |
+
print("Configuring LLM")
|
421 |
+
lv_model = fn_create_chatllm(ui_llm_provider, ui_api_key, ui_model_details)
|
422 |
+
st.session_state.lv_model_session = lv_model
|
423 |
+
else:
|
424 |
+
st.error('Please configure LLM Details')
|
425 |
+
fn_display_user_messages("Please configure LLM Details","Error", ui_processing_message)
|
426 |
+
except Exception as e:
|
427 |
+
st.error('Error Configuring LLM Details'+str(e))
|
428 |
+
fn_display_user_messages("Error Configuring LLM Details","Error", ui_processing_message)
|
429 |
+
|
430 |
+
# -- Recording Knowledge Base Details
|
431 |
+
st.text("")
|
432 |
+
try:
|
433 |
+
st.subheader("Knowledge Base")
|
434 |
+
ui_grounding_url = st.text_input("Grounding URL")
|
435 |
+
ui_youtube_url = st.text_input("Youtube Video ID")
|
436 |
+
ui_grounding_pdf = st.file_uploader("Grounding PDF",type="pdf",accept_multiple_files=False)
|
437 |
+
ui_grounding_wav = st.file_uploader("Grounding WAV",type="wav",accept_multiple_files=False)
|
438 |
+
ui_search_web = st.checkbox("Search Web")
|
439 |
+
if ui_search_web:
|
440 |
+
ui_search_web_input = st.text_input("Search Details")
|
441 |
+
|
442 |
+
col1, col2, col3 = st.columns([0.85,0.80,1.40])
|
443 |
+
|
444 |
+
if col1.button("Extract"):
|
445 |
+
|
446 |
+
if ui_youtube_url:
|
447 |
+
lv_extracted_text +=fn_you_tube_video_transcript(ui_youtube_url,ui_processing_message)
|
448 |
+
|
449 |
+
if ui_grounding_url:
|
450 |
+
lv_extracted_text += ' '.join(doc.page_content for doc in fn_scrape_website(ui_grounding_url))
|
451 |
+
|
452 |
+
if ui_grounding_pdf:
|
453 |
+
lv_extracted_text += ' '.join(doc.page_content for doc in fn_scraper_pdf(ui_grounding_pdf))
|
454 |
+
|
455 |
+
if ui_search_web:
|
456 |
+
if ui_search_web_input:
|
457 |
+
lv_extracted_text += fn_search_web(ui_search_web_input)
|
458 |
+
|
459 |
+
if ui_grounding_wav:
|
460 |
+
lv_extracted_text += fn_generate_speech_to_text(ui_grounding_wav.getvalue(),ui_api_key)
|
461 |
+
|
462 |
+
st.session_state.lv_extracted_text = lv_extracted_text
|
463 |
+
|
464 |
+
if col2.button("Clear"):
|
465 |
+
lv_extracted_text = ""
|
466 |
+
st.session_state.lv_extracted_text = lv_extracted_text
|
467 |
+
lv_response = ""
|
468 |
+
st.session_state.lv_response = lv_response
|
469 |
+
except Exception as e:
|
470 |
+
st.error('Error extracting data - '+str(e))
|
471 |
+
fn_display_user_messages("Error extracting data","Error", ui_processing_message)
|
472 |
+
|
473 |
+
# -- User Actions
|
474 |
+
user_story, release_notes, requirement_generation, summarization = st.tabs(lv_user_actions)
|
475 |
+
|
476 |
+
with user_story:
|
477 |
+
# -- Generate User Story LLM Response
|
478 |
+
if ui_llm_provider and lv_extracted_text and not(lv_response) and "User Story" in ui_user_actions:
|
479 |
+
# -- Pydantice Schema
|
480 |
+
lv_parser = PydanticOutputParser(pydantic_object=us.UserStory)
|
481 |
+
|
482 |
+
# -- Creating Prompt
|
483 |
+
lv_template = pt.CN_USER_STORY
|
484 |
+
lv_summarize_prompt = PromptTemplate(
|
485 |
+
template=lv_template,
|
486 |
+
input_variables=["context"],
|
487 |
+
partial_variables={"format_instructions": lv_parser.get_format_instructions()},
|
488 |
+
)
|
489 |
+
lv_summarize_prompt_formatted = lv_summarize_prompt.format(
|
490 |
+
context=lv_extracted_text
|
491 |
+
)
|
492 |
+
|
493 |
+
# -- LLM Response
|
494 |
+
if lv_model:
|
495 |
+
lv_response = fn_chatllm_response(ui_llm_provider, lv_summarize_prompt_formatted, lv_model, ui_processing_message)
|
496 |
+
st.session_state.lv_response = lv_response
|
497 |
+
|
498 |
+
# -- Display LLM response
|
499 |
+
if lv_response and "User Story" in ui_user_actions:
|
500 |
+
lv_repaired = repair_json(lv_response, skip_json_loads=True)
|
501 |
+
|
502 |
+
if ui_show_json:
|
503 |
+
st.header("User Story")
|
504 |
+
st.json(lv_repaired)
|
505 |
+
else:
|
506 |
+
lv_markdown = fn_convert_user_story_json_to_markdown(json.loads(lv_repaired))
|
507 |
+
st.markdown(lv_markdown)
|
508 |
+
# st.json(lv_response)
|
509 |
+
|
510 |
+
with release_notes:
|
511 |
+
if ui_llm_provider and lv_extracted_text and not(lv_response) and "Release Notes" in ui_user_actions:
|
512 |
+
# -- Pydantice Schema
|
513 |
+
lv_parser = PydanticOutputParser(pydantic_object=rs.ReleaseNotes)
|
514 |
+
|
515 |
+
# -- Creating Prompt
|
516 |
+
lv_template = pt.CN_RELEASE_NOTES
|
517 |
+
lv_summarize_prompt = PromptTemplate(
|
518 |
+
template=lv_template,
|
519 |
+
input_variables=["context"],
|
520 |
+
partial_variables={"format_instructions": lv_parser.get_format_instructions()},
|
521 |
+
)
|
522 |
+
lv_summarize_prompt_formatted = lv_summarize_prompt.format(
|
523 |
+
context=lv_extracted_text
|
524 |
+
)
|
525 |
+
|
526 |
+
# -- LLM Response
|
527 |
+
if lv_model:
|
528 |
+
lv_response = fn_chatllm_response(ui_llm_provider, lv_summarize_prompt_formatted, lv_model, ui_processing_message)
|
529 |
+
st.session_state.lv_response = lv_response
|
530 |
+
|
531 |
+
# -- Display LLM response
|
532 |
+
if lv_response and "Release Notes" in ui_user_actions:
|
533 |
+
lv_repaired = repair_json(lv_response, skip_json_loads=True)
|
534 |
+
if ui_show_json:
|
535 |
+
st.header("Release Notes")
|
536 |
+
st.json(lv_repaired)
|
537 |
+
else:
|
538 |
+
lv_markdown = fn_convert_release_notes_json_to_markdown(json.loads(lv_repaired))
|
539 |
+
st.markdown(lv_markdown)
|
540 |
+
|
541 |
+
with requirement_generation:
|
542 |
+
if ui_llm_provider and lv_extracted_text and not(lv_response) and "Requirement Generation" in ui_user_actions:
|
543 |
+
# -- Pydantice Schema
|
544 |
+
lv_parser = PydanticOutputParser(pydantic_object=rq.RequirementGatheringDetails)
|
545 |
+
|
546 |
+
# -- Creating Prompt
|
547 |
+
lv_template = pt.CN_REQUIREMENT_GATHERING
|
548 |
+
lv_summarize_prompt = PromptTemplate(
|
549 |
+
template=lv_template,
|
550 |
+
input_variables=["context"],
|
551 |
+
partial_variables={"format_instructions": lv_parser.get_format_instructions()},
|
552 |
+
)
|
553 |
+
lv_summarize_prompt_formatted = lv_summarize_prompt.format(
|
554 |
+
context=lv_extracted_text
|
555 |
+
)
|
556 |
+
|
557 |
+
# -- LLM Response
|
558 |
+
if lv_model:
|
559 |
+
lv_response = fn_chatllm_response(ui_llm_provider, lv_summarize_prompt_formatted, lv_model, ui_processing_message)
|
560 |
+
st.session_state.lv_response = lv_response
|
561 |
+
|
562 |
+
# -- Display LLM response
|
563 |
+
if lv_response and "Requirement Generation" in ui_user_actions:
|
564 |
+
lv_repaired = repair_json(lv_response, skip_json_loads=True)
|
565 |
+
|
566 |
+
if ui_show_json:
|
567 |
+
st.header("Requirement Generation")
|
568 |
+
st.json(lv_repaired)
|
569 |
+
else:
|
570 |
+
lv_markdown = fn_convert_requirement_generation_json_to_markdown(json.loads(lv_repaired))
|
571 |
+
st.markdown(lv_markdown)
|
572 |
+
|
573 |
+
with summarization:
|
574 |
+
if ui_llm_provider and "Summarization" in ui_user_actions:
|
575 |
+
st.header("Summarization")
|
576 |
+
st.text("")
|
577 |
+
st.text("")
|
578 |
+
|
579 |
+
ui_summary_input = st.text_area("Input Text", value=lv_extracted_text)
|
580 |
+
if st.button("Summarize",key="summary"):
|
581 |
+
# -- Creating Prompt
|
582 |
+
lv_template = pt.CN_SUMMARY
|
583 |
+
lv_summarize_prompt = PromptTemplate(
|
584 |
+
template=lv_template,
|
585 |
+
input_variables=["context"]
|
586 |
+
)
|
587 |
+
lv_summarize_prompt_formatted = lv_summarize_prompt.format(
|
588 |
+
context=ui_summary_input
|
589 |
+
)
|
590 |
+
|
591 |
+
# -- LLM Response
|
592 |
+
if lv_model:
|
593 |
+
lv_response = fn_chatllm_response(ui_llm_provider, lv_summarize_prompt_formatted, lv_model, ui_processing_message)
|
594 |
+
st.session_state.lv_response = lv_response
|
595 |
+
|
596 |
+
# -- Display LLM response
|
597 |
+
if lv_response:
|
598 |
+
st.subheader("Summary")
|
599 |
+
st.markdown(lv_response)
|
600 |
+
|
601 |
+
# Loading Main
|
602 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
603 |
main()
|