whitphx HF Staff commited on
Commit
87a4d41
·
1 Parent(s): 2909846

Change to call the API inside on_change

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -1,10 +1,15 @@
 
 
1
  import streamlit as st
2
  import os
3
  import requests
4
 
5
 
6
- @st.cache_data
 
 
7
  def query(text):
 
8
  API_URL = "https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base"
9
  headers = {"Authorization": "Bearer " + os.environ["HF_API_KEY"]}
10
  payload = {"inputs": text}
@@ -20,21 +25,20 @@ col1, col2 = st.columns(2)
20
 
21
  with col1:
22
  if st.button("Add query"):
23
- st.session_state.prompts.append("")
24
  if st.button("Remove query"):
25
  st.session_state.prompts.pop(0)
26
 
27
 
28
  def card(index: int):
29
  def on_change():
30
- # Update the `prompts[index]` session state with the value from the text input which is specified by the key, `f"prompt_{index}"`.
31
- st.session_state.prompts[index] = st.session_state[f"prompt_{index}"]
32
- st.text_input("Prompt:", key=f"prompt_{index}", value=st.session_state.prompts[index], on_change=on_change)
33
 
34
- prompt = st.session_state.prompts[index]
35
 
36
- # This `query` function is cached, so it will only be re-run if the input `prompt` changes.
37
- result = query(prompt)
38
 
39
  st.write(f'Prompt: "{prompt}"')
40
  st.json(result)
 
1
+ import logging
2
+
3
  import streamlit as st
4
  import os
5
  import requests
6
 
7
 
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
  def query(text):
12
+ logger.info("Query the API with the text: %s" % text)
13
  API_URL = "https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base"
14
  headers = {"Authorization": "Bearer " + os.environ["HF_API_KEY"]}
15
  payload = {"inputs": text}
 
25
 
26
  with col1:
27
  if st.button("Add query"):
28
+ st.session_state.prompts.append(("", None))
29
  if st.button("Remove query"):
30
  st.session_state.prompts.pop(0)
31
 
32
 
33
  def card(index: int):
34
  def on_change():
35
+ prompt = st.session_state[f"prompt_{index}"]
36
+ result = query(prompt)
37
+ st.session_state.prompts[index] = (prompt, result)
38
 
39
+ st.text_input("Prompt:", key=f"prompt_{index}", value=st.session_state.prompts[index][0], on_change=on_change)
40
 
41
+ prompt, result = st.session_state.prompts[index]
 
42
 
43
  st.write(f'Prompt: "{prompt}"')
44
  st.json(result)