Spaces:
Sleeping
Sleeping
File size: 2,276 Bytes
af29019 f0dc6f5 af29019 8b852aa af29019 4b99f88 af29019 4b99f88 af29019 f0dc6f5 af29019 4b99f88 af29019 4b99f88 af29019 f0dc6f5 |
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 57 58 59 60 61 62 63 64 65 66 67 68 |
import streamlit as st
import requests
import os
SECRET_TOKEN = os.getenv("SECRET_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/ProsusAI/finbert"
headers = {"Authorization": "Bearer " + SECRET_TOKEN}
def show_plot(bot_response):
scores = {label_info['label']: label_info['score'] for label_info in bot_response}
st.bar_chart(scores)
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def display_chat_history(chat_history):
for entry in chat_history[::-1]:
if entry["speaker"] == "You":
st.text(f"You: {entry['message']}")
elif entry["speaker"] == "Chatbot":
try:
st.text(f"Chatbot:")
# for label_info in entry['message'][0]:
# st.write(f"Label: {label_info['label']}, Score: {label_info['score']}")
print(entry['message'])
show_plot(entry['message'][0])
except:
st.text(f"Something went wrong.")
def display_bot_response(bot_response):
st.write("Bot Response:")
for label_info in bot_response[0]:
st.write(f"Label: {label_info['label']}, Score: {label_info['score']}")
def main():
st.title("Chatbot Interface")
# Initialize chat history
chat_history = st.session_state.get("chat_history", [])
# Input box for user to type messages
user_input = st.text_input("Type your message here:")
if st.button("Send"):
if user_input:
# Make an API call to get the chatbot's response
bot_response = query({
"inputs": user_input,
})
# Update the chat history
chat_history.append({"speaker": "Chatbot", "message": bot_response})
chat_history.append({"speaker": "You", "message": user_input})
# Save the chat history in session state
st.session_state.chat_history = chat_history
# Display the chat history
display_chat_history(chat_history)
# Display the bot response if available
# if chat_history and chat_history[-1]["speaker"] == "Chatbot":
# display_bot_response(chat_history[-1]["message"])
if __name__ == "__main__":
main() |