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()