File size: 2,116 Bytes
c325552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from annotated_text import annotation
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
from utils.config import TWITTER_BEARER_TOKEN, SERPER_KEY, OPENAI_API_KEY

set_initial_state()

sidebar()

st.write("# πŸ‘© What would they have tweeted about this?")

# if st.session_state.get("OPENAI_API_KEY"):
if "last_k_tweets" not in st.session_state:
    st.session_state["last_k_tweets"] = st.slider("How many tweets should we retrieve?", value=10, step=10, max_value=100, min_value=10, on_change = reset_results)

agent = start_haystack(openai_key=OPENAI_API_KEY, twitter_bearer=TWITTER_BEARER_TOKEN, serper_key=SERPER_KEY, last_k_tweets=st.session_state["last_k_tweets"])
# st.session_state["api_keys_configured"] = True

# Search bar
question = st.text_input("If the twitter account tuanacelik were to write a tweet in their style about climate change, what would it be?", on_change=reset_results) 
run_pressed = st.button("Generate tweet")
# 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_keys_configured"):
run_query = (
    run_pressed or question != st.session_state.question
)
if run_query and question:
    reset_results()
    st.session_state.question = question
    with st.spinner("πŸ”Ž"):
        try:
            st.session_state.result = query(agent, question)
        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:
    result = st.session_state.result
    st.write(result["answers"][0].answer)