abanm commited on
Commit
383b748
·
verified ·
1 Parent(s): 3aca786

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -10
app.py CHANGED
@@ -1,5 +1,47 @@
 
 
1
  import streamlit as st
2
- from api_helper import call_api # Import the updated API helper
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Streamlit Configurations
5
  st.set_page_config(page_title="Chatbot", layout="centered")
@@ -15,29 +57,27 @@ def handle_input():
15
  # Save User Message
16
  st.session_state["messages"].append({"role": "user", "content": user_message})
17
 
18
-
19
- # Display Bot Response Placeholder
20
- response_placeholder = st.empty() # Create an empty container for dynamic updates
21
  bot_response = ""
22
 
23
  with st.spinner("Fetching response..."):
24
  for part in call_api(user_message):
25
- bot_response += part # Append each part to the complete response
26
  response_placeholder.markdown(
27
- f"<div style='text-align: left; color: green; margin: 10px 0;'><b>Bot:</b> {bot_response}</div>",
28
- unsafe_allow_html=True,
29
- )
30
 
31
  # Save Final Bot Response
32
  st.session_state["messages"].append({"role": "assistant", "content": bot_response})
33
 
34
-
35
  # Clear Input Field
36
  st.session_state.user_input = "" # Reset user input field
37
 
38
  # Main Chat UI
39
  st.title("DUBS v.0.0.3")
40
- st.markdown("Emnpowering you with a Sustainable AI ")
41
 
42
  # Chat Display
43
  for message in st.session_state["messages"]:
@@ -59,3 +99,4 @@ st.text_input(
59
  key="user_input",
60
  on_change=handle_input,
61
  )
 
 
1
+ import requests
2
+ import json
3
  import streamlit as st
4
+
5
+ # Constants
6
+ SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
7
+ API_KEY = "s3cr3t_k3y" # Replace with your actual API key
8
+
9
+ def call_api(message):
10
+ """
11
+ Calls the backend API with the user's input using streaming.
12
+
13
+ Args:
14
+ message (str): User's message.
15
+
16
+ Yields:
17
+ str: Streamed response from the backend API or an error message.
18
+ """
19
+ headers = {
20
+ "Authorization": f"Bearer {API_KEY}",
21
+ "Content-Type": "application/json",
22
+ }
23
+ payload = {"prompt": message} # Match the API's expected input key
24
+
25
+ try:
26
+ # Send request with streaming enabled
27
+ response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
28
+ response.raise_for_status()
29
+
30
+ # Stream each line of the response
31
+ for chunk in response.iter_lines(decode_unicode=True):
32
+ if chunk.strip(): # Ensure the chunk is not empty
33
+ try:
34
+ # Parse JSON chunk
35
+ data = json.loads(chunk)
36
+ if "response" in data:
37
+ yield data["response"]
38
+ except json.JSONDecodeError:
39
+ yield "\nError decoding response chunk."
40
+
41
+ except requests.exceptions.Timeout:
42
+ yield "Error: The API call timed out."
43
+ except requests.exceptions.RequestException as e:
44
+ yield f"Error: {str(e)}"
45
 
46
  # Streamlit Configurations
47
  st.set_page_config(page_title="Chatbot", layout="centered")
 
57
  # Save User Message
58
  st.session_state["messages"].append({"role": "user", "content": user_message})
59
 
60
+ # Placeholder for dynamic bot response
61
+ response_placeholder = st.empty() # Create a dynamic container
 
62
  bot_response = ""
63
 
64
  with st.spinner("Fetching response..."):
65
  for part in call_api(user_message):
66
+ bot_response += part # Append each part to the full response
67
  response_placeholder.markdown(
68
+ f"<div style='text-align: left; color: green; margin: 10px 0;'><b>Bot:</b> {bot_response}</div>",
69
+ unsafe_allow_html=True,
70
+ )
71
 
72
  # Save Final Bot Response
73
  st.session_state["messages"].append({"role": "assistant", "content": bot_response})
74
 
 
75
  # Clear Input Field
76
  st.session_state.user_input = "" # Reset user input field
77
 
78
  # Main Chat UI
79
  st.title("DUBS v.0.0.3")
80
+ st.markdown("Empowering you with a Sustainable AI")
81
 
82
  # Chat Display
83
  for message in st.session_state["messages"]:
 
99
  key="user_input",
100
  on_change=handle_input,
101
  )
102
+