Spaces:
Runtime error
Runtime error
Create app.py and requirements.txt
Browse files- app.py +45 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}
|
11 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
12 |
+
return response.json()
|
13 |
+
|
14 |
+
|
15 |
+
# Initialize the `prompts` session state
|
16 |
+
if "prompts" not in st.session_state:
|
17 |
+
st.session_state.prompts = []
|
18 |
+
|
19 |
+
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)
|
41 |
+
|
42 |
+
|
43 |
+
with col2:
|
44 |
+
for index in range(len(st.session_state.prompts)):
|
45 |
+
card(index)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
requests
|