Spaces:
Running
Running
Tuana
commited on
Commit
•
a576a00
0
Parent(s):
first commit
Browse files- .gitignore +3 -0
- README.md +17 -0
- app.py +45 -0
- requirements.txt +5 -0
- utils/__init__.py +0 -0
- utils/config.py +6 -0
- utils/haystack.py +59 -0
- utils/ui.py +12 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
__pycache__
|
3 |
+
.DS*
|
README.md
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Temaplate Streamlit App for Haystack Search Pipelines
|
2 |
+
|
3 |
+
In this repo you will find a template streamlit app set up for search applications.
|
4 |
+
|
5 |
+
## Installation and Running
|
6 |
+
To run the bare application which does _nothing_:
|
7 |
+
1. Install requirements:
|
8 |
+
`pip install -r requirements.txt`
|
9 |
+
2. Run the streamlit app:
|
10 |
+
`streamlit run app.py`
|
11 |
+
3. Createa a `.env` and add your Twitter Bearer and OpenAI tokens:
|
12 |
+
`TWITTER_BEARER_TOKEN` and `OPEN_AI_KEY`
|
13 |
+
|
14 |
+
This will start up the app on `localhost:8501` where you will dind a simple search bar
|
15 |
+
|
16 |
+
## What to edit:
|
17 |
+
|
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from annotated_text import annotation
|
3 |
+
from json import JSONDecodeError
|
4 |
+
import logging
|
5 |
+
from markdown import markdown
|
6 |
+
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
from utils.haystack import query
|
10 |
+
from utils.ui import reset_results, set_initial_state
|
11 |
+
|
12 |
+
set_initial_state()
|
13 |
+
|
14 |
+
st.write("# What have they been tweeting about lately?")
|
15 |
+
|
16 |
+
# Search bar
|
17 |
+
username = st.text_input("", value=st.session_state.username, max_chars=100, on_change=reset_results)
|
18 |
+
|
19 |
+
run_pressed = st.button("Run")
|
20 |
+
|
21 |
+
run_query = (
|
22 |
+
run_pressed or username != st.session_state.username
|
23 |
+
)
|
24 |
+
|
25 |
+
# Get results for query
|
26 |
+
if run_query and username:
|
27 |
+
reset_results()
|
28 |
+
st.session_state.username = username
|
29 |
+
with st.spinner("🔎"):
|
30 |
+
try:
|
31 |
+
st.session_state.result = query(username)
|
32 |
+
except JSONDecodeError as je:
|
33 |
+
st.error(
|
34 |
+
"👓 An error occurred reading the results. Is the document store working?"
|
35 |
+
)
|
36 |
+
except Exception as e:
|
37 |
+
logging.exception(e)
|
38 |
+
st.error("🐞 An error occurred during the request.")
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
if st.session_state.result:
|
43 |
+
voice = st.session_state.result
|
44 |
+
st.write(voice[0])
|
45 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
farm-haystack==1.13.0
|
2 |
+
streamlit==1.10.0
|
3 |
+
markdown
|
4 |
+
st-annotated-text
|
5 |
+
python-dotenv
|
utils/__init__.py
ADDED
File without changes
|
utils/config.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
|
4 |
+
load_dotenv()
|
5 |
+
TWITTER_BEARER = os.getenv('TWITTER_BEARER_TOKEN')
|
6 |
+
OEPN_AI_KEY = os.getenv('OPEN_AI_KEY')
|
utils/haystack.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from utils.config import TWITTER_BEARER, OEPN_AI_KEY
|
4 |
+
|
5 |
+
from haystack.nodes import PromptNode, PromptTemplate
|
6 |
+
|
7 |
+
# cached to make index and models load only at start
|
8 |
+
@st.cache(
|
9 |
+
hash_funcs={"builtins.SwigPyObject": lambda _: None}, allow_output_mutation=True
|
10 |
+
)
|
11 |
+
def start_haystack():
|
12 |
+
#Use this function to contruct a pipeline
|
13 |
+
prompt_node = PromptNode(model_name_or_path="text-davinci-003", api_key=OEPN_AI_KEY)
|
14 |
+
|
15 |
+
twitter_template = PromptTemplate(name="twitter-voice", prompt_text="""You will be given a twitter stream belonging to a specific profile. Tell us what they've lately been tweeting about and in what languages.
|
16 |
+
You may go into some detail about what topics they tend to like tweeting about. Please also mention their overall tone, for example: positive,
|
17 |
+
negative, political, sarcastic or something else.
|
18 |
+
|
19 |
+
Example:
|
20 |
+
|
21 |
+
Twitter stream: Many people in our community asked how to utilize LLMs in their NLP pipelines and how to modify prompts for their tasks.…
|
22 |
+
RT @deepset_ai: We use parts of news articles from The Guardian as documents and create custom prompt templates to categorize these article
|
23 |
+
|
24 |
+
Voice: This person has lately been tweeting about NLP and LLMs. Their tweets have been in Enlish
|
25 |
+
|
26 |
+
Example:
|
27 |
+
|
28 |
+
Twitter stream: I've directed my team to set sharper rules on how we deal with unidentified objects.\n\nWe will inventory, improve ca…
|
29 |
+
the incursion by China’s high-altitude balloon, we enhanced radar to pick up slower objects.\n \nBy doing so, w…
|
30 |
+
I gave an update on the United States’ response to recent aerial objects.
|
31 |
+
|
32 |
+
Voice: This person has lately been tweeting about an unidentified object and an incursion by China with a high-altitude baloon.
|
33 |
+
They have been tweeting about the USA. They have had a political tone. They mostly post in English.
|
34 |
+
|
35 |
+
Twitter stream: $tweets.
|
36 |
+
|
37 |
+
Voice:
|
38 |
+
""")
|
39 |
+
return prompt_node, twitter_template
|
40 |
+
|
41 |
+
prompter, template = start_haystack()
|
42 |
+
|
43 |
+
@st.cache(allow_output_mutation=True)
|
44 |
+
def query(username):
|
45 |
+
|
46 |
+
bearer_token = TWITTER_BEARER
|
47 |
+
|
48 |
+
headers = {"Authorization": "Bearer {}".format(bearer_token)}
|
49 |
+
|
50 |
+
url = f"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={username}&count={100}"
|
51 |
+
try:
|
52 |
+
response = requests.request("GET", url, headers = headers)
|
53 |
+
twitter_stream = ""
|
54 |
+
for tweet in response.json():
|
55 |
+
twitter_stream += (tweet["text"])
|
56 |
+
result = prompter.prompt(prompt_template=template, tweets=twitter_stream[0:4097])
|
57 |
+
except:
|
58 |
+
result = ["Please make sure you are providing a correct, public twitter accout"]
|
59 |
+
return result
|
utils/ui.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
def set_state_if_absent(key, value):
|
4 |
+
if key not in st.session_state:
|
5 |
+
st.session_state[key] = value
|
6 |
+
|
7 |
+
def set_initial_state():
|
8 |
+
set_state_if_absent("username", "Provide a Twitter username")
|
9 |
+
set_state_if_absent("result", None)
|
10 |
+
|
11 |
+
def reset_results(*args):
|
12 |
+
st.session_state.results = None
|