Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| from langchain import PromptTemplate | |
| from langchain.chat_models import AzureChatOpenAI | |
| import tiktoken | |
| from langchain.docstore.document import Document | |
| from langchain.text_splitter import CharacterTextSplitter | |
| from langchain.chains.summarize import load_summarize_chain | |
| from langchain.chains import AnalyzeDocumentChain | |
| os.environ["OPENAI_API_TYPE"] = "azure" | |
| os.environ["OPENAI_API_VERSION"] = "2023-05-15" | |
| os.environ["OPENAI_API_BASE"] = "https://cog-mnjbf5r4o6b3e.openai.azure.com/" | |
| os.environ["OPENAI_API_KEY"] = "957f7d98b47a467a98a786f7ca903112" | |
| def generate_response(txt): | |
| # Instantiate the LLM model | |
| llm = AzureChatOpenAI(temperature=0, deployment_name='gpt-4-32k', openai_api_version="2023-03-15-preview") | |
| # Split text | |
| text_splitter = CharacterTextSplitter() | |
| texts = text_splitter.split_text(txt) | |
| # Create multiple documents | |
| docs = [Document(page_content=t) for t in texts] | |
| # Text summarization | |
| prompt_template = """Write a structured report on the quality issues in the following text, are there any similarities across sites?: | |
| {text} | |
| Report:""" | |
| PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"]) | |
| 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) | |
| output = chain({"input_documents": docs}, return_only_outputs=True)['output_text'] | |
| return output | |
| # Page title | |
| st.set_page_config(page_title='Health Data Summarization App') | |
| st.title('quality issues') | |
| # Text input | |
| txt_input = st.text_area('Enter your quality data', '', height=200) | |
| # Form to accept user's text input for summarization | |
| result = [] | |
| with st.form('summarize_form', clear_on_submit=True): | |
| submitted = st.form_submit_button('Submit') | |
| if submitted: | |
| with st.spinner('Calculating...'): | |
| response = generate_response(txt_input) | |
| result.append(response) | |
| if len(result): | |
| st.info(response) |