Rajagopal commited on
Commit
d7ff885
·
1 Parent(s): f9801a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -83
app.py CHANGED
@@ -1,121 +1,127 @@
1
- import streamlit as st
2
- from langchain import OpenAI, PromptTemplate, LLMChain
3
- from langchain.text_splitter import CharacterTextSplitter
4
- from langchain.chains.mapreduce import MapReduceChain
5
- from langchain.prompts import PromptTemplate
6
- from langchain.chat_models import AzureChatOpenAI
7
- from langchain.chains.summarize import load_summarize_chain
8
- from langchain.chains import AnalyzeDocumentChain
9
- from PyPDF2 import PdfReader
10
  from langchain.document_loaders import TextLoader
11
- from langchain.indexes import VectorstoreIndexCreator
12
  from langchain.document_loaders import PyPDFLoader
13
- import os
14
- import openai
 
 
 
 
 
 
 
15
 
 
 
16
 
17
- import os
18
 
 
19
 
20
- os.environ["OPENAI_API_TYPE"] = "azure"
21
- os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview"
22
 
23
- openai.api_type = "azure"
24
- openai.api_base = "https://embeddinguseopenai.openai.azure.com/"
25
- openai.api_version = "2023-03-15-preview"
26
- openai.api_key = os.environ["OPENAI_API_KEY"]
27
-
28
 
 
29
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
 
31
 
32
- st.title("Wipro demo with azure cognitive 2 ")
33
 
34
 
35
- atemprature = st.slider('Fact vs Creative?', 0, 10, 1)
36
- atemprature = atemprature / 10.0
37
 
 
 
38
 
39
- yourquestion = st.text_input('Your Question', 'First identify the indicators required as per EFRAG Environmental document. List these indicators. For each of these indicators, find out how Wipro is performing.')
40
- st.write('Your input is ', yourquestion)
 
41
 
42
 
43
 
44
 
 
 
45
 
46
- if st.button("Ask Questions "):
47
- template = """
48
- You are an AI assistant.
49
- {concept}
50
- """
51
 
52
- response = openai.ChatCompletion.create(
53
- engine="gpt-35-turbo",
54
- messages = [{"role":"system","content":"You are an AI assistant that helps people find information."},{"role":"user","content":yourquestion}],
55
- temperature=atemprature,
56
- max_tokens=800,
57
- top_p=1,
58
- frequency_penalty=0,
59
- presence_penalty=0,
60
- stop=None)
61
 
62
- # Run the chain only specifying the input variable.
63
- st.write(response)
 
 
 
64
 
 
65
 
66
- if st.button("Ask Questions Simplify "):
67
- template = """
68
- You are an AI assistant.
69
- {concept}
70
- """
71
 
72
- response = openai.ChatCompletion.create(
73
- engine="gpt-35-turbo",
74
- messages = [{"role":"system","content":"You are an AI assistant that helps people find information. Please explain the information like i am a five."},{"role":"user","content":yourquestion}],
75
- temperature=0,
76
- max_tokens=800,
77
- top_p=1,
78
- frequency_penalty=0,
79
- presence_penalty=0,
80
- stop=None)
81
 
82
- # Run the chain only specifying the input variable.
83
- st.write(response)
84
 
85
- if st.button("Ask trying here "):
86
- template = """
87
- You are an expert on topics of Sustainability, Climate action and UN Sustainable Development Goals.
88
- Explain the concept of {concept} like i am a five
89
- """
 
90
 
91
- prompt = PromptTemplate(
92
- input_variables=["concept"],
93
- template=template,
94
- )
 
 
 
 
 
 
 
 
 
95
 
96
 
97
- from langchain.chains import LLMChain
98
- chain = LLMChain(llm=llm, prompt=prompt)
 
99
 
100
- # Run the chain only specifying the input variable.
101
- st.write(chain.run(yourquestion))
102
 
 
 
 
 
 
 
 
 
103
 
104
 
105
- if st.button("Ask Hindi "):
106
- template = """
107
- You are an expert on topics of Sustainability, Climate action and UN Sustainable Development Goals.
108
- Explain the concept of {concept} in Hindi
109
- """
110
 
111
- prompt = PromptTemplate(
112
- input_variables=["concept"],
113
- template=template,
114
- )
115
 
116
 
117
- from langchain.chains import LLMChain
118
- chain = LLMChain(llm=llm, prompt=prompt)
119
 
120
- # Run the chain only specifying the input variable.
121
- st.write(chain.run(yourquestion))
 
1
+ %%writefile app.py
2
+ import os
3
+ from langchain.chains import RetrievalQA
4
+ from langchain.llms import AzureOpenAI
 
 
 
 
 
5
  from langchain.document_loaders import TextLoader
 
6
  from langchain.document_loaders import PyPDFLoader
7
+ from langchain.indexes import VectorstoreIndexCreator
8
+ from langchain.text_splitter import CharacterTextSplitter
9
+ from langchain.embeddings import OpenAIEmbeddings
10
+ from langchain.vectorstores import Chroma
11
+ from langchain.chains.question_answering import load_qa_chain
12
+ from langchain.llms import AzureOpenAI
13
+ from langchain.chains.question_answering import load_qa_chain
14
+ import streamlit as st
15
+ from PIL import Image
16
 
17
+ #image = Image.open('https://www.wipro.com/content/dam/nexus/en/brand/images/secondary-logo-400x276.png')
18
+ #st.image(image, caption='Wipro impact logo')
19
 
20
+ st.title("Wipro Impact | CSRD | Advisor")
21
 
22
+ st.header("Enable clients to prepare for CSRD.. ")
23
 
 
 
24
 
25
+ genre = st.radio(
26
+ "Choose a CSRD focus area for AI question answering",
27
+ ('E1-Climate Change', 'E4-Biodiversity and ecosystem', 'S1-Own Workforce'), index=0)
 
 
28
 
29
+ url = "https://www.efrag.org/Assets/Download?assetUrl=%2Fsites%2Fwebpublishing%2FSiteAssets%2F13%2520Draft%2520ESRS%2520S1%2520Own%2520workforce%2520November%25202022.pdf"
30
 
31
+ if genre == 'E1-Climate Change':
32
+ url = "https://www.efrag.org/Assets/Download?assetUrl=%2Fsites%2Fwebpublishing%2FSiteAssets%2F08%2520Draft%2520ESRS%2520E1%2520Climate%2520Change%2520November%25202022.pdf"
33
+ samplequestions = ["What are the climate related indicators?", "List all the disclosure requirments listed in page 3?", "Policies related to climate change mitigation and adaptation?",
34
+ "What should the company disclose regarding Actions and resources in relation to climate change policies?" , "How should the GHG emission reduction targets be reported?" ,
35
+ "Total energy consumption from non-renewable sources for high climate impact sectors should be disaggregated as ?",
36
+ "Total energy consumption from renewable sources should be disaggregated as ?" ,
37
+ "What should be disclosed on GHG removals and GHG mitigation projects financed through carbon credits ?" ,
38
+ "Is it wise to invest on carbon credits? ",
39
+ "What is Climate change adaptation? ", "What are Decarbonisation levers?" ]
40
 
41
+ if genre == 'E4-Biodiversity and ecosystem':
42
+ url = "https://www.efrag.org/Assets/Download?assetUrl=%2Fsites%2Fwebpublishing%2FSiteAssets%2F11%2520Draft%2520ESRS%2520E4%2520Biodiversity%2520and%2520ecosystems%2520November%25202022.pdf"
43
+ samplequestions = ["What are the Biodiversity related indicators?", "List all the disclosure requirments listed in page 3?"]
44
+ if genre == 'S1-Own Workforce':
45
+ url = "https://www.efrag.org/Assets/Download?assetUrl=%2Fsites%2Fwebpublishing%2FSiteAssets%2F13%2520Draft%2520ESRS%2520S1%2520Own%2520workforce%2520November%25202022.pdf"
46
+ samplequestions = ["What are the social related indicators?", "List all the disclosure requirments listed in page 3?"]
47
 
 
48
 
49
 
 
 
50
 
51
+ sampleselectedquestion = st.selectbox(
52
+ 'Just ask your question or start with a one of these example questions...', samplequestions )
53
 
54
+ st.write(" :green[ Ask any thing on your mind...just type your question here...]")
55
+ yourquestion = st.text_input('Your question', sampleselectedquestion)
56
+ st.write('Your typed .. ', yourquestion)
57
 
58
 
59
 
60
 
61
+ os.environ['OPENAI_API_TYPE'] = 'azure'
62
+ os.environ['OPENAI_API_VERSION'] = '2023-03-15-preview'
63
 
64
+ llmgpt3 = AzureOpenAI( deployment_name="testdavanci", model_name="text-davinci-003" )
65
+ chain = load_qa_chain(llm=llmgpt3, chain_type="map_reduce")
 
 
 
66
 
67
+ aimethod = st.radio(
68
+ "Choose a AI brain or document comprehension method",
69
+ ('2 minutes AI method map_reduce', '4 minutes AI method refine' ), index=0)
 
 
 
 
 
 
70
 
71
+ mychain_type = "map_reduce"
72
+ if aimethod == '2 minutes AI method map_reduce':
73
+ mychain_type = "map_reduce"
74
+ if aimethod == '4 minutes AI method refine':
75
+ mychain_type = "refine"
76
 
77
+ chain = load_qa_chain(llm=llmgpt3, chain_type=mychain_type)
78
 
 
 
 
 
 
79
 
80
+ loader1 = PyPDFLoader(url)
 
 
 
 
 
 
 
 
81
 
 
 
82
 
83
+ def history():
84
+ mycount = 0
85
+ if 'count' not in st.session_state:
86
+ st.session_state['count'] = 0
87
+ else:
88
+ mycount = st.session_state['count']
89
 
90
+ if True:
91
+ st.write(mycount)
92
+ mycount = mycount + 1
93
+ st.session_state['count'] = mycount
94
+ for i in range(mycount):
95
+ mystatekeyindex = "element" + str(i)
96
+ mystatekeyanswerindex = "elementANS" + str(i)
97
+ if mystatekeyindex not in st.session_state:
98
+ st.session_state[mystatekeyindex] = yourquestion
99
+ st.session_state[mystatekeyanswerindex] = answer
100
+ if mystatekeyindex in st.session_state:
101
+ with st.expander(st.session_state[mystatekeyindex]):
102
+ st.write( st.session_state[mystatekeyanswerindex] )
103
 
104
 
105
+ def colorizedtext(acolor, astring):
106
+ formattedcolor = ":" + acolor + "[" + astring + "]"
107
+ return formattedcolor
108
 
 
 
109
 
110
+ if st.button("Ask QA "):
111
+ documents = loader1.load()
112
+ answer = ""
113
+ with st.spinner(" Finding answer for your question " ):
114
+ st.write("AI is reading this [link](%s)" % url)
115
+ with st.expander( "Employing your choice of AI method ... " + aimethod + "..."):
116
+ st.write(str(chain)[:100])
117
+ st.subheader(colorizedtext("red", yourquestion))
118
 
119
 
120
+ answer = chain.run(input_documents=documents, question=yourquestion)
121
+ st.subheader(colorizedtext("blue", answer))
122
+ history()
 
 
123
 
 
 
 
 
124
 
125
 
 
 
126
 
127
+