cloud-sean commited on
Commit
b564660
·
1 Parent(s): 7974fef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from langchain import PromptTemplate
4
+ from langchain.chat_models import AzureChatOpenAI
5
+ import tiktoken
6
+ from langchain.docstore.document import Document
7
+ from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain.chains.summarize import load_summarize_chain
9
+ from langchain.chains import AnalyzeDocumentChain
10
+
11
+
12
+ os.environ["OPENAI_API_TYPE"] = "azure"
13
+ os.environ["OPENAI_API_VERSION"] = "2023-05-15"
14
+ os.environ["OPENAI_API_BASE"] = "https://cog-mnjbf5r4o6b3e.openai.azure.com/"
15
+ os.environ["OPENAI_API_KEY"] = "957f7d98b47a467a98a786f7ca903112"
16
+
17
+ def generate_response(txt):
18
+ # Instantiate the LLM model
19
+ llm = AzureChatOpenAI(temperature=0, deployment_name='gpt-4-32k', openai_api_version="2023-03-15-preview")
20
+ # Split text
21
+ text_splitter = CharacterTextSplitter()
22
+ texts = text_splitter.split_text(txt)
23
+ # Create multiple documents
24
+ docs = [Document(page_content=t) for t in texts]
25
+ # Text summarization
26
+ prompt_template = """Write a structured report on the quality issues in the following text, are there any similarities across sites?:
27
+
28
+
29
+ {text}
30
+
31
+
32
+ Report:"""
33
+ PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
34
+ chain = load_summarize_chain(AzureChatOpenAI(deployment_name="chat", temperature=0), chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT, verbose=True)
35
+ output = chain({"input_documents": docs}, return_only_outputs=True)['output_text']
36
+
37
+
38
+ return output
39
+
40
+ # Page title
41
+ st.set_page_config(page_title='Health Data Summarization App')
42
+ st.title('quality issues')
43
+
44
+ # Text input
45
+ txt_input = st.text_area('Enter your quality data', '', height=200)
46
+
47
+ # Form to accept user's text input for summarization
48
+ result = []
49
+ with st.form('summarize_form', clear_on_submit=True):
50
+ submitted = st.form_submit_button('Submit')
51
+ if submitted:
52
+ with st.spinner('Calculating...'):
53
+ response = generate_response(txt_input)
54
+ result.append(response)
55
+
56
+
57
+ if len(result):
58
+ st.info(response)