File size: 1,863 Bytes
af1cf81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbf4ec6
af1cf81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbf4ec6
af1cf81
 
 
bbf4ec6
af1cf81
bbf4ec6
af1cf81
 
bbf4ec6
af1cf81
 
 
 
 
 
 
 
 
b004ff1
3e8bc94
af1cf81
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57

from json import JSONDecodeError
import logging
from markdown import markdown
import requests

import streamlit as st

from utils.haystack import query, start_haystack
from utils.ui import reset_results, set_initial_state, sidebar

set_initial_state()

sidebar()

st.write("# 🐀 What have they been posting about lately on Mastodon?")

if st.session_state.get("HUGGING_FACE_TOKEN"):
    pipeline = start_haystack(st.session_state.get("HUGGING_FACE_TOKEN"))
    st.session_state["api_key_configured"] = True
    search_bar, button = st.columns(2)
    # Search bar
    with search_bar: 
        question = st.text_input("Ask a question", on_change=reset_results)

    with button: 
        st.write("")
        st.write("")
        run_pressed = st.button("Search posts (toots)")
else:
    st.write("Please provide your OpenAI Key to start using the application")
    st.write("If you are using a smaller screen, open the sidebar from the top left to provide your OpenAI Key πŸ™Œ")
    
if st.session_state.get("api_key_configured"):
    run_query = (
        run_pressed or question != st.session_state.question
    )

    # Get results for query
    if run_query and question:
        reset_results()
        st.session_state.question = question
        with st.spinner("πŸ”Ž"):
            try:
                st.session_state.result = query(question, pipeline)
            except JSONDecodeError as je:
                st.error(
                    "πŸ‘“    An error occurred reading the results. Is the document store working?"
                )    
            except Exception as e:
                logging.exception(e)
                st.error("🐞    An error occurred during the request.")            
                
    if st.session_state.result:
        reply = st.session_state.result
        st.write(reply[0])