code: #!/usr/bin/env python3
  
  import json
  import logging
  import re
  import requests
  
  import altair as alt
  import matplotlib.pyplot as plt
  import pandas as pd
  import streamlit as st
  
  from datetime import datetime, timedelta
  from langchain.llms import OpenAI
  from llama_index import GPTVectorStoreIndex, Document, LLMPredictor, ServiceContext
  from requests.exceptions import HTTPError
  from wordcloud import WordCloud
  
  
  logger = logging.getLogger("llama_index")
  logger.setLevel(logging.WARNING)
  
  TITLE = "Daily News Summary"
  ICON = "https://archive.org/favicon.ico"
  VISEXP = "https://storage.googleapis.com/data.gdeltproject.org/gdeltv3/iatv/visualexplorer"
  
  BGNDT = pd.to_datetime("2022-03-25").date()
  ENDDT = (datetime.now() - timedelta(hours=30)).date()
  
  CHANNELS = {
    "": "-- Select --",
    "ESPRESO": "Espreso TV",
    "RUSSIA1": "Russia-1",
    "RUSSIA24": "Russia-24",
    "1TV": "Channel One Russia",
    "NTV": "NTV",
    "BELARUSTV": "Belarus TV",
    "IRINN": "Islamic Republic of Iran News Network"
  }
  
  st.set_page_config(page_title=TITLE, page_icon=ICON, layout="centered", initial_sidebar_state="collapsed")
  st.title(TITLE)
  
  llm_predictor = LLMPredictor(llm=OpenAI(max_tokens=1024, model_name="text-davinci-003"))
  
  
  @st.cache_resource(show_spinner=False)
  def load_transcript(id, lg):
    lang = "" if lg == "Original" else ".en"
    r = requests.get(f"{VISEXP}/{id}.transcript{lang}.txt")
    r.raise_for_status()
    return r.content
  
  
  @st.cache_resource(show_spinner=False)
  def load_index(ch, dt, lg):
    r = requests.get(f"{VISEXP}/{ch}.{dt}.inventory.json")
    r.raise_for_status()
    shows = r.json()["shows"]
    idx = GPTVectorStoreIndex.from_documents([], service_context=ServiceContext.from_defaults(llm_predictor=llm_predictor))
    msg = f"Loading `{dt[:4]}-{dt[4:6]}-{dt[6:8]}` {lg} transcripts for `{CHANNELS.get(ch, 'selected')}` channel..."
    prog = st.progress(0.0, text=msg)
    for i, tr in enumerate(shows, start=1):
      try:
        idx.insert(Document(load_transcript(tr["id"], lg).decode("utf-8")), llm_predictor=llm_predictor)
      except HTTPError as e:
        pass
      prog.progress(i/len(shows), text=msg)
    prog.empty()
    return idx.as_query_engine()
  
  
  @st.cache_resource(show_spinner="Extracting top entities...")
  def get_top_entities(_idx, ch, dt, lg):
    res = _idx.query("20 most frequent entities with their frequency in these articles as key-value in JSON format")
    kw = json.loads(res.response.strip())
    wc = WordCloud(background_color="white")
    wc.generate_from_frequencies(kw)
    fig, ax = plt.subplots()
    ax.imshow(wc)
    ax.axis("off")
    return fig, pd.DataFrame(kw.items()).rename(columns={0: "Entity", 1: "Frequency"}).sort_values("Frequency", ascending=False)
  
  
  @st.cache_resource(show_spinner="Constructing news headlines...")
  def get_headlines(_idx, ch, dt, lg):
    return _idx.query("Top 10 news headlines with summary in these articles in Markdown format")
  
  
  qp = st.experimental_get_query_params()
  if "date" not in st.session_state and qp.get("date"):
      st.session_state["date"] = datetime.strptime(qp.get("date")[0], "%Y-%m-%d").date()
  if "chan" not in st.session_state and qp.get("chan"):
      st.session_state["chan"] = qp.get("chan")[0]
  if "lang" not in st.session_state and qp.get("lang"):
      st.session_state["lang"] = qp.get("lang")[0]
  
  cols = st.columns(3)
  dt = cols[0].date_input("Date", value=ENDDT, min_value=BGNDT, max_value=ENDDT, key="date").strftime("%Y%m%d")
  ch = cols[1].selectbox("Channel", CHANNELS, format_func=lambda x: CHANNELS.get(x, ""), key="chan")
  lg = cols[2].selectbox("Language", ["English", "Original"], format_func=lambda x: "English (Translation)" if x == "English" else x, key="lang", disabled=True) # Disabled due to a bug https://github.com/jerryjliu/gpt_index/issues/294
  
  if not ch:
    st.info(f"Select a channel to summarize for the selected day.")
    st.stop()
  
  st.experimental_set_query_params(**st.session_state)
  
  try:
    idx = load_index(ch, dt, lg)
  except HTTPError as e:
    st.warning(f"Transcripts for `{CHANNELS.get(ch, 'selected')}` channel are not available for `{dt[:4]}-{dt[4:6]}-{dt[6:8]}` yet, try selecting another date!", icon="⚠️")
    st.stop()
  
  tbs = st.tabs(["Top Entities", "Frequencies"])
  try:
    fig, d = get_top_entities(idx, ch, dt, lg)
    tbs[0].pyplot(fig)
    tbs[1].dataframe(d, use_container_width=True)
  except:
    msg = "Entity frequency data is not in the expected JSON shape!"
    tbs[0].warning(msg)
    tbs[1].warning(msg)
  
  
  "### Top Headlines"
  
  res = get_headlines(idx, ch, dt, lg)
  st.markdown(res.response)
  
apis: llama_index.ServiceContext.from_defaults
extract_api: [((419, 451), 'logging.getLogger', 'logging.getLogger', (['"""llama_index"""'], {}), "('llama_index')\n", (436, 451), False, 'import logging\n'), ((992, 1102), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': 'TITLE', 'page_icon': 'ICON', 'layout': '"""centered"""', 'initial_sidebar_state': '"""collapsed"""'}), "(page_title=TITLE, page_icon=ICON, layout='centered',\n    initial_sidebar_state='collapsed')\n", (1010, 1102), True, 'import streamlit as st\n'), ((1099, 1114), 'streamlit.title', 'st.title', (['TITLE'], {}), '(TITLE)\n', (1107, 1114), True, 'import streamlit as st\n'), ((1208, 1245), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (1225, 1245), True, 'import streamlit as st\n'), ((1421, 1458), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (1438, 1458), True, 'import streamlit as st\n'), ((2151, 2211), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '"""Extracting top entities..."""'}), "(show_spinner='Extracting top entities...')\n", (2168, 2211), True, 'import streamlit as st\n'), ((2675, 2739), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '"""Constructing news headlines..."""'}), "(show_spinner='Constructing news headlines...')\n", (2692, 2739), True, 'import streamlit as st\n'), ((2879, 2913), 'streamlit.experimental_get_query_params', 'st.experimental_get_query_params', ([], {}), '()\n', (2911, 2913), True, 'import streamlit as st\n'), ((3269, 3282), 'streamlit.columns', 'st.columns', (['(3)'], {}), '(3)\n', (3279, 3282), True, 'import streamlit as st\n'), ((3816, 3868), 'streamlit.experimental_set_query_params', 'st.experimental_set_query_params', ([], {}), '(**st.session_state)\n', (3848, 3868), True, 'import streamlit as st\n'), ((4119, 4159), 'streamlit.tabs', 'st.tabs', (["['Top Entities', 'Frequencies']"], {}), "(['Top Entities', 'Frequencies'])\n", (4126, 4159), True, 'import streamlit as st\n'), ((4458, 4483), 'streamlit.markdown', 'st.markdown', (['res.response'], {}), '(res.response)\n', (4469, 4483), True, 'import streamlit as st\n'), ((1324, 1375), 'requests.get', 'requests.get', (['f"""{VISEXP}/{id}.transcript{lang}.txt"""'], {}), "(f'{VISEXP}/{id}.transcript{lang}.txt')\n", (1336, 1375), False, 'import requests\n'), ((1493, 1543), 'requests.get', 'requests.get', (['f"""{VISEXP}/{ch}.{dt}.inventory.json"""'], {}), "(f'{VISEXP}/{ch}.{dt}.inventory.json')\n", (1505, 1543), False, 'import requests\n'), ((1841, 1867), 'streamlit.progress', 'st.progress', (['(0.0)'], {'text': 'msg'}), '(0.0, text=msg)\n', (1852, 1867), True, 'import streamlit as st\n'), ((2414, 2449), 'wordcloud.WordCloud', 'WordCloud', ([], {'background_color': '"""white"""'}), "(background_color='white')\n", (2423, 2449), False, 'from wordcloud import WordCloud\n'), ((2497, 2511), 'matplotlib.pyplot.subplots', 'plt.subplots', ([], {}), '()\n', (2509, 2511), True, 'import matplotlib.pyplot as plt\n'), ((3739, 3802), 'streamlit.info', 'st.info', (['f"""Select a channel to summarize for the selected day."""'], {}), "(f'Select a channel to summarize for the selected day.')\n", (3746, 3802), True, 'import streamlit as st\n'), ((3805, 3814), 'streamlit.stop', 'st.stop', ([], {}), '()\n', (3812, 3814), True, 'import streamlit as st\n'), ((657, 685), 'pandas.to_datetime', 'pd.to_datetime', (['"""2022-03-25"""'], {}), "('2022-03-25')\n", (671, 685), True, 'import pandas as pd\n'), ((1149, 1203), 'langchain.llms.OpenAI', 'OpenAI', ([], {'max_tokens': '(1024)', 'model_name': '"""text-davinci-003"""'}), "(max_tokens=1024, model_name='text-davinci-003')\n", (1155, 1203), False, 'from langchain.llms import OpenAI\n'), ((4102, 4111), 'streamlit.stop', 'st.stop', ([], {}), '()\n', (4109, 4111), True, 'import streamlit as st\n'), ((702, 716), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (714, 716), False, 'from datetime import datetime, timedelta\n'), ((719, 738), 'datetime.timedelta', 'timedelta', ([], {'hours': '(30)'}), '(hours=30)\n', (728, 738), False, 'from datetime import datetime, timedelta\n'), ((1658, 1715), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm_predictor': 'llm_predictor'}), '(llm_predictor=llm_predictor)\n', (1686, 1715), False, 'from llama_index import GPTVectorStoreIndex, Document, LLMPredictor, ServiceContext\n')]