atishayj281 commited on
Commit
af29019
·
1 Parent(s): 629b030
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+ API_URL = "https://api-inference.huggingface.co/models/ProsusAI/finbert"
5
+ headers = {"Authorization": "Bearer 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"}
6
+
7
+ def query(payload):
8
+ response = requests.post(API_URL, headers=headers, json=payload)
9
+ return response.json()
10
+
11
+ def display_chat_history(chat_history):
12
+ for entry in chat_history:
13
+ if entry["speaker"] == "You":
14
+ st.text(f"You: {entry['message']}")
15
+ elif entry["speaker"] == "Chatbot":
16
+ st.text(f"Chatbot:")
17
+
18
+ for label_info in entry['message'][0]:
19
+ st.write(f"Label: {label_info['label']}, Score: {label_info['score']}")
20
+
21
+
22
+ def display_bot_response(bot_response):
23
+ st.write("Bot Response:")
24
+ for label_info in bot_response[0]:
25
+ st.write(f"Label: {label_info['label']}, Score: {label_info['score']}")
26
+
27
+ def main():
28
+ st.title("Chatbot Interface")
29
+
30
+ # Initialize chat history
31
+ chat_history = st.session_state.get("chat_history", [])
32
+
33
+ # Input box for user to type messages
34
+ user_input = st.text_input("Type your message here:")
35
+
36
+ if st.button("Send"):
37
+ if user_input:
38
+ # Make an API call to get the chatbot's response
39
+ bot_response = query({
40
+ "inputs": user_input,
41
+ })
42
+
43
+ # Update the chat history
44
+ chat_history.append({"speaker": "You", "message": user_input})
45
+ chat_history.append({"speaker": "Chatbot", "message": bot_response})
46
+
47
+ # Save the chat history in session state
48
+ st.session_state.chat_history = chat_history
49
+
50
+ # Display the chat history
51
+ display_chat_history(chat_history)
52
+
53
+ # Display the bot response if available
54
+ # if chat_history and chat_history[-1]["speaker"] == "Chatbot":
55
+ # display_bot_response(chat_history[-1]["message"])
56
+
57
+ if __name__ == "__main__":
58
+ main()