Aditya7864 commited on
Commit
2d574a1
·
verified ·
1 Parent(s): 03ea7b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import requests
4
+
5
+ def main():
6
+ st.title("🤖 Simple Chatbot Interface")
7
+
8
+ # Input for API URL
9
+ api_url = st.text_input("Enter API URL", placeholder="https://example.com/query")
10
+
11
+ # Display the chatbot interface only if an API URL is provided
12
+ if api_url:
13
+ st.write("Chatbot is ready to use!")
14
+
15
+ if "chat_history" not in st.session_state:
16
+ st.session_state["chat_history"] = []
17
+
18
+ user_input = st.chat_input(placeholder="Type your query here...")
19
+
20
+ if user_input:
21
+ # Append user query to the chat history
22
+ st.session_state["chat_history"].append({"role": "user", "content": user_input})
23
+
24
+ # Prepare the query for the API
25
+ input_data_for_model = {"query": user_input}
26
+
27
+ with st.spinner("Generating response..."):
28
+ try:
29
+ # Send the user query to the API
30
+ response = requests.post(
31
+ api_url,
32
+ data=json.dumps(input_data_for_model),
33
+ headers={"Content-Type": "application/json"}
34
+ )
35
+
36
+ if response.status_code == 200:
37
+ response_data = response.json() # Parse API response
38
+ bot_reply = response_data.get("answer", "I'm sorry, I didn't understand that.")
39
+ else:
40
+ bot_reply = f"Error: Received status code {response.status_code}"
41
+
42
+ except Exception as e:
43
+ bot_reply = f"Error: {str(e)}"
44
+
45
+ # Append the assistant's reply to the chat history
46
+ st.session_state["chat_history"].append({"role": "assistant", "content": bot_reply})
47
+
48
+ # Display the conversation history
49
+ for chat in st.session_state["chat_history"]:
50
+ if chat["role"] == "user":
51
+ st.chat_message("user").write(chat["content"])
52
+ elif chat["role"] == "assistant":
53
+ st.chat_message("assistant").write(chat["content"])
54
+
55
+ if __name__ == "__main__":
56
+ main()