File size: 1,544 Bytes
7bf9eb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import streamlit as st
from dotenv import load_dotenv
import requests
import os
import pyperclip

load_dotenv()

def summarize(article):
    headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ os.getenv("API_KEY")), 'azureml-model-deployment': 'heute-summary-api' }
    data = {'article': article}
    try:
        with st.spinner("Summarizing the article..."):
            response = requests.post(os.getenv("API_URL"), headers=headers, json=data)
            article_summary = response.json()
            return article_summary["summary"]
    except Exception as e:
        print(e)
        st.error("An error occurred while trying to summarize the article. Please try again later.", icon="🚨")
        return ""

def summary_btn_handler():
    summary = summarize(st.session_state["article"])
    st.session_state["summary"] = summary
    
if "summary" not in st.session_state:
    st.session_state["summary"] = ""

col1, col2 = st.columns([2, 1])

col1.title("AI - Summarizer")
col2.image("tensora_logo.png")

st.text_area("Enter your article to summarize", height=200, key="article")
st.button("Summarize", key="summarize_btn", on_click=summary_btn_handler, disabled=not st.session_state["article"])
st.write(st.session_state["summary"])
if len(st.session_state["summary"]) > 0:
    copy_col1, copy_col2 = st.columns([2, 1])
    if copy_col1.button("Copy to clipboard", key="copy_btn"):
        pyperclip.copy(st.session_state["summary"])
        with copy_col2:
            st.success('Text copied successfully!')