File size: 2,508 Bytes
c325552
 
 
 
 
7a4f3b9
5b37c89
c325552
c3d7567
c325552
 
 
 
 
c3d7567
 
7a4f3b9
c3d7567
 
7a4f3b9
c3d7567
1851971
c3d7567
c325552
7a4f3b9
c325552
c3d7567
c325552
 
 
c3d7567
 
c325552
7a4f3b9
c325552
 
 
 
 
 
 
 
 
89bf715
 
 
 
 
 
 
 
 
c325552
 
 
7a4f3b9
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

from json import JSONDecodeError
import logging

import streamlit as st

from utils.haystack import run_agent, start_haystack
from utils.ui import reset_results, set_initial_state, sidebar
from utils.config import OPENAI_API_KEY, SERPER_KEY

set_initial_state()

sidebar()

st.write("# ๐Ÿ‘ฉ What would they have posted about this?")
st.write("### This app had to be changed from searching Twitter posts to searching Mastodon posts due to API changes")
st.write("## About this App")
st.write("This app, built with [Haystack](https://github.com/deepset-ai/haystack#readme) uses an [Agent](https://docs.haystack.deepset.ai/docs/agent) (with GPT-4) with a `WebSearch` and `MastodonRetriever` tool")
st.write("The `MastodonRetriever` is set to retrieve the latest 20 mastodon posts by the given Mastodon username to construct an understanding of their style of posting")
st.write("### Instructions")
st.write("For best results, please provide a Mastodon username as it appears on twitter. E.g.: [[email protected]](https://mastodon.social/@ivepetthatdog) ๐Ÿถ")
st.write("### Example")
st.image("./images/mastodon_example.png")

# if st.session_state.get("OPENAI_API_KEY") and st.session_state.get("SERPER_KEY"):

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

# Search bar
question = st.text_input("Example: If the twitter account tuanacelik were to write a post in their style about climate change, what would it be?", on_change=reset_results) 
run_pressed = st.button("Generate post")
# else:
#     st.write("Please provide your OpenAI and SerperDev Keys 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
    try:
        st.session_state.result = run_agent(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)